1、dbconfig.properties数据库连接配置
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://192.168.121.200:3306/mybatis
username=root
password=root
2、log4j.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
3、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>
<properties resource="dbconfig.properties"/>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<package name="com.atguigu.domain"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="com/atguigu/mapper/UserMapper.xml"/>-->
<package name="com.atguigu.mapper"/>
</mappers>
</configuration>
4、实体类
public class User {
private Long userId;
private Long deptId;
private String userName;
private String email;
private Dept dept;
}
public class Dept {
private Long deptId;
private String deptName;
private List<User> users;
}
5、Mapper接口
public interface UserMapper {
public User getUserById(Long id);
public User getUserAndDeptByUserId(Long userId);
public User getUserAndDeptByUserIdStep(Long userId);
public User getUserByDeptId(Long deptId);
}
public interface DeptMapper {
public Dept getDeptByDeptId(Long deptId);
public Dept getDeptAndUserByDeptId(Long deptId);
public Dept getDeptAndUserByDeptIdStep(Long deptId);
}
6、mapper XML
UserMapper.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.atguigu.mapper.UserMapper">
<!-- association-嵌套结果集 开始 -->
<resultMap id="userAndDeptResultMap1" type="user">
<id property="userId" column="user_id" />
<result property="userName" column="user_name" />
<result property="email" column="email" />
<result property="deptId" column="dept_id" />
<result property="dept.deptId" column="dept_id" />
<result property="dept.deptName" column="dept_name" />
</resultMap>
<resultMap id="userAndDeptResultMap2" type="user">
<id property="userId" column="user_id" />
<result property="userName" column="user_name" />
<result property="email" column="email" />
<result property="deptId" column="dept_id" />
<association property="dept" javaType="dept">
<result property="deptId" column="dept_id" />
<result property="deptName" column="dept_name" />
</association>
</resultMap>
<select id="getUserAndDeptByUserId" resultMap="userAndDeptResultMap2">
SELECT
u.user_id,
u.user_name,
u.email,
u.dept_id,
d.dept_name
FROM
sys_user u
LEFT JOIN sys_dept d ON d.dept_id = u.dept_id
WHERE
u.user_id = #{userId}
</select>
<!-- association-嵌套结果集 结束 -->
<!-- association-分段结果集 开始 -->
<resultMap id="userAndDeptResultMapStep" type="user">
<id property="userId" column="user_id" />
<result property="userName" column="user_name" />
<result property="email" column="email" />
<result property="deptId" column="dept_id" />
<association property="dept" javaType="dept"
select="com.atguigu.mapper.DeptMapper.getDeptByDeptId"
column="dept_id"
>
<result property="deptId" column="dept_id" />
<result property="deptName" column="dept_name" />
</association>
</resultMap>
<select id="getUserAndDeptByUserIdStep" resultMap="userAndDeptResultMapStep">
select * from sys_user where user_id = #{id}
</select>
<!-- association-分段结果集 结束 -->
<select id="getUserById" resultType="user">
select * from sys_user where user_id = #{id}
</select>
<select id="getUserByDeptId" resultType="user">
select * from sys_user where dept_id = #{deptId}
</select>
</mapper>
DeptMapper.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.atguigu.mapper.DeptMapper">
<select id="getDeptByDeptId" resultType="dept">
select * from sys_dept where dept_id = #{id}
</select>
<!-- collection-嵌套结果集 开始 -->
<resultMap id="DeptAndUserResultMap" type="dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
<collection property="users" ofType="user" columnPrefix="u_">
<id property="userId" column="user_id"/>
<result property="userName" column="user_name"/>
<result property="email" column="email"/>
</collection>
</resultMap>
<select id="getDeptAndUserByDeptId" resultMap="DeptAndUserResultMap">
SELECT
u.user_id AS u_user_id,
u.user_name AS u_user_name,
u.email AS u_email,
u.dept_id,
d.dept_name
FROM
sys_dept d
LEFT JOIN sys_user u ON u.dept_id = d.dept_id
WHERE
d.dept_id = #{deptId}
</select>
<!-- collection-嵌套结果集 结束 -->
<!-- collection-分步结果集 开始 -->
<resultMap id="resultMap103" type="dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
<collection property="users"
ofType="user"
column="dept_id"
select="com.atguigu.mapper.UserMapper.getUserByDeptId"
>
</collection>
</resultMap>
<resultMap id="resultMap104" type="dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
</resultMap>
<resultMap id="DeptAndUserResultMapStep" type="dept">
<discriminator javaType="long" column="dept_id">
<case value="103" resultMap="resultMap103"/>
<case value="104" resultMap="resultMap104"/>
</discriminator>
</resultMap>
<select id="getDeptAndUserByDeptIdStep" resultMap="DeptAndUserResultMapStep">
select * from sys_dept where dept_id = #{deptId}
</select>
<!-- collection-分布结果集 结束 -->
</mapper>