This article directory
introduction
When talking about resultMap
elements earlier, [Mybatis] configures the resultMap element and resultType element of the mapping file . This element contains a sub-element association
, Mybatis
which is mainly used to process one-to-one association relationships.
association
element, usually the following attributes can be configured
- property: Specifies the object property mapped to the entity class.
- column: Specifies the corresponding field in the table (that is, the column name returned by the query).
- javaType: Specifies the type mapped to the attribute of the entity object.
- select: Specifies the sub-SQL statement that introduces the nested query, this attribute is used for the nested query in the association map
Application scenario example: the relationship between a person and an ID card is one-to-one, that is, a person can only have one ID card, and an ID card can only correspond to one person.
Create project and database tables
Create a Spring integration Mybatis project [Spring] Spring integration Mybatis case
Create idcard
tables and user
tables
DROP TABLE IF EXISTS `idcard`;
CREATE TABLE `idcard` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(25) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user` (
`card_id` int(11) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `card_id` (`card_id`),
CONSTRAINT `card_id` FOREIGN KEY (`card_id`) REFERENCES `idcard` (`id`) ON DELETE SET NULL
)
The project structure is as follows:
Create entity classes and database operation interfaces
IdCard class
public class IdCard {
private int id;
private String code;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "Idcard{" +
"id=" + id +
", code='" + code + '\'' +
'}';
}
}
User class
public class User {
private int id;
private String name;
private int age;
private IdCard idcard;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public IdCard getIdcard() {
return idcard;
}
public void setIdcard(IdCard idcard) {
this.idcard = idcard;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", idcard=" + idcard +
'}';
}
}
IdCardMapper
@Repository("idCardMapper")
@Mapper
public interface IdCardMapper {
IdCard findCardById(int id);
}
UserMapper
@Repository("userMapper")
@Mapper
public interface UserMapper {
List<User> findAllUser();
}
Create configuration file
mybatis-config.xml
<?xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0 //EN"
"http//mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<settings>
<!--在使用MyBatis嵌套查询方式进行关联查询时,使用MyBatis的延迟加载可以在一定程度上提高查询效率-->
<!--打开延迟加载的开关-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--将积极加载改为按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<mappers>
<mapper resource="UserMapper.xml"/>
<mapper resource="IdCardMapper.xml"/>
</mappers>
</configuration>
IdCardMapper.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.lucas.mybatis.mapper.IdCardMapper">
<select id="findCardById" parameterType="Integer" resultType="com.lucas.mybatis.model.IdCard" >
select * from idcard where id=#{id}
</select>
</mapper>
UserMapper.xml
method 1
<?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.lucas.mybatis.mapper.UserMapper">
<!-- 定义结果集,column表示sql查询列,property表示映射到实体的属性 -->
<resultMap type="com.lucas.mybatis.model.User" id="cardAndUser">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<!-- 关联对象 -->
<!-- 使用select属性指定第二步调用的方法,并通过column指定传递的参数值,这个值是第一步的查询的数据 -->
<association property="idcard" column="card_id"
javaType="com.lucas.mybatis.model.IdCard" select="com.lucas.mybatis.mapper.IdCardMapper.findCardById"/>
</resultMap>
<!-- 第一步值只查询user表 -->
<select id="findAllUser" resultMap="cardAndUser">
select * from user
</select>
</mapper>
Method 2
<?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.lucas.mybatis.mapper.UserMapper">
<resultMap type="com.lucas.mybatis.model.User" id="cardAndUser">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<!-- 一对一级联查询-->
<association property="idcard" javaType="com.lucas.mybatis.model.IdCard" column="card_id">
<!-- idcard的id对应着user的card_id -->
<id property="id" column="card_id"/>
<result property="code" column="code"/>
</association>
</resultMap>
<!-- resultMap指定使用上面定义的结果集,查询使用关联查询,查询列要和上面的column对应 -->
<select id="findAllUser" resultMap="cardAndUser">
select user.*,idcard.code
from `user`,idcard
where user.card_id=idcard.id
</select>
</mapper>
Method 3
<?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.lucas.mybatis.mapper.UserMapper">
<select id="findAllUser" resultType="com.lucas.mybatis.model.UserPojo" >
select user.*,idcard.code from `user`,idcard where user.card_id=idcard.id
</select>
</mapper>
UserPojo
kind
public class UserPojo {
private int id;
private String name;
private int age;
private String code;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "UserPojo{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", code='" + code + '\'' +
'}';
}
}
add test code
public class App {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
List<User> users = userMapper.findAllUser();
for (User user :users){
System.out.println(user);
}
}
}