一.接口代理方式实现Dao
1.1 代理开发方式介绍
采用 Mybatis 的代理开发方式实现 DAO 层的开发,这种方式是我们后面进入企业的主流。
Mapper 接口开发方法只需要程序员编写Mapper 接口(相当于Dao 接口),由Mybatis 框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper 接口开发需要遵循以下规范:
1) Mapper.xml文件中的namespace与mapper接口的全限定名相同
2) Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3) Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4) Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
总结:
接口开发的方式: 程序员只需定义接口,就可以对数据库进行操作,那么具体的对象怎么创建?
1.程序员负责定义接口
2.在操作数据库,mybatis框架根据接口,通过动态代理的方式生成代理对象,负责数据库的crud操作
1.2.编写StudentMapper接口
代码实现
数据准备
USE `db1`;
/*Table structure for table `product` */
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`price` double DEFAULT NULL,
`brand` varchar(10) DEFAULT NULL,
`stock` int(11) DEFAULT NULL,
`insert_time` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `product` */
insert into `product`(`id`,`name`,`price`,`brand`,`stock`,`insert_time`) values (1,'华为手机',3999,'华为',23,'2088-03-10'),(2,'小米手机',2999,'小米',30,'2088-05-15'),(3,'苹果手机',5999,'苹果',18,'2088-08-20'),(4,'华为电脑',6999,'华为',14,'2088-06-16'),(5,'小米电脑',4999,'小米',26,'2088-07-08'),(6,'苹果电脑',8999,'苹果',15,'2088-10-25'),(7,'联想电脑',7999,'联想',NULL,'2088-11-11'),(8,'戴尔电脑',5655,'戴尔',16,NULL);
/*Table structure for table `student` */
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
/*Data for the table `student` */
insert into `student`(`id`,`NAME`,`age`) values (1,'张三',23),(2,'李四',24),(3,'王五',25),(4,'张三',23),(5,'李四',24),(6,'王五',25);
Student类
package com.shiyitiancheng.bean;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
mybatisConfig.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="jdbc-config.properties"/>
<!--配置数据环境(可以有多个),default指定使用-->
<environments default="development">
<!--具体配置数据库,id为唯一标识-->
<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>
<!--
resource属性:映射配置文件路径
-->
<mapper resource="com/shiyitiancheng/dao/StudentMapper.xml"/>
</mappers>
</configuration>
StudentMapper.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">
<!--
namespace属性:映射接口的全类名
-->
<mapper namespace="com.shiyitiancheng.dao.StudentMapper">
<!--
id属性:方法名(唯一)
resultType属性:结果类型的全类名
parameterType属性:参数类型的全类名
-->
<select id="selectAll" resultType="com.shiyitiancheng.bean.Student">
select * from student
</select>
</mapper>
StudentMapper接口
package com.shiyitiancheng.dao;
import com.shiyitiancheng.bean.Student;
import java.util.List;
public interface StudentMapper {
public abstract List<Student> selectAll();
}
1.3 测试代理方式
package com.shiyitiancheng.testfile;
import com.shiyitiancheng.bean.Student;
import com.shiyitiancheng.dao.StudentMapper;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Test01 {
@Test
public void selectAll() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentdao = sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentdao.selectAll();
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
is.close();
}
}
输出结果
Student{id=1, name='张三', age=23}
Student{id=2, name='李四', age=24}
Student{id=3, name='王五', age=25}
Student{id=4, name='张三', age=23}
Student{id=5, name='李四', age=24}
Student{id=6, name='王五', age=25}
1.4 源码分析
-
分析动态代理对象如何生成的?
通过动态代理开发模式,我们只编写一个接口,不写实现类,我们通过 getMapper() 方法最终获取到 org.apache.ibatis.binding.MapperProxy 代理对象,然后执行功能,而这个代理对象正是 MyBatis 使用了 JDK 的动态代理技术,帮助我们生成了代理实现类对象。从而可以进行相关持久化操作。
-
分析方法是如何执行的?
动态代理实现类对象在执行方法的时候最终调用了 mapperMethod.execute() 方法,这个方法中通过 switch 语句根据操作类型来判断是新增、修改、删除、查询操作,最后一步回到了 MyBatis 最原生的 SqlSession 方式来执行增删改查。
1.5 知识小结
接口代理方式可以让我们只编写接口即可,而实现类对象由 MyBatis 生成。
实现规则 :
- 映射配置文件中的名称空间必须和 Dao 层接口的全类名相同。
- 映射配置文件中的增删改查标签的 id 属性必须和 Dao 层接口的方法名相同。
- 映射配置文件中的增删改查标签的 parameterType 属性必须和 Dao 层接口方法的参数相同。
- 映射配置文件中的增删改查标签的 resultType 属性必须和 Dao 层接口方法的返回值相同。
- 获取动态代理对象 SqlSession 功能类中的 getMapper() 方法。
二. 动态sql语句
2.1 动态sql语句概述
Mybatis 的映射文件中,前面我们的 SQL 都是比较简单的,有些时候业务逻辑复杂时,我们的 SQL是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了。
2.2 动态 SQL 之<if>
我们根据实体类的不同取值,使用不同的 SQL语句来进行查询。比如在 id如果不为空时可以根据id查询,如果username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。
如下图:
<select id="findByCondition" resultType="com.shiyitiancheng.bean.Student">
select * from student
<where>
<if test="id!=null">
<!--第一个if中的and可以省略-->
id=#{id}
</if>
<if test="name!=null">
and name=#{name}
</if>
<if test="age!=null">
and age=#{age}
</if>
</where>
</select>
当查询条件都存在时,控制台打印的sql语句如下:
@Test
public void findByCondition() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentdao = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setId(3);
stu.setName("王五");
stu.setAge(25);
List<Student> students = studentdao.findByCondition(stu);
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
is.close();
}
DEBUG [main] - ==> Preparing: select * from student WHERE id=? and name=? and age=?
DEBUG [main] - ==> Parameters: 3(Integer), 王五(String), 25(Integer)
DEBUG [main] - <== Total: 1
Student{id=3, name='王五', age=25}
当查询条件只有id存在时,控制台打印的sql语句如下:
@Test
public void findByCondition() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentdao = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setId(3);
// stu.setName("王五");
// stu.setAge(25);
List<Student> students = studentdao.findByCondition(stu);
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
is.close();
}
DEBUG [main] - ==> Preparing: select * from student WHERE id=?
DEBUG [main] - ==> Parameters: 3(Integer)
DEBUG [main] - <== Total: 1
Student{id=3, name='王五', age=25}
总结语法:
<where>:条件标签。如果有动态条件,则使用该标签代替 where 关键字。
<if>:条件判断标签。
<if test=“条件判断”>
查询条件拼接
</if>
2.3 动态 SQL 之<foreach>
循环执行sql的拼接操作,例如:SELECT * FROM student WHERE id IN (1,2,5)。
<select id="findByIds" resultType="com.shiyitiancheng.bean.Student" parameterType="list">
select * from student
<where>
<foreach collection="list" open="id in (" close=")" item="id" separator="," >
#{id}
</foreach>
</where>
</select>
测试代码片段如下:
@Test
public void findByIds() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentdao = sqlSession.getMapper(StudentMapper.class);
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
List<Student> students = studentdao.findByIds(list);
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
is.close();
}
DEBUG [main] - ==> Preparing: select * from student WHERE id in ( ? , ? , ? )
DEBUG [main] - ==> Parameters: 1(Integer), 2(Integer), 3(Integer)
DEBUG [main] - <== Total: 3
Student{id=1, name='张三', age=23}
Student{id=2, name='李四', age=24}
Student{id=3, name='王五', age=25}
总结语法:
<!--<foreach>:循环遍历标签。适用于多个参数或者的关系。-->
<foreach collection="" open="" close="" item="" separator="">
获取参数
</foreach>
属性
collection:参数容器类型, (list-集合, array-数组)。
open:开始的 SQL 语句。
close:结束的 SQL 语句。
item:参数变量名。
separator:分隔符。
2.4 SQL片段抽取
Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的
<!--抽取sql片段简化编写-->
<sql id="selectStudent"> select * from student</sql>
<select id="selectAll" resultType="com.shiyitiancheng.bean.Student">
<include refid="selectStudent"/>
</select>
<select id="findByCondition" resultType="com.shiyitiancheng.bean.Student">
<include refid="selectStudent"/>
<where>
<if test="id!=null">
<!--第一个if中的and可以省略-->
id=#{id}
</if>
<if test="name!=null">
and name=#{name}
</if>
<if test="age!=null">
and age=#{age}
</if>
</where>
</select>
总结语法:
我们可以将一些重复性的 SQL 语句进行抽取,以达到复用的效果。
- <sql>:抽取 SQL 语句标签。
- <include>:引入 SQL 片段标签。
<sql id=“片段唯一标识”>抽取的 SQL 语句</sql> <include refid=“片段唯一标识”/>
2.5 知识小结
MyBatis映射文件配置:
<select>:查询
<insert>:插入
<update>:修改
<delete>:删除
<where>:where条件
<if>:if判断
<foreach>:循环
<sql>:sql片段抽取
三. 分页插件
3.1 分页插件介绍
- 分页可以将很多条结果进行分页显示。
- 如果当前在第一页,则没有上一页。如果当前在最后一页,则没有下一页。
- 需要明确当前是第几页,这一页中显示多少条结果。
- MyBatis分页插件总结
- 在企业级开发中,分页也是一种常见的技术。而目前使用的 MyBatis 是不带分页功能的,如果想实现分页的 功能,需要我们手动编写 LIMIT 语句。但是不同的数据库实现分页的 SQL 语句也是不同的,所以手写分页 成本较高。这个时候就可以借助分页插件来帮助我们实现分页功能。
- PageHelper:第三方分页助手。将复杂的分页操作进行封装,从而让分页功能变得非常简单。
3.2 分页插件的使用
MyBatis可以使用第三方的插件来对功能进行扩展,分页助手PageHelper是将分页的复杂操作进行封装,使用简单的方式即可获得分页的相关数据
开发步骤:
①导入与PageHelper的jar包
②在mybatis核心配置文件中配置PageHelper插件
<!--引入连接的配置文件-->
<properties resource="jdbc-config.properties"/>
<!-- 注意:分页助手的插件 配置在通用environments之前、reflectorFactory之后 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<!--配置数据环境(可以有多个),default指定使用-->
<environments default="development">
<!--具体配置数据库,id为唯一标识-->
<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>
<!--
resource属性:映射配置文件路径
-->
<mapper resource="com/shiyitiancheng/dao/StudentMapper.xml"/>
</mappers>
③测试分页数据获取
@Test
public void selectPaging() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentdao = sqlSession.getMapper(StudentMapper.class);
//通过分页助手来实现分页功能
// 第一页:显示3条数据
PageHelper.startPage(1,3);
List<Student> students = studentdao.selectAll();
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
is.close();
}
DEBUG [main] - ==> Preparing: select * from student LIMIT ?
DEBUG [main] - ==> Parameters: 3(Integer)
DEBUG [main] - <== Total: 3
Student{id=1, name='张三', age=23}
Student{id=2, name='李四', age=24}
Student{id=3, name='王五', age=25}
@Test
public void selectPaging() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentdao = sqlSession.getMapper(StudentMapper.class);
//通过分页助手来实现分页功能
// 第二页:显示3条数据
PageHelper.startPage(2,3);
List<Student> students = studentdao.selectAll();
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
is.close();
}
DEBUG [main] - ==> Preparing: select * from student LIMIT ?, ?
DEBUG [main] - ==> Parameters: 3(Integer), 3(Integer)
DEBUG [main] - <== Total: 3
Student{id=4, name='张三', age=23}
Student{id=5, name='李四', age=24}
Student{id=6, name='王五', age=25}
3.3 分页插件的参数获取
获得分页相关的其他参数:
//其他分页的数据
@Test
public void selectPaging() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentdao = sqlSession.getMapper(StudentMapper.class);
//通过分页助手来实现分页功能
// 第一页:显示3条数据
PageHelper.startPage(2,3);
List<Student> students = studentdao.selectAll();
//调用实现类的方法,接收结果
PageInfo<Student> info = new PageInfo<>(students);
for (Student student : students) {
System.out.println(student);
}
//获取分页相关参数
System.out.println("总条数:" + info.getTotal());
System.out.println("总页数:" + info.getPages());
System.out.println("当前页:" + info.getPageNum());
System.out.println("每页显示条数:" + info.getPageSize());
System.out.println("上一页:" + info.getPrePage());
System.out.println("下一页:" + info.getNextPage());
System.out.println("是否是第一页:" + info.isIsFirstPage());
System.out.println("是否是最后一页:" + info.isIsLastPage());
sqlSession.close();
is.close();
}
DEBUG [main] - ==> Preparing: select * from student LIMIT ?, ?
DEBUG [main] - ==> Parameters: 3(Integer), 3(Integer)
DEBUG [main] - <== Total: 3
Student{id=4, name='张三', age=23}
Student{id=5, name='李四', age=24}
Student{id=6, name='王五', age=25}
总条数:6
总页数:2
当前页:2
每页显示条数:3
上一页:1
下一页:0
是否是第一页:false
是否是最后一页:true
3.4 分页插件知识小结
分页:可以将很多条结果进行分页显示。
-
分页插件 jar 包: pagehelper-5.1.10.jar jsqlparser-3.1.jar
-
:集成插件标签。
-
分页助手相关 API
1.PageHelper:分页助手功能类。
- startPage():设置分页参数
- PageInfo:分页相关参数功能类。
- getTotal():获取总条数
- getPages():获取总页数
- getPageNum():获取当前页
- getPageSize():获取每页显示条数
- getPrePage():获取上一页
- getNextPage():获取下一页
- isIsFirstPage():获取是否是第一页
- isIsLastPage():获取是否是最后一页
四.MyBatis的多表操作
4.1 多表模型介绍
- 多表模型分类 一对一:在任意一方建立外键,关联对方的主键。
- 一对多:在多的一方建立外键,关联一的一方的主键。
- 多对多:借助中间表,中间表至少两个字段,分别关联两张表的主键。
4.2 多表模型一对一操作
-
一对一模型: 人和身份证,一个人只有一个身份证
-
代码实现
-
步骤一: sql语句准备
CREATE TABLE person( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT ); INSERT INTO person VALUES (NULL,'张三',23); INSERT INTO person VALUES (NULL,'李四',24); INSERT INTO person VALUES (NULL,'王五',25); CREATE TABLE card( id INT PRIMARY KEY AUTO_INCREMENT, number VARCHAR(30), pid INT, CONSTRAINT cp_fk FOREIGN KEY (pid) REFERENCES person(id) ); INSERT INTO card VALUES (NULL,'12345',1); INSERT INTO card VALUES (NULL,'23456',2); INSERT INTO card VALUES (NULL,'34567',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">
<!--
namespace属性:映射接口的全类名
-->
<mapper namespace="com.shiyitiancheng.dao.OneToOne">
<!--
id属性:方法名(唯一)
resultType属性:结果类型的全类名
parameterType属性:参数类型的全类名
-->
<resultMap id="oneToOne" type="card">
<id column="cid" property="id"></id>
<result column="number" property="number"></result>
<association property="p" javaType="person">
<id column="pid" property="id"></id>
<result column="name" property="name"></result>
<result column="age" property="age"></result>
</association>
</resultMap>
<select id="selectAll" resultMap="oneToOne">
select c.id cid,number,name,age from card c,person p where c.id = p.id
</select>
</mapper>
- 步骤三:测试类
@Test
public void selectO2O() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
OneToOne mapper = sqlSession.getMapper(OneToOne.class);
List<Card> cards = mapper.selectAll();
for (Card card : cards) {
System.out.println(card.getId()+","+card.getNumber()+","+card.getP().getName()+","+card.getP().getAge());
}
}
3.一对一配置总结:
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<association>:配置被包含对象的映射关系标签。
property 属性:被包含对象的变量名
javaType 属性:被包含对象的数据类型
4.3 多表模型一对多操作
-
一对多模型: 一对多模型:班级和学生,一个班级可以有多个学生。
-
代码实现
- 步骤一: sql语句准备
CREATE DATABASE db2;
USE db2;
CREATE TABLE person(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
age INT
);
INSERT INTO person VALUES (NULL,'张三',23);
INSERT INTO person VALUES (NULL,'李四',24);
INSERT INTO person VALUES (NULL,'王五',25);
CREATE TABLE card(
id INT PRIMARY KEY AUTO_INCREMENT,
number VARCHAR(30),
pid INT,
CONSTRAINT cp_fk FOREIGN KEY (pid) REFERENCES person(id)
);
INSERT INTO card VALUES (NULL,'12345',1);
INSERT INTO card VALUES (NULL,'23456',2);
INSERT INTO card VALUES (NULL,'34567',3);
CREATE TABLE classes(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
INSERT INTO classes VALUES (NULL,'一班');
INSERT INTO classes VALUES (NULL,'二班');
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(30),
age INT,
cid INT,
CONSTRAINT cs_fk FOREIGN KEY (cid) REFERENCES classes(id)
);
INSERT INTO student VALUES (NULL,'张三',23,1);
INSERT INTO student VALUES (NULL,'李四',24,1);
INSERT INTO student VALUES (NULL,'王五',25,2);
INSERT INTO student VALUES (NULL,'赵六',26,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">
<!--
namespace属性:映射接口的全类名
-->
<mapper namespace="com.shiyitiancheng.dao.OneToMany">
<!--
id属性:方法名(唯一)
resultType属性:结果类型的全类名
parameterType属性:参数类型的全类名
-->
<resultMap id="OTM" type="classes">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
<collection property="list" ofType="student">
<id column="sid" property="id"></id>
<result column="sname" property="name"></result>
<result column="sage" property="age"></result>
</collection>
</resultMap>
<select id="selectAll" resultMap="OTM">
SELECT c.id cid,c.name cname,s.id sid,s.name sname,s.age sage FROM classes c,student s WHERE c.id=s.cid
</select>
</mapper>
- 步骤三:测试类
@Test
public void selectO2M() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
OneToMany mapper = sqlSession.getMapper(OneToMany.class);
List<Classes> list = mapper.selectAll();
for (Classes classes : list) {
System.out.println(classes.getId()+","+classes.getName());
for (Student student : classes.getList()) {
System.out.println(classes.getId()+","+classes.getName()+","+student.getId()+","+student.getAge()+","+student.getName());
}
}
}
3.一对多配置文件总结:
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型
4.4 多表模型多对多操作
-
多对多模型:学生和课程,一个学生可以选择多门课程、一个课程也可以被多个学生所选择。
-
代码实现
CREATE TABLE course(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
INSERT INTO course VALUES (NULL,'语文');
INSERT INTO course VALUES (NULL,'数学');
CREATE TABLE stu_cr(
id INT PRIMARY KEY AUTO_INCREMENT,
sid INT,
cid INT,
CONSTRAINT sc_fk1 FOREIGN KEY (sid) REFERENCES student(id),
CONSTRAINT sc_fk2 FOREIGN KEY (cid) REFERENCES course(id)
);
INSERT INTO stu_cr VALUES (NULL,1,1);
INSERT INTO stu_cr VALUES (NULL,1,2);
INSERT INTO stu_cr VALUES (NULL,2,1);
INSERT INTO stu_cr VALUES (NULL,2,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">
<!--
namespace属性:映射接口的全类名
-->
<mapper namespace="com.shiyitiancheng.dao.ManyToMany">
<!--
id属性:方法名(唯一)
resultType属性:结果类型的全类名
parameterType属性:参数类型的全类名
-->
<resultMap id="M2M" type="student">
<id column="sid" property="id"></id>
<result column="sname" property="name"></result>
<result column="sage" property="age"></result>
<collection property="courses" ofType="course">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
</collection>
</resultMap>
<select id="selectAll" resultMap="M2M">
SELECT sc.sid,s.name sname,s.age sage,sc.cid,c.name cname FROM student s,course c,stu_cr sc WHERE sc.sid=s.id AND sc.cid=c.id
</select>
</mapper>
- 测试类
@Test
public void selectM2M() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
ManyToMany mapper = sqlSession.getMapper(ManyToMany.class);
List<Student> list = mapper.selectAll();
for (Student student : list) {
List<Course> courses = student.getCourses();
for (Course course : courses) {
System.out.println(student.getId()+","+student.getName()+","+student.getAge()+","+course.getId()+","+course.getName());
}
}
}
3.多对多配置文件总结:
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型
4.5 多表模型操作总结
<resultMap>:配置字段和对象属性的映射关系标签。
id 属性:唯一标识
type 属性:实体对象类型
<id>:配置主键映射关系标签。
<result>:配置非主键映射关系标签。
column 属性:表中字段名称
property 属性: 实体对象变量名称
<association>:配置被包含对象的映射关系标签。
property 属性:被包含对象的变量名
javaType 属性:被包含对象的数据类型
<collection>:配置被包含集合对象的映射关系标签。
property 属性:被包含集合对象的变量名
ofType 属性:集合中保存的对象数据类型