1、数据库准备
CREATE TABLE `role` (
`ID` INT(11) NOT NULL COMMENT '编号',`ROLE_NAME` VARCHAR(30) DEFAULT NULL COMMENT '角色名称',
`ROLE_DESC` VARCHAR(60) DEFAULT NULL COMMENT '角色描述',
PRIMARY KEY (`ID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `role`(`ID`,`ROLE_NAME`,`ROLE_DESC`) VALUES (1,'院长','管理整个学院'),(2,'总裁','管理整个公司'),(3,'校长','管理整个学校');
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(32) NOT NULL COMMENT '用户名称',
`birthday` DATETIME DEFAULT NULL COMMENT '生日',
`sex` CHAR(1) DEFAULT NULL COMMENT '性别',
`address` VARCHAR(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`username`,`birthday`,`sex`,`address`) VALUES (41,'老王','2018-02-27 17:47:08','男','北京'),(42,'小二王','2018-03-02 15:09:37','女','北京金燕龙'),(43,'小二王','2018-03-04 11:34:34','女','北京金燕龙'),(45,'传智播客','2018-03-04 12:04:06','男','北京金燕龙'),(46,'老王','2018-03-07 17:37:26','男','北京'),(48,'小马宝莉','2018-03-08 11:44:00','女','北京修正');
2、创建一个Maven项目
在pom.xml引入约束:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.wzq</groupId>
<artifactId>MyBatis_day04_01_note_user_role_onetomany</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- mybatis的坐标 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2、创建数据库信息配置文件:jdbc.properties
jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
3、配置核心配置文件
<?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="jdbc.properties"></properties>
<!-- 别名配置 -->
<typeAliases>
<package name="cn.wzq.mybatis.domin"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" ></property>
<property name="url" value="${jdbc.url}" ></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<!-- sql映射配置 -->
<mappers>
<package name="cn.wzq.mybatis.dao"/>
</mappers>
</configuration>
4、创建user和role对象
public class User {
private Integer id;
private String username;
private String sex;
private String address;
private Date birthday;
private List<Role> list;
//get/set/toString省略;
}
public class Role {
private Integer id;
private String ROLE_NAME;
private String ROLE_DESC;
//get/set/toString省略;
}
5、创建IRoleDao接口
public interface IRoleDao {
//根据用户查询角色
@Select(value="select * from role where id = #{id}")
public Role findRole(int uid);
}
6、创建IUserDao接口
public interface IUserDao {
//查询用户下的所有角色
@Select(value="select * from user")
@Results(value= {
@Result(property="id",column="id",id=true),
@Result(property="username",column="username"),
@Result(property="sex",column="sex"),
@Result(property="address",column="address"),
@Result(property="birthday",column="birthday"),
@Result(property="list",javaType=List.class,column="id",
many=@Many(select="cn.wzq.mybatis.dao.IRoleDao.findRole",fetchType=FetchType.LAZY)
)
})
public List<User> findAll();
}
7、测试
//查询用户下所有角色
@Test
public void findAllTest() {
SqlSession openSession = sqlSessionFactory.openSession();
IUserDao userDao = openSession.getMapper(IUserDao.class);
List<User> list = userDao.findAll();
for (User user : list) {
System.out.println(user.getId());
//System.out.println(user.getList()); //需要使用时释放注解
}
}
整体目录结构