- 单表查询
- 一对一关联查询(非懒加载 or 懒加载)
- 一对多关联查询(懒加载)
表结构介绍
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20),
PASSWORD VARCHAR(32),
user_expansion_id INT
);
CREATE TABLE user_expansion(
id INT PRIMARY KEY AUTO_INCREMENT,
address VARCHAR(20)
);
CREATE TABLE user_groups(
id INT PRIMARY KEY AUTO_INCREMENT,
group_name VARCHAR(20)
);
CREATE TABLE user_group_relation(
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
group_id INT
);
-- DML 基础数据
INSERT INTO users(username,PASSWORD,user_expansion_id) VALUES ('admin','admin',1);
INSERT INTO user_expansion(address) VALUES ('河北省沧州市XXX');
INSERT INTO user_groups(group_name) VALUES('Administrator');
INSERT INTO user_group_relation(user_id,group_id) VALUES(1,1);
INSERT INTO user_groups(group_name) VALUES('User');
INSERT INTO user_group_relation(user_id,group_id) VALUES(1,2);
1.单表查询(so easy)
根据主键查询用户
<select id="selectById" parameterType="java.lang.Long" resultType="com.xbz.user.entity.User">
SELECT * FROM users WHERE id = #{id}
select>
2.1一对一关联查询(非懒加载)
根据主键查询用户及其扩展信息
2.2一对一关联查询(懒加载)
注意:使用懒加载必须需要在mybatis的configure文件配置:
lazyLoadingEnabled
aggressiveLazyLoading
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<select id="selectUserAndLazyExpansion" resultMap="userMap">
SELECT
id,username,password,
user_expansion_id AS "expansionId"
FROM
users
WHERE
id=#{id}
select>
<select id="selectExpansionById" resultType="com.xbz.user.entity.UserExpansion">
SELECT
id,address
FROM
user_expansion
WHERE
id=#{id}
select>
<resultMap id="userMap" type="com.xbz.user.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<association
property="expansion"
column="expansionId"
select="selectExpansionById">
association>
resultMap>
3.多对多关联查询(懒加载)
<select id="selectUserAndGroups" resultMap="userMap">
SELECT
id,username,password,user_expansion_id
FROM
users
WHERE
id=#{id}
select>
<select id="selectGroupsByUserId" resultType="com.xbz.user.entity.UserGroup">
SELECT
g.id,g.group_name
FROM
user_groups g,user_group_relation r
WHERE
g.id=r.group_id
AND r.user_id=#{uid}
select>
<resultMap id="userMap" type="com.xbz.user.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<association
property="expansion"
column="expansionId"
select="selectExpansionById">
association>
<collection
property="groups"
column="id"
ofType="com.xbz.user.entity.UserGroup"
select="selectGroupsByUserId">
<id column="id" property="id"/>
<result column="group_name" property="groupName" />
collection>
resultMap>
完整配置
mapper Class
package com.xbz.user.mapper;
import java.io.Serializable;
import com.xbz.user.entity.User;
public interface UserMapper {
public User selectById(Serializable id);
public void save(User user);
public void deleteById(Serializable id);
public void update(User user);
/**
* 根据id查询用户并获取用户拓展信息
*
测试mybatis的一对一关联查询(非懒加载)
* @param id
* @return
*/
public User selectUserAndExpansion(Serializable id);
/**
* 根据id查询用户并懒加载获取用户拓展信息
*
测试mybatis的一对一关联查询(懒加载)
* @param id
* @return
*/
public User selectUserAndLazyExpansion(Serializable id);
/**
* 根据id查询用户并获取用户所在用户组信息
*
测试mybatis的一对多关联查询(懒加载)
* @param id
* @return
*/
public User selectUserAndGroups(Serializable id);
}
相关实体类
getter、setter省略
package com.xbz.user.entity;
import java.io.Serializable;
import java.util.Set;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String password;
private UserExpansion expansion;
private Set groups;
}
public class UserExpansion implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String address;
}
public class UserGroup implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String groupName;
}
mapper.xml
<mapper namespace="com.xbz.user.mapper.UserMapper">
<select id="selectById" parameterType="java.lang.Long" resultType="com.xbz.user.entity.User">
SELECT * FROM users WHERE id = #{id}
select>
<select id="selectUserAndExpansion" resultType="com.xbz.user.entity.User">
SELECT
u.id,u.username,u.password,u.user_expansion_id,
ue.id AS "expansion.id",
ue.address AS "expansion.address"
FROM
users u,user_expansion ue
WHERE
u.user_expansion_id=ue.id
AND u.id=#{id}
select>
<select id="selectUserAndLazyExpansion" resultMap="userMap">
SELECT
id,username,password,user_expansion_id AS "expansionId"
FROM
users
WHERE
id=#{id}
select>
<select id="selectExpansionById" resultType="com.xbz.user.entity.UserExpansion">
SELECT
id,address
FROM
user_expansion
WHERE
id=#{id}
select>
<select id="selectUserAndGroups" resultMap="userMap">
SELECT
id,username,password,user_expansion_id
FROM
users
WHERE
id=#{id}
select>
<select id="selectGroupsByUserId" resultType="com.xbz.user.entity.UserGroup">
SELECT
g.id,g.group_name
FROM
user_groups g,user_group_relation r
WHERE
g.id=r.group_id
AND r.user_id=#{uid}
select>
<resultMap id="userMap" type="com.xbz.user.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<association
property="expansion"
column="expansionId"
select="selectExpansionById">
association>
<collection
property="groups"
column="id"
ofType="com.xbz.user.entity.UserGroup"
select="selectGroupsByUserId">
<id column="id" property="id"/>
<result column="group_name" property="groupName" />
collection>
resultMap>
mapper>
测试类
package com.xbz.test.mapper;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.xbz.user.entity.User;
import com.xbz.user.entity.UserExpansion;
import com.xbz.user.mapper.UserMapper;
/**
* Spring测试配置使用如下注解
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/*.xml")
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
@Test
public void testSelectUserAndExpansion(){
User user = userMapper.selectUserAndExpansion(1L);
System.out.println("-----------------------------------------------------------");
user.getExpansion();
System.out.println(user);
}
@Test
public void testSelectUserAndLazyExpansion(){
User user = userMapper.selectUserAndLazyExpansion(1L);
System.out.println("-----------------------------------------------------------");
UserExpansion expansion = user.getExpansion();
System.out.println(expansion);
}
@Test
public void testSelectUserAndGroups(){
User user = userMapper.selectUserAndGroups(1L);
System.out.println("-----------------------------------------------------------");
System.out.println(Arrays.toString(user.getGroups().toArray()));
}
}