动态SQL语句-更复杂的查询业务需求
- 官方文档
- 基本介绍
- 映射方式
- 配置Mapper.xml的方式-应用实例
- 注解的方式实现-应用实例
- 课后练习
官方文档
文档地址: https://mybatis.org/mybatis-3/zh_CN/sqlmap-xml.html
基本介绍
●基本介绍
1.项目中多对1
的关系是一个基本的映射关系, 也可以理解成1
对多
2.User --- Pet:
一个用户可以养多只宠物
3.Dep --- Emp:
一个部门可以有多个员工
●注意细节
1.我们直接讲 双向的多对一的关系, 单向的多对一比双向的多对一简单.
2.在实际的项目开发中, 要求会使用双向的多对一的映射关系
3.说明: 什么是双向的多对一的关系: 比如通过User
可以查询到对应的Pet
, 返回来, 通过Pet
也可以级联查询到对应的User
信息.
4.多对多的关系, 是在多对1
的基础上扩展即可.
映射方式
1.方式1:
通过配置XxxMapper.xml
实现多对1
[配置方式]
2.方式2:
通过注解的方式实现 多对1
[注解方式]
3.我们都实现代码, 应用举例.
配置Mapper.xml的方式-应用实例
●需求说明: 实现级联查询, 通过user
的id
可以查询到用户信息, 并可以查询到关联的pet
信息. 反过来, 通过Pet
的id
可以查询到Pet
的信息, 并且可以级联查询到它的主人User
对象信息
1.创建user
表和pet
表
USE mybatis;CREATE TABLE `mybatis_user` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL DEFAULT ''
)CHARSET=utf8CREATE TABLE `mybatis_pet` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`nickname` VARCHAR(32) NOT NULL DEFAULT '',
`user_id` INT,
FOREIGN KEY(`user_id`) REFERENCES `mybatis_user`(`id`)
)CHARSET=utf8INSERT INTO `mybatis_user` VALUES (NULL, '宋江'), (NULL, '卢俊义');
INSERT INTO `mybatis_pet` VALUES (NULL, '波斯猫', 1), (NULL, '旺财', 1);
INSERT INTO `mybatis_pet` VALUES (NULL, '斑点猫', 2), (NULL, '花猫', 2);SELECT * FROM `mybatis_user`;
SELECT * FROM `mybatis_pet`;
2.创建com.zzw.entity.Pet
和 com.zzw.entity.User
. 这里toString
方法有问题, 后面揭晓
@Getter
@Setter
public class Pet {/*** CREATE TABLE `mybatis_pet` (* `id` INT PRIMARY KEY AUTO_INCREMENT,* `nickname` VARCHAR(32) NOT NULL DEFAULT '',* `user_id` INT,* FOREIGN KEY(`user_id`) REFERENCES `mybatis_user`(`id`)* )CHARSET=utf8*/private Integer id;private String nickname;//一个pet对应一个主人 User对象private User user;//toString会造成StackOverFlow//@Override//public String toString() {// return "Pet{" +// "id=" + id +// ", nickname='" + nickname + '\'' +// ", user=" + user +// '}';//}
}
@Getter
@Setter
public class User {/*** CREATE TABLE `mybatis_user` (* `id` INT PRIMARY KEY AUTO_INCREMENT,* `name` VARCHAR(32) NOT NULL DEFAULT ''* )CHARSET=utf8*/private Integer id;private String name;//因为一个user可以养多个宠物, mybatis 使用集合体现体现这个关系private List<Pet> pets;//toString会带来麻烦?=>会造成StackOverFlow//@Override//public String toString() {// return "User{" +// "id=" + id +// ", name='" + name + '\'' +// ", pets=" + pets +// '}';//}
}
3.创建UserMapper
和 PetMapper
public interface UserMapper {//通过id获取User对象public User getUserById(Integer id);
}
public interface PetMapper {//通过User的id来获取pet对象, 可能有多个, 因此使用List接收public List<Pet> getPetByUserId(Integer userId);//通过pet的id获取pet对象, 同时会查询到pet对象关联的user对象public Pet getPetById(Integer id);
}
4.创建PetMapper.xml
和 UserMapper.xml
<mapper namespace="com.zzw.mapper.PetMapper"><!--1.配置/实现public List<Pet> getPetByUserId(Integer userId);2.通过User的id来获取pet对象, 可能有多个, 因此使用List接收3.完成的思路和前面大体相同--><resultMap id="petResultMap" type="Pet"><id property="id" column="id"/><result property="nickname" column="nickname"/><association property="user" column="user_id"select="com.zzw.mapper.UserMapper.getUserById"/><!--先写完, 再去实现getUserById方法--></resultMap><select id="getPetByUserId" parameterType="Integer"resultMap="petResultMap">SELECT * FROM `mybatis_pet` WHERE `user_id` = #{userId}</select>
</mapper>
<mapper namespace="com.zzw.mapper.UserMapper"><!--解读1.想一想前面的1对1怎么实现的2.配置/实现public User getUserById(Integer id);3.通过id获取User对象4.思路(1) 先通过user-id 查询得到用户信息 (2) 再根据user-id查询对应的pet信息并映射到List<Pet> pets--><resultMap id="userResultMap" type="User"><id property="id" column="id"/><result property="name" column="name"/><!--解读: 因为pets属性是集合, 因此这里需要使用collection标签来处理1.ofType="Pet" 指定返回的集合中存放的数据类型Pet2.collection 表示 pets 是一个集合3.property="pets" 是返回的user对象的属性 pets4.column="id" select * from `mybatis_user` where `id` = #{id} 返回的id字段对应的值--><collection property="pets" column="id" ofType="Pet"select="com.zzw.mapper.PetMapper.getPetByUserId"/></resultMap><select id="getUserById" parameterType="Integer"resultMap="userResultMap">select * from `mybatis_user` where `id` = #{id}</select>
</mapper>
5.新建UserMapperTest
和 PetMapperTest
public class UserMapperTest {//属性private SqlSession sqlSession;private UserMapper userMapper;//初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();userMapper = sqlSession.getMapper(UserMapper.class);System.out.println("userMapper--" + userMapper);}@Testpublic void getUserById() {User user = userMapper.getUserById(2);System.out.println("user信息--" + user.getId() + "-" + user.getName());List<Pet> pets = user.getPets();for (Pet pet : pets) {System.out.println("养的宠物信息-" + pet.getId() + "-" + pet.getNickname());}if (sqlSession != null) {sqlSession.close();}}
}
public class PetMapperTest {//属性private SqlSession sqlSession;private PetMapper petMapper;//初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();petMapper = sqlSession.getMapper(PetMapper.class);System.out.println("petMapper--" + petMapper.getClass());}@Testpublic void getPetByUserId() {List<Pet> pets = petMapper.getPetByUserId(1);for (Pet pet : pets) {System.out.println("pet信息-" + pet.getId() + "-" + pet.getNickname());User user = pet.getUser();System.out.println("user信息-" + user.getName());}if (sqlSession != null) {sqlSession.close();}}
}
6.实现getPetById
, 体会复用
<!--说明
1. 注意体会resultMap带来好处, 直接复用
2. 配置/实现public Pet getPetById(Integer id);
3. 通过pet的id获取pet对象
-->
<select id="getPetById" parameterType="Integer"resultMap="petResultMap">SELECT * FROM `mybatis_pet` where `id` = #{id};
</select>
@Test
public void getPetById() {Pet pet = petMapper.getPetById(4);System.out.println("pet信息-" + pet.getId() + "-" + pet.getNickname());User user = pet.getUser();System.out.println("主人信息-" + user.getId() + "-" + user.getName());if (sqlSession != null) {sqlSession.close();}
}
注解的方式实现-应用实例
●需求说明: 通过注解的方式来实现下面的多对1
的映射关系, 实现级联查询, 通过user
的id
可以查询到用户信息, 并可以查询到关联的pet
信息. 反过来, 通过Pet
的id
可以查询到Pet
的信息, 并且可以级联查询到它的主人User
对象信息
说明: 在实际开发中还是 推荐使用配置方式
1.新建com.zzw.mapper.UserMapperAnnotation
接口
/*** @author 赵志伟* @version 1.0* UserMapperAnnotation: 使用注解的方式实现多对一*/
public interface UserMapperAnnotation {/*** 1.说明: 注解的形式就是前面xml配置方式的体现* 2.这里同学们可以结合前面xml实现* <resultMap id="userResultMap" type="User">* <id property="id" column="id"/>* <result property="name" column="name"/>* <collection property="pets" column="id" ofType="Pet"* select="com.zzw.mapper.PetMapper.getPetByUserId"/>* </resultMap>* <select id="getUserById" parameterType="Integer"* resultMap="userResultMap">* select * from `mybatis_user` where `id` = #{id}* </select>*/@Select("select * from `mybatis_user` where `id` = #{id}")@Results({@Result(id = true, property = "id", column = "id"),@Result(property = "name", column = "name"),//这里请小伙伴注意, pets属性对应的是集合@Result(property = "pets", column = "id",many = @Many(select = "com.zzw.mapper.PetMapperAnnotation.getPetByUserId"))})public User getUserById(Integer id);
}
2.新建com.zzw.mapper.PetMapperAnnotation
接口
public interface PetMapperAnnotation {/*** <resultMap id="petResultMap" type="Pet">* <id property="id" column="id"/>* <result property="nickname" column="nickname"/>* <association property="user" column="user_id"* select="com.zzw.mapper.UserMapper.getUserById"/>* </resultMap>* <select id="getPetByUserId" parameterType="Integer"* resultMap="petResultMap">* SELECT * FROM `mybatis_pet` WHERE `user_id` = #{userId}* </select>*///id = "petResultMap" 就是给我们的Results[Result Map] 指定一个名字//, 目的是为了后面复用@Select("SELECT * FROM `mybatis_pet` WHERE `user_id` = #{userId}")@Results(id = "petResultMap", value = {@Result(id = true, property = "id", column = "id"),@Result(property = "nickname", column = "nickname"),@Result(property = "user", column="user_id",one = @One(select = "com.zzw.mapper.UserMapperAnnotation.getUserById"))})public List<Pet> getPetByUserId(Integer userId);/*** <select id="getPetById" parameterType="Integer"* resultMap="petResultMap">* SELECT * FROM `mybatis_pet` where `id` = #{id}* </select>** @ResultMap("petResultMap") 使用/引用我们上面定义的 Results[ResultMap]*/@Select("SELECT * FROM `mybatis_pet` where `id` = #{id}")@ResultMap("petResultMap")public Pet getPetById(Integer id);
}
3.新建 com.zzw.mapper.UserMapperAnnotationTest
和 com.zzw.mapper.PetMapperAnnotationTest
public class UserMapperAnnotationTest {//属性private SqlSession sqlSession;private UserMapperAnnotation userMapperAnnotation;//初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();userMapperAnnotation = sqlSession.getMapper(UserMapperAnnotation.class);System.out.println("userMapperAnnotation--" + userMapperAnnotation.getClass());}@Testpublic void getUserById() {User user = userMapperAnnotation.getUserById(2);System.out.println("[注解方式]--user信息--" + user.getId() + "-" + user.getName());List<Pet> pets = user.getPets();for (Pet pet : pets) {System.out.println("养的宠物信息-" + pet.getId() + "-" + pet.getNickname());}if (sqlSession != null) {sqlSession.close();}}
}
public class PetMapperAnnotationTest {//属性private SqlSession sqlSession;private PetMapperAnnotation petMapperAnnotation;//初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();petMapperAnnotation = sqlSession.getMapper(PetMapperAnnotation.class);System.out.println("petMapperAnnotation--" + petMapperAnnotation.getClass());}@Testpublic void getPetByUserId() {List<Pet> pets = petMapperAnnotation.getPetByUserId(1);for (Pet pet : pets) {System.out.println("pet信息-" + pet.getId() + "-" + pet.getNickname());User user = pet.getUser();System.out.println("user信息-" + user.getName());}if (sqlSession != null) {sqlSession.close();}}@Testpublic void getPetById() {Pet pet = petMapperAnnotation.getPetById(4);System.out.println("pet信息-" + pet.getId() + "-" + pet.getNickname());User user = pet.getUser();System.out.println("主人信息-" + user.getId() + "-" + user.getName());if (sqlSession != null) {sqlSession.close();}}
}
课后练习
1.自己设计表 dept
(部门) 和 emp
(雇员), 是1
对多的关系
2.通过查询dept
, 可以级联查询得到所有emp
的信息
3.通过查询emp
, 可以级联查询得到对应的dept
信息
4.字段小伙伴可以自己设计, 尽量完成.
- dept(id, dep_name)
- emp(id, name, salary, dept_id)
●代码实现
1.创建 dept
表 和 emp
表
USE mybatis;CREATE TABLE `dept` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`dep_name` VARCHAR(64) NOT NULL DEFAULT ''
)CHARSET=utf8CREATE TABLE `emp` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL DEFAULT '',
`salay` DOUBLE NOT NULL DEFAULT 0.0,
`dept_id` INT,
FOREIGN KEY(`dept_id`) REFERENCES `dept`(`id`)
)CHARSET=utf8INSERT INTO `dept` VALUES (1, '宣传部'), (2, '技术部');
INSERT INTO `emp` VALUES (1, '赵志伟', 6000, 2), (2, '赵培竹', 6000, 1), (3, '阳光', 12000, 2), (4, '月亮', 12000, 1);SELECT * FROM `dept`;
SELECT * FROM `emp`;
2.创建com.zzw.entity.dept.java
和 com.zzw.entity.emp.java
@Getter
@Setter
public class Dept {private Integer id;private String depName;//级联查询, 一对多private List<Emp> emps;
}
@Getter
@Setter
public class Emp {private Integer id;private String name;private Double salary;//级联查询, 一对一private Dept dept;
}
3.创建EmpMapper.java
和 DeptMapper.java
public interface EmpMapper {//通过emp的id查询到emp对象, 同时会查询到emp对象关联的dept对象public Emp getEmpById(Integer id);//通过dept的id获取emp对象, 可能有多个, 因此用List接收public List<Emp> getEmpByDeptId(Integer deptId);
}
public interface DeptMapper {//通过dept的id查询到dept对象public Dept getDeptById(Integer id);
}
4.创建EmpMapper.xml
和 DeptMapper.xml
<mapper namespace="com.zzw.mapper.EmpMapper"><!--1.配置/实现public List<Emp> getEmpByDeptId(Integer deptId);2.通过dept的id获取emp对象, 可能有多个, 因此用List接收--><resultMap id="EmpResultMap" type="Emp"><id property="id" column="id"/><result property="name" column="name"/><result property="salary" column="salary"/><association property="dept" column="dept_id"select="com.zzw.mapper.DeptMapper.getDeptById"/></resultMap><select id="getEmpByDeptId" parameterType="Integer" resultMap="EmpResultMap">SELECT * FROM `emp` WHERE dept_id = #{deptId}</select>
</mapper>
<mapper namespace="com.zzw.mapper.EmpMapper"><!--1.配置/实现public Dept getDeptById(Integer id);2.通过dept的id查询到dept对象3.思路(1) 先通过dept-id 查询到部门信息 (2) 再根据dept-id查询对应的emp信息并映射到List<Emp> emps--><resultMap id="DeptResultMap" type="Dept"><id property="id" column="id"/><result property="depName" column="dep_name"/><!--解读: 因为emps属性是集合, 因此这里需要使用collection标签来处理1.ofType="Emp" 指定返回的集合中存放的数据类型Emp2.collection 表示 emps 是一个集合3.property="emps" 是返回的dept对象的属性 emps4.column="id" SELECT * FROM `dept` WHERE `id` = #{id} 返回的id字段的值--><collection property="emps" column="id" ofType="Emp"select="com.zzw.mapper.EmpMapper.getEmpByDeptId"/></resultMap><select id="getDeptById" parameterType="Integer" resultMap="DeptResultMap">SELECT * FROM `dept` WHERE `id` = #{id}</select>
</mapper>
5.创建EmpMapperTest
和 DeptMapperTest
public class EmpMapperTest {//属性private SqlSession sqlSession;private EmpMapper empMapper;//编写方法完成初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();empMapper = sqlSession.getMapper(EmpMapper.class);System.out.println("empMapper=" + empMapper.getClass());}@Testpublic void getEmpByDeptId() {List<Emp> emps = empMapper.getEmpByDeptId(2);for (Emp emp : emps) {System.out.println("emp信息-" + emp.getId() + "-" + emp.getName() + "-" + emp.getSalary());Dept dept = emp.getDept();System.out.println("部门信息-" + dept.getId() + "-" + dept.getDepName());}if (sqlSession != null) {sqlSession.close();}}
}
public class DeptMapperTest {//属性private SqlSession sqlSession;private DeptMapper deptMapper;//编写方法完成初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();deptMapper = sqlSession.getMapper(DeptMapper.class);System.out.println("deptMapper=" + deptMapper.getClass());}@Testpublic void getDeptById() {Dept dept = deptMapper.getDeptById(1);System.out.println("部门信息-" + dept.getId() + "-" + dept.getDepName());List<Emp> emps = dept.getEmps();for (Emp emp : emps) {System.out.println("员工信息-" + emp.getId() + "-" + emp.getName() + "-" + emp.getSalary());}if (sqlSession != null) {sqlSession.close();}}
}
6.实现getEmpById
, 体会复用
<!--
1.配置/实现public Emp getEmpById(Integer id);
2.通过emp的id查询到emp对象, 同时会查询到emp对象关联的dept对象
-->
<select id="getEmpById" parameterType="Integer" resultMap="EmpResultMap">SELECT * FROM `emp` WHERE `id` = #{id}
</select>
@Test
public void getEmpById() {Emp emp = empMapper.getEmpById(2);System.out.println("emp信息-" + emp.getId() + "-" + emp.getName() + "-" + emp.getSalary());Dept dept = emp.getDept();System.out.println("部门信息-" + dept.getId() + "-" + dept.getDepName());if (sqlSession != null) {sqlSession.close();}
}
注解方式实现
1.新建EmpMapperAnnotation
接口
public interface EmpMapperAnnotation {/*** 通过emp的id查询到emp对象, 同时会查询到emp对象关联的dept对象* <select id="getEmpById" parameterType="Integer" resultMap="EmpResultMap">* SELECT * FROM `emp` WHERE `id` = #{id}* </select>*/@Select("SELECT * FROM `emp` WHERE `id` = #{id}")@ResultMap("EmpResultMap")public Emp getEmpById(Integer id);/*** 通过dept的id获取emp对象, 可能有多个, 因此用List接收* <resultMap id="EmpResultMap" type="Emp">* <id property="id" column="id"/>* <result property="name" column="name"/>* <result property="salary" column="salary"/>* <association property="dept" column="dept_id"* select="com.zzw.mapper.DeptMapper.getDeptById"/>* </resultMap>* <select id="getEmpByDeptId" parameterType="Integer" resultMap="EmpResultMap">* SELECT * FROM `emp` WHERE dept_id = #{deptId}* </select>*/@Select("SELECT * FROM `emp` WHERE dept_id = #{deptId}")@Results(id = "EmpResultMap", value = {@Result(id = true, property = "id", column = "id"),@Result(property = "name", column = "name"),@Result(property = "salary", column = "salary"),@Result(property = "dept", column = "dept_id",one = @One(select = "com.zzw.mapper.DeptMapperAnnotation.getDeptById"))})public List<Emp> getEmpByDeptId(Integer deptId);
}
2.新建DeptMapperAnnotation
接口
public interface DeptMapperAnnotation {/**通过dept的id查询到dept对象* <resultMap id="DeptResultMap" type="Dept">* <id property="id" column="id"/>* <result property="depName" column="dep_name"/>* 解读: 因为emps属性是集合, 因此这里需要使用collection标签来处理* 1.ofType="Emp" 指定返回的集合中存放的数据类型Emp* 2.collection 表示 emps 是一个集合* 3.property="emps" 是返回的dept对象的属性 emps* 4.column="id" SELECT * FROM `dept` WHERE `id` = #{id} 返回的id字段的值* -->* <collection property="emps" column="id" ofType="Emp"* select="com.zzw.mapper.EmpMapper.getEmpByDeptId"/>* </resultMap>* <select id="getDeptById" parameterType="Integer" resultMap="DeptResultMap">* SELECT * FROM `dept` WHERE `id` = #{id}* </select>*/@Select("SELECT * FROM `dept` WHERE `id` = #{id}")@Results(id = "DeptResultMap", value = {@Result(id = true, property = "id", column = "id"),@Result(property = "depName", column = "dep_name"),@Result(property = "emps", column = "id",many = @Many(select = "com.zzw.mapper.EmpMapperAnnotation.getEmpByDeptId"))})public Dept getDeptById(Integer id);
}
3.新建 com.zzw.mapper.EmpMapperAnnotationTest
和 com.zzw.mapper.DeptMapperAnnotationTest
public class EmpMapperAnnotationTest {//属性private SqlSession sqlSession;private EmpMapperAnnotation empMapperAnnotation;//编写方法完成初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();empMapperAnnotation = sqlSession.getMapper(EmpMapperAnnotation.class);System.out.println("empMapperAnnotation=" + empMapperAnnotation.getClass());}@Testpublic void getEmpByDeptId() {List<Emp> emps = empMapperAnnotation.getEmpByDeptId(2);for (Emp emp : emps) {System.out.println("[注解方式]emp信息-" + emp.getId() + "-" + emp.getName() + "-" + emp.getSalary());Dept dept = emp.getDept();System.out.println("部门信息-" + dept.getId() + "-" + dept.getDepName());}if (sqlSession != null) {sqlSession.close();}}@Testpublic void getEmpById() {Emp emp = empMapperAnnotation.getEmpById(2);System.out.println("[注解方式]emp信息-" + emp.getId() + "-" + emp.getName() + "-" + emp.getSalary());Dept dept = emp.getDept();System.out.println("[注解方式]dept信息-" + dept.getId() + "-" + dept.getDepName());if (sqlSession != null) {sqlSession.close();}}
}
public class DeptMapperAnnotationTest {//属性private SqlSession sqlSession;private DeptMapperAnnotation deptMapperAnnotation;//编写方法完成初始化@Beforepublic void init() {sqlSession = MyBatisUtils.getSqlSession();deptMapperAnnotation = sqlSession.getMapper(DeptMapperAnnotation.class);System.out.println("deptMapperAnnotation=" + deptMapperAnnotation.getClass());}@Testpublic void getDeptById() {Dept dept = deptMapperAnnotation.getDeptById(1);System.out.println("[注解方式]dept信息-" + dept.getId() + "-" + dept.getDepName());List<Emp> emps = dept.getEmps();for (Emp emp : emps) {System.out.println("[注解方式]emp信息-" + emp.getId() + "-" + emp.getName() + "-" + emp.getSalary());}if (sqlSession != null) {sqlSession.close();}}
}
接下来我们学习, mybatis缓存…
💐💐💐💐💐💐💐💐给个赞, 点个关注吧, 各位大佬!💐💐💐💐💐💐💐💐
💐💐💐💐💐💐💐💐祝各位2024年大吉大运💐💐💐💐💐💐💐💐💐💐