【意义】
这种方式取Car下的Engine,实际还是走了两次DB,一次是拿id取Car的属性,一次是用car的eid去取Engine的属性,没有节省时间,效率不比在代码中配置Engine和Car的关联高,意义不大。
例程:https://files.cnblogs.com/files/heyang78/MybatisCli-CarEnginCompanyEmp-220528.rar?t=1653685453
【目标】
完成汽车Car中对应引擎Engine的配置。
【两个类】
Car类:
package com.hy.entity; public class Car { private long id; private String name; private Engine engine; public String toString(){ return String.format("Car id=%d name=%s engine=%s",id,name,engine.getName()); } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Engine getEngine() { return engine; } public void setEngine(Engine engine) { this.engine = engine; } }
Engine类:
package com.hy.entity; public class Engine { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
注意:以下Mapper接口和xml可以合并成一个,不是非两个不可。
【两个Mapper接口】
Car接口:
package com.hy.dao; import com.hy.entity.Car; import com.hy.entity.Company; import org.apache.ibatis.annotations.Mapper; @Mapper public interface CarMapper { Car fetchOne(long id); }
Engine接口:
package com.hy.dao; import com.hy.entity.Car; import com.hy.entity.Engine; import org.apache.ibatis.annotations.Mapper; @Mapper public interface EngineMapper { Engine fetchOne(long id); }
【两个Mapper.xml】
CarMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hy.dao.CarMapper"> <select id="fetchOne" resultMap="map2"> select id,name,eid from car where id=#{id} </select> <resultMap id="map2" type="com.hy.entity.Car"> <id column="id" property="id"/> <result column="name" property="name"/> <association column="eid" property="engine" select="com.hy.dao.EngineMapper.fetchOne" javaType="com.hy.entity.Engine"/> </resultMap> </mapper>
EngineMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hy.dao.EngineMapper"> <select id="fetchOne" resultType="com.hy.entity.Engine"> select id,name from engine where id=#{id} </select> </mapper>
【执行程序】
package com.hy; import com.hy.dao.CarMapper; import com.hy.dao.CompanyMapper; import com.hy.entity.Car; import com.hy.entity.Company; import com.hy.entity.Emp; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.InputStream; import java.util.List; @SpringBootApplication public class MyApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sqlSessionFactory.openSession()) { CarMapper mapper = session.getMapper(CarMapper.class); Car honda= mapper.fetchOne(1); System.out.println(honda); Car benz= mapper.fetchOne(3); System.out.println(benz); } } }
【输出】
Car id=1 name=honda engine=v1
Car id=3 name=benz engine=v3
【参考资料】
《Spring+MyBatis企业应用实战》(第2版)疯狂软件编著 P211
END