MyBatis——(二)一对多 多对一 多对多

2018-11-27 20:53:00     

一、摘要

表和表之间的关系,常见的有一对多,多对一,多对多。比如教室表(classroom)和学生表(student)是有关系的,那么如何分析他们之间的关系呢?

  • 一个学生(student)只能对应一间教室(classroom)
  • 一间教室(classroom)可以对应多个学生(student)
所以教室(classroom)对学生(student)就是一对多,学生(student)对教室(classroom)就是多对一

引入一个老师表(teacher),那老师表(teacher)和学生表(student)的关系是怎样的呢?

  • 一个老师(teacher)可以对应多个学生(student)
  • 一个学生(student)可以对应多个老师(teacher)
所以学生(student)对老师(teacher)就是多对多

二、一对多

首先创建教室表(classroom)并插入两条数据模拟两间教室,如下代码:

CREATE TABLE `classroom` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;


INSERT INTO `classroom` VALUES ('1', '1号教室');
INSERT INTO `classroom` VALUES ('2', '2号教室');

创建学生表(student)并插入四条数据模拟四个学生,如下代码:

CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `cid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;


INSERT INTO `student` VALUES ('1', 'Bounds', '1');
INSERT INTO `student` VALUES ('2', 'Cubi', '2');
INSERT INTO `student` VALUES ('3', 'Peter', '1');
INSERT INTO `student` VALUES ('4', 'Louis', '2');

创建Java Project项目并且导入相关jar包,在【src/dao】目录下新建Student实体类,代码如下:

package dao;

public class Student {

	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}

}

在【src/dao】目录下新建ClassRoom实体类,代码如下:

package dao;

import java.util.List;

public class ClassRoom {
	private int id;
	private String name;
	private List<Student> student;
	

	public List<Student> getStudent() {
		return student;
	}

	public void setStudent(List<Student> student) {
		this.student = student;
	}

	@Override
	public String toString() {
		return "ClassRoom [id=" + id + ", name=" + name + "]";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

在【src/config】目录下新建db.properties件用于存放连接数据库的账号密码等信息,代码如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/transaction
username=root
password=123456

在【src/config】目录下新建mybatis配置文件mybatis-config.xml其作用主要是提供连接数据库用的驱动,数据库名称,编码方式,账号密码,加载sql,代码如下:

<?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文件 -->
	<properties resource="config/db.properties" />
	<!-- 自动扫描dao包下的类型,使得在后续配置文件Teacher.xml中使用resultType的时候 
	可以直接使用Teacher,而不必写全dao.Teacher -->
	<typeAliases>
		<package name="dao" />
	</typeAliases>
	<!--和Spring整合后 environments 配置将废除 -->
	<environments default="development">
		<environment id="development">
			<!-- 使用JDBC事务管理 -->
			<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>
	<!-- 映射teacher.xml加载sql语句 -->
	<mappers>
		<mapper resource="config/ClassRoom.xml" />
	</mappers>
</configuration>

在【src/config】目录下新建ClassRoom.xml专门用于存放sql映射文件,代码如下:

<?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">

<!-- 表示命名空间是config,在后续调用sql语句的时候,会用到它 -->
<mapper namespace="config">
	<!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
	<resultMap id="classRoomBean" type="ClassRoom">
		<!-- id标签代表这个表的主键,取主键别名 -->
		<id column="cid" property="id" />
		<!-- result标签代表这个表的非主键字段,取别名 -->
		<result column="cname" property="name" />
		<!-- 一对多的关系 -->
		<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
		<collection property="student" ofType="Student">
			<id column="sid" property="id" />
			<result column="sname" property="name" />
		</collection>
	</resultMap>
	<!-- 通过left join on关联查询,对classroom和student表进行关联查询 -->
	<select id="listClassRoom" resultMap="classRoomBean">
		select c.*, s.*, c.id
		'cid', c.name 'cname', s.id 'sid', s.name 'sname' from classroom c
		left join student s on c.id = s.cid
	</select>
</mapper>

在【src/test】目录下新建TestMybatis测试类,执行定义的select语句,如下代码:

// 查询所有
@Test
public void testOne() throws IOException {
	// 获取配置文件
	String resource = "config/mybatis-config.xml";
	// 利用一个叫Resources的类的getResourceAsStream方法从配置文件xml中获取里面的内容资源
	InputStream is = Resources.getResourceAsStream(resource);
	// 然后用SqlSessionFactoryBuilder工厂构建者把获取的资源传进去就构建出了我们需要使用的工厂
	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	// 工厂生产产品
	SqlSession session = sessionFactory.openSession();
	// 产品实现相应的功能(listTeacher这个就是在配置文件teacher.xml中那条sql语句设置的id)
	List<ClassRoom> list = session.selectList("listClassRoom");
	for (ClassRoom t : list) {
		System.out.println(t);
		List<Student> student = t.getStudent();
		for (Student s : student) {
			System.out.println("\t" + s);
		}
	}
	session.commit();
	// 使用session执行完SQL之后需要关闭session
	session.close();
}

执行结果:

16:25:38.217 [main] DEBUG config.listClassRoom - ==>  Preparing: select c.*, s.*, c.id 'cid', c.name 'cname', s.id 'sid', s.name 'sname' from classroom c left join student s on c.id = s.cid 
16:25:38.231 [main] DEBUG config.listClassRoom - ==> Parameters: 
16:25:38.251 [main] DEBUG config.listClassRoom - <==      Total: 4
ClassRoom [id=1, name=1号教室]
	Student [id=1, name=Bounds]
	Student [id=3, name=Peter]
ClassRoom [id=2, name=2号教室]
	Student [id=2, name=Cubi]
	Student [id=4, name=Louis]

三、多对一

修改Student实体类,加入ClassRoom对象,如下代码:

private ClassRoom classRoom;

public ClassRoom getClassRoom() {
	return classRoom;
}

public void setClassRoom(ClassRoom classRoom) {
	this.classRoom = classRoom;
}

在【src/config】目录下新建Student.xml配置文件专门用于存放sql映射文件,代码如下:

<?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">

<!-- 表示命名空间是config,在后续调用sql语句的时候,会用到它 -->
<mapper namespace="config">
	<!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
	<resultMap id="studentBean" type="Student">
		<!-- id标签代表这个表的主键,取主键别名 -->
		<id column="sid" property="id" />
		<!-- result标签代表这个表的非主键字段,取别名 -->
		<result column="sname" property="name" />
		<!-- 多对一的关系 -->
		<!-- property: 指的是集合属性的值, javaType:指的是集合中元素的类型 -->
		<association property="classRoom" javaType="ClassRoom">
			<id column="cid" property="id" />
			<result column="cname" property="name" />
		</association>
	</resultMap>
	<!-- 通过left join on关联查询,对classroom和student表进行关联查询 -->
	<select id="listStudent" resultMap="studentBean">
		select c.id 'cid', c.name
		'cname', s.id 'sid', s.name 'sname', s.cid 'scid' from classroom c
		left join student s on c.id = s.cid
	</select>
</mapper>

修改mybatis-config.xml配置文件,引入Student.xml配置文件,如下代码:

<mapper resource="config/Student.xml" />

TestMybatis测试类:

// 查询所有
@Test
public void testTwo() throws IOException {
	// 获取配置文件
	String resource = "config/mybatis-config.xml";
	// 利用一个叫Resources的类的getResourceAsStream方法从配置文件xml中获取里面的内容资源
	InputStream is = Resources.getResourceAsStream(resource);
	// 然后用SqlSessionFactoryBuilder工厂构建者把获取的资源传进去就构建出了我们需要使用的工厂
	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	// 工厂生产产品
	SqlSession session = sessionFactory.openSession();
	// 产品实现相应的功能(listTeacher这个就是在配置文件teacher.xml中那条sql语句设置的id)
	List<Student> list = session.selectList("listStudent");
	for (Student t : list) {
		System.out.println(t);
		System.out.println("\t" + t.getClassRoom());
	}
	session.commit();
	// 使用session执行完SQL之后需要关闭session
	session.close();
}

执行结果:

16:34:52.917 [main] DEBUG config.listStudent - ==>  Preparing: select c.id 'cid', c.name 'cname', s.id 'sid', s.name 'sname', s.cid 'scid' from classroom c left join student s on c.id = s.cid 
16:34:52.931 [main] DEBUG config.listStudent - ==> Parameters: 
16:34:52.942 [main] DEBUG config.listStudent - <==      Total: 4
Student [id=1, name=Bounds]
	ClassRoom [id=1, name=1号教室]
Student [id=3, name=Peter]
	ClassRoom [id=1, name=1号教室]
Student [id=2, name=Cubi]
	ClassRoom [id=2, name=2号教室]
Student [id=4, name=Louis]
	ClassRoom [id=2, name=2号教室]

四、多对多

新建一张老师表(teacher)

CREATE TABLE `teacher` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;


INSERT INTO `teacher` VALUES ('1', '数学老师');
INSERT INTO `teacher` VALUES ('2', '语文老师');

为了维持多对多的关系,必须要有一个中间表,这里设置一个中间表(tsbeen)

  • 插入6条中间表数据,建立如下关系
  • 编号为1的数学老师对应编号为 1,2,3的学生
  • 编号为2的语文老师对应编号为 2,3,4的学生
CREATE TABLE `tsbeen` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `number` int(11) NOT NULL,
  `tid` int(11) NOT NULL,
  `sid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;


