一:Mybatis基础操作--基础&核心
面向注解编程
返回值,参数值,预编译(占位符,解决sql注入不安全的问题)
(一)环境准备
1.需求说明
2.环境准备
Emp文件
package com.ithema.jopo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer id; //主键自增
private String usename; //用户名
private String password; //密码
private String name; //姓名
private short gender; //性别
private String image; //图片
private short job; //工作
private LocalDate entrydata; //入职时间
private Integer deptId; //所属部门id
private LocalDateTime createTime; //创建时间
private LocalDateTime dataTime; //修改时间
}
(二)删除--@Delete()
1.根据主键删除
-- mysql语句:
delete from emp where id=17
-- 接口文件 @Mapper public interface EmpMapper { //删除元素 //主键删除 @Delete("delete from emp where id=#{id}") public void delet( Integer id); }
-- test文件 class SpringbootMybatisDemoApplicationTests { @Autowired public EmpMapper empMapper; @Test //演示操作方法 public void delet(){ empMapper.delet(17); } }
2.配置mybatis日志
3.预编译sql--性能更高&安全
(1)性能高
(2)安全
解决SQL注入问题
预编译:
4.SQL注入问题(不安全)
5.占位符
(三)插入--@Insert()
1.新增操作+#占位
多个参数封装到对象中,然后传递一个对象
id主键自增,一开始17个数据,删掉两个,再新增一个,新增的数据id为18而不是16
2.新增(主键返回)
获取新增数据的自增主键值
(四)更新--@Updata()
1.需求展示
2.更新操作
(五)查询--@Select()
1.查询操作
2.数据封装
(1)给字段起别名
(2)@result注解,手动映射封装--不推荐
(3)在配置文件里开启mybatis的 驼峰命名自动映射开关--推荐
# 开启驼峰命名 mybatis.configuration.map-underscore-to-camel-case=true
3.条件查询
(1)案例操作
模糊匹配时不能用#占位符,只能用$占位符(拼接占位符)
---问题$占位符(拼接占位符)生成的不是预编译sql,导致性能低,不安全,存在sql注入问题
---解决方法--concat 字符串拼接函数
(不能用#占位符:因为:模糊匹配中like ('%张%'),用#占位符,变成了like ('%?%'),?在’‘里无法匹配)
(2)参数名说明
二.XML映射文件
java下运行文件
resourse下各类配置文件
基于注解的SQL语句---@Select
基于XML映射文件的SQL语句---本节内容
1. 3个规范
(1)建XML映射文件
(2)namesapce属性
上面复制的这个代码会报错---如下解决
正确的通用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">
namesapce属性
(3)mapper中写sql语句, 并定义id
id:接口中的方法名(使用该sql语句的方法)
返回值类型: 单条记录封装的类型,即使返回的是多行多列的list,list为每一条数据,这里返回值为list里的每一条数据的类型。注意同样要同上面namesapce属性值一样,用特殊引用格式
2.XML映射文件插件
3.如何选择注解与XML映射文件
三:Mybatis--动态SQL
1.<if> & <where>&<set>
(1)基础用法--<if> & <where>
<select id="select" resultType="com.ithema.jopo.Emp"> -- XML映射文件 select username, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp <where> <if test="name!=null"> name like concat('%',#{name},'%') </if> <if test="gender!=null"> and gender=#{gender} </if> <if test="begin!=null and end!=null"> and entrydate between #{begin} and #{end} </if> </where> </select>
//动态sql <if>--接口文件 public List<Emp> select(String name, short gender, LocalDate begin, LocalDate end );
@Test // 测试文件 public void selec(){ // if & where List<Emp> empList= empMapper.select(null,(short)2,null,null); System.out.println(empList); }
(2)案例--动态更新--<set>
报错:
要改成如下格式,图中还有一个错误,updata 中不用and,将and改为 , 逗号
错误: Mapper method 'com.ithema.mapper.EmpMapper.updataEmp' has an unsupported return type: class com.ithema.jopo.Emp
<update id="updataEmp"> update emp <set> <if test="name!=null"> name=#{name} </if> <if test="gender!=null"> , gender=#{gender} </if> <if test="usename!=null"> , usename =#{usename} </if> <if test="password!=null"> , password =#{password} </if> <if test="image!=null"> , image =#{image} </if> <if test="job!=null"> , job =#{job} </if> <if test="entrydata!=null"> , entrydata =#{entrydata} </if> <if test="deptId!=null"> , dept_id =#{deptId} </if> <if test="dataTime!=null"> , data_time =#{dataTime} </if> <if test="createTime!=null"> , create_time =#{createTime} </if> where id=#{id} </set> </update>
动态sql <if> <set> public void updataEmp(Emp emp );
@Test public void updat(){ Emp emp=new Emp(); emp.setGender((short)2); emp.setId(15); emp.setName("tom22"); emp.setDataTime(now()); empMapper.updataEmp(emp); }
2.<foreach>
举例:
<delete id="dele"> delete from emp where id in <foreach collection="arg" item="id" separator="," open="(" close=")"> #{id} </foreach> </delete>
@Test public void delee(){ ArrayList<Integer> arg=new ArrayList<>(); Collections.addAll(arg,1,2,3); empMapper.dele(arg); }
//<foreach> public void dele(ArrayList<Integer> arg);
3.<sql><include>
sql片段的查询与引用