数据库准备
-- user 用户
create table dbuser(
id int unsigned auto_increment,
name varchar(20),
pass varchar(32),
primary key(id)
)engine=innodb charset=utf8;
insert into dbuser values(null,'admin','123');
insert into dbuser values(null,'aa','aa');
insert into dbuser values(null,'user','user');
select * from dbuser;
-- role 角色
create table dbrole(
id int unsigned auto_increment,
name varchar(20),
uid int unsigned,
primary key(id)
)engine=innodb charset=utf8;
drop table dbrole;
insert into dbrole values(null,'删除',1);
insert into dbrole values(null,'编辑',1);
insert into dbrole values(null,'修改',2);
insert into dbrole values(null,'审核',1);
insert into dbrole values(null,'打开',1);
insert into dbrole values(null,'关闭',2);
insert into dbrole values(null,'删除',2);
select * from
select * from dbrole;
User.java
@Data
public class User {
private int id;
private String name;
private String pass;
private List<Role> roles;
}
UserMapper.java
public interface UserMapper {
public User findById(int id);
}
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.fz.mapper.UserMapper">
<resultMap id="ur" type="user">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="pass" column="pass"/>
<collection property="roles" fetchType="eager" column="id" select="com.fz.mapper.RoleMapper.findByUserId"></collection>
</resultMap>
<select id="findById" parameterType="int" resultMap="ur">
select * from dbuser where id=#{id}
</select>
</mapper>
Role.java
@Data
public class Role {
private int id;
private String name;
}
RoleMapper.java
public interface RoleMapper {
public Role findByUserId(int id);
public Role findById(int id);
}
RoleMapper.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.fz.mapper.RoleMapper">
<resultMap id="rr" type="role">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<!--
<select id="findByUserId" parameterType="int" resultType="role">
select * from db_role where uid=#{id}
</select>
-->
<select id="findByUserId" parameterType="int" resultMap="rr">
select * from dbrole where uid=#{id}
</select>
<select id="findById" parameterType="int" resultMap="rr">
select * from dbrole where id=#{id}
</select>
</mapper>
测试代码
package com;
import com.fz.entity.User;
import com.fz.mapper.RoleMapper;
import com.fz.mapper.UserMapper;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
/**
* Created by webrx on 2017-06-16.
*/
public class Demo {
protected SqlSessionFactory sf;
protected SqlSession ss;
protected UserMapper udao;
protected RoleMapper rdao;
@Before
public void init(){
try {
this.sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
this.ss = this.sf.openSession();
this.udao = this.ss.getMapper(UserMapper.class);
this.rdao = this.ss.getMapper(RoleMapper.class);
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void close(){
this.ss.commit();
this.ss.close();
}
@Test
public void one2many(){
User u = udao.findById(2);
System.out.println(u.getName());
//System.out.println(u.getRoles());
}
}
一对多关系 默认延迟加载
<!-- 全局配置参数 -->
<settings>
<!-- 延迟加载总开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 设置按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="logPrefix" value="dao."/>
</settings>
如果要使用立即加载
UserMapper.xml fetchType="eager" 立即
fetchType="lazy" 延迟
<resultMap id="ur" type="user">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="pass" column="pass"/>
<collection property="roles" fetchType="eager" column="id" select="com.fz.mapper.RoleMapper.findByUserId"></collection>
</resultMap>
<select id="findById" parameterType="int" resultMap="ur">
select * from dbuser where id=#{id}
</select>