INSERT INTO `tsbeen` VALUES ('1', '10', '1', '1');
INSERT INTO `tsbeen` VALUES ('2', '15', '1', '2');
INSERT INTO `tsbeen` VALUES ('3', '20', '1', '3');
INSERT INTO `tsbeen` VALUES ('4', '25', '2', '2');
INSERT INTO `tsbeen` VALUES ('5', '30', '2', '3');
INSERT INTO `tsbeen` VALUES ('6', '35', '2', '4');

新建Teacher实体类,如下代码:

package dao;

import java.util.List;

public class Teacher {

	private int id;
	private String name;
	private List<Tsbeen> tsbeen;

	public List<Tsbeen> getTsbeen() {
		return tsbeen;
	}

	public void setTsbeen(List<Tsbeen> tsbeen) {
		this.tsbeen = tsbeen;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

新建Tsbeen实体类,如下代码:

package dao;

public class Tsbeen {
	private int id;
	private int number;
	private Teacher teacher;
	private Student student;
	

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

}

在【src/config】目录下新建Teacher.xml配置文件专门用于存放sql映射文件,代码如下:

<?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">

<!-- 表示命名空间是config,在后续调用sql语句的时候,会用到它 -->
<mapper namespace="config">
	<!-- 使用resultMap映射实体类和字段之间的一一对应关系 -->
	<resultMap id="teacherBean" type="Teacher">
		<!-- id标签代表这个表的主键,取主键别名 -->
		<id column="tid" property="id" />
		<!-- result标签代表这个表的非主键字段,取别名 -->
		<result column="tname" property="name" />
		<!-- 一对多的关系 -->
		<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
		<collection property="tsbeen" ofType="Tsbeen">
			<id column="tsid" property="id" />
			<result column="number" property="number" />
			<association property="student" javaType="Student">
				<id column="sid" property="id" />
				<result column="sname" property="name" />
			</association>
		</collection>
	</resultMap>
	<!-- 通过left join on关联查询,对teacher和student表进行关联查询 -->
	<select id="listTeacher" resultMap="teacherBean">
		select t.*, ts.*, s.*, t.id
		'tid', t.name 'tname', ts.id 'tsid', s.id 'sid', s.name 'sname' from
		teacher t left join tsbeen ts on t.id = ts.tid left join student s on
		s.id = ts.sid
	</select>
	<select id="getTeacher" resultMap="teacherBean">
		select t.*, ts.*, s.*, t.id
		'tid', t.name 'tname', ts.id 'tsid', s.id 'sid', s.name 'sname' from
		teacher t left join tsbeen ts on t.id = ts.tid left join student s on
		s.id = ts.sid where t.id = #{id}
	</select>
</mapper>

TestMybatis测试类:

// 查询所有
@Test
public void testThree() throws IOException {
	// 获取配置文件
	String resource = "config/mybatis-config.xml";
	// 利用一个叫Resources的类的getResourceAsStream方法从配置文件xml中获取里面的内容资源
	InputStream is = Resources.getResourceAsStream(resource);
	// 然后用SqlSessionFactoryBuilder工厂构建者把获取的资源传进去就构建出了我们需要使用的工厂
	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	// 工厂生产产品
	SqlSession session = sessionFactory.openSession();
	// 产品实现相应的功能(listTeacher这个就是在配置文件teacher.xml中那条sql语句设置的id)
	List<Teacher> list = session.selectList("listTeacher");
	for (Teacher t : list) {
		System.out.println(t.getName());
		List<Tsbeen> tsbeen = t.getTsbeen();
		//遍历每个老师底下的多个学生
		for (Tsbeen ts : tsbeen) {
			System.out.println("\t"+ts.getNumber()+",,,"+ts.getStudent().getId()+",,,"+ts.getStudent().getName());
		}
	}
	session.commit();
	// 使用session执行完SQL之后需要关闭session
	session.close();
}

执行结果:

18:48:06.660 [main] DEBUG config.listTeacher - ==>  Preparing: select t.*, ts.*, s.*, t.id 'tid', t.name 'tname', ts.id 'tsid', s.id 'sid', s.name 'sname' from teacher t left join tsbeen ts on t.id = ts.tid left join student s on s.id = ts.sid 
18:48:06.674 [main] DEBUG config.listTeacher - ==> Parameters: 
18:48:06.686 [main] DEBUG config.listTeacher - <==      Total: 6
数学老师
	10,,,1,,,Bounds
	15,,,2,,,Cubi
	20,,,3,,,Peter
语文老师
	25,,,2,,,Cubi
	30,,,3,,,Peter
	35,,,4,,,Louis

插入一条中间表(tsbeen)数据

在【src/config】目录下新建Tsbeen.xml配置文件专门用于存放sql映射文件,代码如下:

<?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">

<!-- 表示命名空间是config,在后续调用sql语句的时候,会用到它 -->
<mapper namespace="config">
	<insert id="addTsbeen" parameterType="Tsbeen">
		insert into tsbeen values(null,#{number},#{teacher.id},#{student.id})
	</insert>
</mapper>

Teacher.xml配置文件新增一条sql,通过老师表(teacher)中间表(tsbeen)学生表(student)左关联查出一条数据,如下代码:

<select id="getTeacher" resultMap="teacherBean">
	select t.*, ts.*, s.*, t.id
	'tid', t.name 'tname', ts.id 'tsid', s.id 'sid', s.name 'sname' from
	teacher t left join tsbeen ts on t.id = ts.tid left join student s on
	s.id = ts.sid where t.id = #{id}
</select>

Student.xml配置文件新增一条sql,通过教室表(classroom)学生表(student)左关联查出一条数据,如下代码:

<select id="getStudent" resultMap="studentBean">
	select c.id 'cid', c.name
	'cname', s.id 'sid', s.name 'sname', s.cid 'scid' from classroom c
	left join student s on c.id = s.cid where s.id = #{id}
</select>

mybatis-config.xml文件引入Tsbeen.xml配置文件:

<mapper resource="config/Tsbeen.xml" />

TestMybatis测试类:

// 增加一条中间表数据
@Test
public void testFour() throws IOException {
	// 获取配置文件
	String resource = "config/mybatis-config.xml";
	// 利用一个叫Resources的类的getResourceAsStream方法从配置文件xml中获取里面的内容资源
	InputStream is = Resources.getResourceAsStream(resource);
	// 然后用SqlSessionFactoryBuilder工厂构建者把获取的资源传进去就构建出了我们需要使用的工厂
	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	// 工厂生产产品
	SqlSession session = sessionFactory.openSession();
	// 产品实现相应的功能(listTeacher这个就是在配置文件teacher.xml中那条sql语句设置的id)
	Teacher teacher = session.selectOne("getTeacher", 1);
	Student student = session.selectOne("getStudent", 4);
	Tsbeen tsbeen = new Tsbeen();
	tsbeen.setStudent(student);
	tsbeen.setTeacher(teacher);
	tsbeen.setNumber(50);
	session.insert("addTsbeen", tsbeen);
	session.commit();
	// 使用session执行完SQL之后需要关闭session
	session.close();
}

执行结果:

10:02:17.223 [main] DEBUG config.getTeacher - ==>  Preparing: select t.*, ts.*, s.*, t.id 'tid', t.name 'tname', ts.id 'tsid', s.id 'sid', s.name 'sname' from teacher t left join tsbeen ts on t.id = ts.tid left join student s on s.id = ts.sid where t.id = ? 
10:02:17.237 [main] DEBUG config.getTeacher - ==> Parameters: 1(Integer)
10:02:17.249 [main] DEBUG config.getTeacher - <==      Total: 3
10:02:17.249 [main] DEBUG config.getStudent - ==>  Preparing: select c.id 'cid', c.name 'cname', s.id 'sid', s.name 'sname', s.cid 'scid' from classroom c left join student s on c.id = s.cid where s.id = ? 
10:02:17.249 [main] DEBUG config.getStudent - ==> Parameters: 4(Integer)
10:02:17.250 [main] DEBUG config.getStudent - <==      Total: 1
10:02:17.250 [main] DEBUG config.addTsbeen - ==>  Preparing: insert into tsbeen values(null,?,?,?) 
10:02:17.251 [main] DEBUG config.addTsbeen - ==> Parameters: 50(Integer), 1(Integer), 4(Integer)
10:02:17.251 [main] DEBUG config.addTsbeen - <==    Updates: 1

删除一条中间表(tsbeen)数据

Tsbeen.xml配置文件新增一条sql,用于删除中间表(tsbeen)数据:

<delete id="deleteTsbeen" parameterType="Tsbeen">
	delete from tsbeen where tid = #{teacher.id} and sid = #{student.id}
</delete>

TestMybatis测试类:

// 删除一条中间表数据
@Test
public void testFive() throws IOException {
	// 获取配置文件
	String resource = "config/mybatis-config.xml";
	// 利用一个叫Resources的类的getResourceAsStream方法从配置文件xml中获取里面的内容资源
	InputStream is = Resources.getResourceAsStream(resource);
	// 然后用SqlSessionFactoryBuilder工厂构建者把获取的资源传进去就构建出了我们需要使用的工厂
	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
	// 工厂生产产品
	SqlSession session = sessionFactory.openSession();
	// 产品实现相应的功能(listTeacher这个就是在配置文件teacher.xml中那条sql语句设置的id)
	Teacher teacher = session.selectOne("getTeacher", 1);
	Student student = session.selectOne("getStudent", 4);
	Tsbeen tsbeen = new Tsbeen();
	tsbeen.setStudent(student);
	tsbeen.setTeacher(teacher);
	session.delete("deleteTsbeen", tsbeen);
	session.commit();
	// 使用session执行完SQL之后需要关闭session
	session.close();
}

执行结果:

10:47:39.988 [main] DEBUG config.getTeacher - ==>  Preparing: select t.*, ts.*, s.*, t.id 'tid', t.name 'tname', ts.id 'tsid', s.id 'sid', s.name 'sname' from teacher t left join tsbeen ts on t.id = ts.tid left join student s on s.id = ts.sid where t.id = ? 
10:47:40.004 [main] DEBUG config.getTeacher - ==> Parameters: 1(Integer)
10:47:40.015 [main] DEBUG config.getTeacher - <==      Total: 4
10:47:40.016 [main] DEBUG config.getStudent - ==>  Preparing: select c.id 'cid', c.name 'cname', s.id 'sid', s.name 'sname', s.cid 'scid' from classroom c left join student s on c.id = s.cid where s.id = ? 
10:47:40.016 [main] DEBUG config.getStudent - ==> Parameters: 4(Integer)
10:47:40.017 [main] DEBUG config.getStudent - <==      Total: 1
10:47:40.017 [main] DEBUG config.deleteTsbeen - ==>  Preparing: delete from tsbeen where tid = ? and sid = ? 
10:47:40.018 [main] DEBUG config.deleteTsbeen - ==> Parameters: 1(Integer), 4(Integer)
10:47:40.018 [main] DEBUG config.deleteTsbeen - <==    Updates: 1

多对多不存在修改关系的做法,就是删除旧的,然后新增一条即达到修改的效果

ps:本篇博客源码下载链接:https://pan.baidu.com/s/1zyKxHvEyNlJlCX2PITzniw密码:itji
Spring思维导图

Spring如此博大精深,这里没有涉及到原理的东西。只是根据Spring的特性、核心容器、常用注解这些模块简单的介绍一下Spring。通过思维导图分析各个模块的知识点,可以随时开展头脑风暴,帮助我们快速理清思路并提高效率学习效率。

MyBatis——(三)动态SQL

前几篇博客介绍的sql语句都是较简单的单条件查询语句。要是遇到复杂的多条件,往往需要拼接,而拼接 SQL ,稍微不注意,由于引号,空格等缺失可能都会导致错误。 那么怎么去解决这个问题呢?这就是本篇所讲的使用 mybatis 动态SQL,通过 if, where, set, trim, when, choose, otherwise, foreach,bind等标签,可组合成非常灵活的SQL语句,从而在提高 SQL 语句的准确性的同时,也大大提高了开发人员的效率。

 发表评论