CGB2110-DAY09-Mybatis
文章目录
- 1.Mybatis学习
- 1.1 动态Sql-where条件
- 1.1.1 编辑测试类
- 1.1.2 编辑Mapper接口
- 1.1.3 编辑Mapper 映射文件
- 1.2 动态Sql-Set标签
- 1.2.1 编辑测试类
- 1.2.2 编辑Mapper接口
- 1.2.3 编辑Mapper.xml映射文件
- 1.3 动态Sql-choose、when、otherwise
- 1.3.1 编辑测试类
- 1.3.2 编辑接口
- 1.3.3 编辑xml映射文件
- 1.4 ResultMap语法
- 1.4.1 封装数据表-POJO对象
- 1.4.2 准备Mapper接口/映射文件
- 1.4.3 需求说明
- 1.4.4 案例测试
- 1.4.5 编辑Mapper接口
- 1.4.6 编辑xml映射文件
- 1.5 驼峰映射规则
- 1.5.1 业务说明
- 2 Mybatis 关联关系
- 2.1 常见关联关系
- 2.2 一对一映射
- 2.2.1 创建表
- 2.2.2 测试准备
- 2.2.3 关于一对一业务说明
- 2.2.4 关联查询方式
- 2.2.5 编辑测试类
- 2.2.6 编辑EmpMapper接口
- 2.2.7 编辑EmpMapper.xml映射文件
- 2.2.8 连接查询
- 2.3 一对多查询
- 2.3.1 业务需求
- 2.3.2 编辑测试代码
- 2.3.3 编辑Mapper接口
- 2.3.4 编辑DeptMapper.xml文件
- 2.4 Mybatis的注解形式
- 2.5 @MapperScan
- 常用注解
1.Mybatis学习
1.1 动态Sql-where条件
1.1.1 编辑测试类
package com.jt;import com.jt.mapper.UserMapper2;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
public class TestMybatis2 {@Autowiredprivate UserMapper2 userMapper;/*** 需求: 根据用户不为null的属性充当where条件*/@Testpublic void testSqlWhere(){User user = new User();user.setAge(3000).setSex("男");List<User> userList = userMapper.findSqlWhere(user);System.out.println(userList);}}
1.1.2 编辑Mapper接口
@Mapper //将接口的代理对象交给Spring容器管理
public interface UserMapper2 {List<User> findSqlWhere(User user);
}
1.1.3 编辑Mapper 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.mapper.UserMapper2"><!--问题说明: 前端数据传递时可能会有null数据.如果数据为null则不能充当where条件解决方案: 动态Sql实现语法:<if test="判断条件"> id = #{id}</if>true: 则拼接标签内容false: 则不拼接标签内容<where>标签: 去除where后边多余的and/or--><select id="findSqlWhere" resultType="User">select * from demo_user<where><if test="id != null"> id = #{id}</if><if test="name !=null">and name = #{name}</if><if test="age !=null"> and age = #{age}</if><if test="sex !=null"> and sex = #{sex}</if></where></select>
</mapper>
1.2 动态Sql-Set标签
1.2.1 编辑测试类
/*** 需求: 修改id=1 的数据 name="北极熊",age=4000 sex="男"*/@Testpublic void testUpdateUser(){User user = new User();user.setId(1).setAge(5000);userMapper.updateUser(user);System.out.println("更新成功!!!");}
1.2.2 编辑Mapper接口
void updateUser(User user);
1.2.3 编辑Mapper.xml映射文件
<!--根据对象中不为null的属性 当做set条件语法: set标签 去除多余1个,号--><update id="updateUser">update demo_user<set><if test="name !=null">name=#{name},</if><if test="age !=null"> age=#{age},</if><if test="sex !=null"> sex=#{sex},</if></set>where id = #{id}</update>
1.3 动态Sql-choose、when、otherwise
1.3.1 编辑测试类
/*** 如果name有值,则根据name查询.* 如果name没有值,则根据age查询.* 如果name/age都没有值,则根据sex查询*/@Testpublic void testChoose(){User user = new User();user.setSex("男");List<User> userList = userMapper.findChoose(user);System.out.println(userList);}
1.3.2 编辑接口
List<User> findChoose(User user);
1.3.3 编辑xml映射文件
<!--* 如果name有值,则根据name查询.* 如果name没有值,则根据age查询.* 如果name/age都没有值,则根据sex查询语法类似: if->else-if->else--><select id="findChoose" resultType="User">select * from demo_user<where><choose><when test="name !=null"> name=#{name}</when><when test="age !=null"> age = #{age}</when><!--必须保证sex必须有值 --><otherwise>sex=#{sex}</otherwise></choose></where></select>
1.4 ResultMap语法
1.4.1 封装数据表-POJO对象
- 创建dog表
- 编辑POJO对象
package com.jt.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;import java.io.Serializable;
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class Dog implements Serializable {private Integer dogId;private String dogName;private Integer age;}
1.4.2 准备Mapper接口/映射文件
1.4.3 需求说明
1.经过demo_user测试,发现如果字段名称与对象属性的名称一致,Mybatis可以实现自动化的映射
2.如果遇到字段名称与属性的名称不一致的现象,则mybatis如何映射!!!
1.4.4 案例测试
package com.jt;import com.jt.mapper.DogMapper;
import com.jt.pojo.Dog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
public class TestMybatis3 {@Autowiredprivate DogMapper dogMapper;@Testpublic void testFindAll(){List<Dog> dogList = dogMapper.findAll();System.out.println(dogList);}
}
1.4.5 编辑Mapper接口
@Mapper
public interface DogMapper {List<Dog> findAll();
}
1.4.6 编辑xml映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.mapper.DogMapper"><!--规则:1.如果数据库中的表字段名称与POJO属性的名称不一致则mybatis无法自动完成映射.2.Mybatis提供了一个属性resultMap(使用最多的).支持用户手动映射.--><select id="findAll" resultMap="dogRM">select * from dog</select><!--属性说明: id="唯一标识,不能重复"type="映射的POJO对象的类型"简化说明: 如果字段名称与属性名称一致则可以省略autoMapping="true" 开启自动映射--><resultMap id="dogRM" type="Dog" autoMapping="true"><!--1.标识主键--><id column="dog_id" property="dogId"/><!--2.映射结果集--><result column="dog_name" property="dogName"/><!--<result column="age" property="age"/>--></resultMap>
</mapper>
1.5 驼峰映射规则
1.5.1 业务说明
Mybatis中的结果集的字段名称如果与属性的名称满足驼峰映射的规则. 如果开启驼峰映射.,则可以实现自动化的映射.
字段: dog_id, dog_name
属性: dogId, dogName 满足驼峰规则.
配置信息:
#3.配置Mybatis
mybatis:#定义别名包type-aliases-package: com.jt.pojo#将所有的映射文件全部加载mapper-locations: classpath:/mappers/*.xml#开启驼峰映射configuration:map-underscore-to-camel-case: true
2 Mybatis 关联关系
2.1 常见关联关系
思路:看问题从一头出发看向另一头
- 一对一 一个员工对应一个部门
- 一对多 一个部门下对应多个员工
- 多对一 本质是一对一
- 多对多 老师和学生 双向的一对多
一个老师对应多个学生.
一个学生对应多个老师.
2.2 一对一映射
2.2.1 创建表
表名 dept
字段: dept_id, int 主键 自增
dept_name, varchar(40)表名 emp
字段: emp_id, int 主键 自增
emp_name, varchar(40)
dept_id int造表数据
2.2.2 测试准备
根据表 创建POJO/Mapper接口/xml映射文件
2.2.3 关于一对一业务说明
表关系: 一个员工对应一个部门.
需求: 将部门信息与员工信息绑定.
如图所示:
2.2.4 关联查询方式
- 笛卡尔积的形式
- 连接查询 左连接,右连接,内连接
- 子查询
2.2.5 编辑测试类
package com.jt;import com.jt.mapper.DeptMapper;
import com.jt.mapper.DogMapper;
import com.jt.mapper.EmpMapper;
import com.jt.pojo.Dog;
import com.jt.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
public class TestMybatis4 {@Autowiredprivate DeptMapper deptMapper;@Autowiredprivate EmpMapper empMapper;//完成一对一测试@Testpublic void testEmp(){List<Emp> empList = empMapper.findAll();System.out.println(empList);}
}
2.2.6 编辑EmpMapper接口
@Mapper
public interface EmpMapper {List<Emp> findAll();
}
2.2.7 编辑EmpMapper.xml映射文件
<!--知识点:1.如果单表查询首选resultType2.如果进行关联查询 首选resultMap3.如果sql的结果集出现了重名字段,则mybatis映射必然报错.--><select id="findAll" resultMap="empRM">SELECT emp.*,dept.dept_name FROM emp,deptWHERE emp.dept_id = dept.dept_id</select><!-- 完成一对一封装目的: 一个员工中封装一个部门对象语法:1.association 表示一对一封装2.property 当前主对象的属性名称3.javaType 指定属性的类型--><resultMap id="empRM" type="Emp" autoMapping="true"><!-- 标识主键信息 --><id column="emp_id" property="empId"/><!--<result column="emp_name" property="empName"/>--><!--完成一对一映射--><association property="dept" javaType="Dept" autoMapping="true"><id column="dept_id" property="deptId"/></association></resultMap>
2.2.8 连接查询
SELECT emp.*,dept.dept_name FROM
emp LEFT JOIN
deptON emp.dept_id = dept.dept_id
2.3 一对多查询
2.3.1 业务需求
需求: 一个部门对应多个员工
表现形式:
2.3.2 编辑测试代码
@Testpublic void testDept(){List<Dept> deptList = deptMapper.findAll();System.out.println(deptList);}
2.3.3 编辑Mapper接口
@Mapper
public interface DeptMapper {List<Dept> findAll();
}
2.3.4 编辑DeptMapper.xml文件
<mapper namespace="com.jt.mapper.DeptMapper"><select id="findAll" resultMap="deptRM">SELECT dept.*,emp.emp_id,emp.emp_nameFROM dept,empWHERE dept.dept_id=emp.dept_id</select><!--一对多封装:1.collection: 封装集合类型2.ofType: 指定集合内部(泛型)的对象类型--><resultMap id="deptRM" type="Dept" autoMapping="true"><!--主键必须标识--><id column="dept_id" property="deptId"/><!--一对多封装--><collection property="emps" ofType="Emp" autoMapping="true"><id column="emp_id" property="empId"/></collection></resultMap></mapper>
2.4 Mybatis的注解形式
//鸡肋: 1.大公司一般不用, 2.只适用于单表操作.多表操作必须写映射文件// 注解和映射文件二选一@Select("select * from dept")List<Dept> selectAll();@Insert("insert into dept values (null,#{deptName})")void saveDept(Dept dept);
2.5 @MapperScan
常用注解
- @Configuration 标识当前类是配置类
- @ComponentScan 包扫描注解 扫描注解
- @Bean 标识该方法的返回值交给Spring容器管理
- @Scope 控制多例和单例
- @Lazy 懒加载
- @PostConstruct 初始化方法
- @PreDestroy 销毁方法
- @Component 将当前类未来的对象交给容器管理
- @Autowired 按照类型进行注入
- @Qualifier 按照名称进行注入
- @Repository 标识持久层注解
- @Service 标识Service层
- @Controller 标识Controller层
- @Value 为属性赋值 @Value("${key}")
- @PropertySource 加载指定路径的配置文件properties
- @Aspect 标识当前类是一个切面类
- @Pointcut 用于定义切入点表达式 表达式写法4种
- @EnableAspectJAutoProxy 让AOP的注解有效果
- @Before AOP-前置通知
- @AfterReturning AOP-后置通知
- @AfterThrowing AOP-异常通知
- @After AOP-最终通知
- @Around AOP-环绕通知
- @Order(1) //可以利用order关键字 实现AOP的排序 数字越小越先执行.
- @ResponseBody 将返回的数据转化为JSON串, 如果是字符串本身 原数据返回
- @RequestMapping("/hello") 实现浏览器的请求路径与方法的映射
- @PathVariable restFul结构,接收参数的注解.
- @GetMapping("") 只能接收GET请求类型
- @DeleteMapping("") 只能接收DELETE请求类型
- @PostMapping("") 只能接收POST请求类型
- @PutMapping("") 只能接收PUT请求类型
- @RestController 表示Controller类,同时要求返回值为JSON
- @CrossOrigin 允许跨域访问
- @RequestBody 参数接收时,将JSON串转化为java对象 json中的key与对象的属性一致.
- @Data lombok动态生成get/set/toString/equals/hashcode等方法
- @Accessors 控制是否开启链式加载结构
- @NoArgsConstructor 生成无参构造方法
- @AllArgsConstructor 生成全参构造方法
- @Mapper mybatis将当前的接口交给Spring容器管理. Map<类名小写,JDK动态代理对象>
- @SpringBootTest 该注解的作用在进行代码测试时启动spring容器,之后动态的获取对象 注意包路径 主启动类的同包及子包中.
- @Param Mybatis中将参数封装为Map集合. @Param(“maxAge”) int maxAge
- @Alias Mybatis中定义对象的别名 @Alias(“User”)
- @MapperScan Mybatis中扫描指定包路径的接口 为其创建代理对象.
- @Insert Mybatis 新增操作注解
- @Update Mybatis 修改操作注解
- @Delete Mybatis 删除操作注解
- @Select Mybatis 查询操作注解
CGB2110-DAY09-Mybatis相关推荐
- 小汤学编程之JavaEE学习day09——Mybatis
一.Mybatis简介 1.官网 2.Mybatis是什么 3.有何作用 4.扩展:Hibernate 二.Maven介绍 1.导包 2.准备数据库和表.实体类 ...
- 谷粒商城 Day09 首页分类与SpEL动态缓存切面
Day09 首页分类与SpEL动态缓存切面 一.优化缓存逻辑 百万并发进来,判断 bloomFilter 和缓存中拿,先执行哪个最好?1. 先布隆 ,再缓存 面对攻击 1 好 2. 先缓存 ,再布隆 ...
- mybatis查询报错:com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from string
mybatis查询报错: com.mysql.cj.exceptions.DataConversionException: Cannot determine value type from strin ...
- MyBatis的插入后获得主键的方式
需求: 使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法: 在mapper中指定keyProperty属性,示例如下: <insert id=" ...
- mybatis使用注解开发
mybatis使用注解开发 面向接口编程 在之前我们是通过面向对象编程,但是在真正开发的时候我们会选择面向接口编程. 根本原因 : 解耦 , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的 ...
- mybatis ResultMap
ResultMap 解决属性名和字段的名称不一致的问题. 查询为null的问题 创建java实体类: public class User {private int id; //idprivate St ...
- mybatis配置文件解析
mybatis配置文件解析 mybatis核心配置文件`mybatis-config.xml文件. mybatis的配置文件包含了会深深影响mybatis行为的设置和属性信息. 能配置的内容: con ...
- mybatis CRUD操作
mybatis CRUD操作 select select标签是mybatis最常用的标签之一. select语句有很多属性可以详细的配置每一天sql语句. id 命名空间唯一的标识. 接口中的方法名与 ...
- java mybatis基础
java mybatis基础 1.1 什么是mybatis? mybatis是一个优秀的持久层框架. 避免几乎所有的JDBC代码和手动设置参数以及获取结果集的过程. 可以使用简单的xml或者注解来配置 ...
- mybatis的资源过滤错误及xml文件编码错误
mybatis 解决maven项目内资源过滤的问题 写的配置文件无法被导出或者生效的问题. 解决方案: <build><resources><resource>&l ...
最新文章
- linux+向进城发送信号,信号 - it610.com
- android文件加解密开发,Android文件加密解密的实现
- python编程输入名字配对情侣网名_输入名字配对情侣网名 好听的情侣网名大全...
- 【手算】逆序数树形计算方法
- 极光:2021年移动互联网母婴亲子行业研究报告
- 程序员哀叹:专科都是几十万的年薪,互联网的泡沫要破了
- [转]js和jquery获取窗体高度
- 旅游展示网站-前端网页设计技术完整精美源码HTML+CSS+JS
- LDPC — 信道编码
- 记第一次尝试使用node进行爬虫的经历(使用cheerio模块)
- 自签名证书制作和使用方法
- Python模拟简易版淘宝客服机器人
- 《项梵自述》分享我的心酸经历
- 岗位-iOS开发工程师-总结
- 微信自动投票的php代码
- 夫妻贷款买房需要注意什么
- 04穿越功耗墙_怎么提升性能笔记
- xshell起图形界面后,鼠标点击不了
- html excel 在线查看,web在线查看服务器的Excel文件
- 基础35 空心三角形