MyBatis基础操作——增删改查+模糊查询+参数说明
1.删除
直接在Mapper层接口类中新建一个删除方法,为其添加@Delete
注解,例如如下代码:
//根据ID删除数据
@Delete("delete from emp where id = #{id}")
public void delete(Integer id);
在Test类中使用该方法并查看结果:
@Test
public void deleteTest(){
empMapper.delete(16);
}
2.新增
直接在Mapper层接口类中新建一个删除方法,为其添加@Insert
注解,例如如下代码:
//新增员工
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)\n" + "VALUES (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime});")
public void insert(Emp emp);
在Test类中使用该方法并查看结果:
@Test
public void insertTest(){
//构造员工对象
Emp emp = new Emp();
emp.setUsername("Tom3");
emp.setName("汤姆3");
emp.setGender((short) 1);
emp.setImage("1.jpg");
emp.setJob((short) 1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行新增员工信息操作
empMapper.insert(emp);
}
注意:若想要获得新增对象在数据库表中的id值,可以通过在@Insert
注解上面添加@Options(useGeneratedKeys = true, keyProperty = "id")
来实现。
3.更新
直接在Mapper层接口类中新建一个删除方法,为其添加@Update
注解,例如如下代码:
//更新员工
@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, job = #{job}, entrydate = #{entrydate}, dept_id = #{deptId}, update_time = #{updateTime} where id = #{id};")
public void update(Emp emp);
在Test类中使用该方法并查看结果:
//更新员工
@Test
public void updateTest(){
//构造员工对象
Emp emp = new Emp();
emp.setId(18);
emp.setUsername("Tom1");
emp.setName("汤姆1");
emp.setGender((short) 1);
emp.setImage("1.jpg");
emp.setJob((short) 1);
emp.setEntrydate(LocalDate.of(2000,1,1));
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行更新员工操作
empMapper.update(emp);
}
4.查询(根据ID查询)
直接在Mapper层接口类中新建一个删除方法,为其添加@Select
注解,例如如下代码:
//根据id查询员工
@Select("select * from emp where id = #{id}")
public Emp getById(Integer id);
在Test类中使用该方法并查看结果:
//根据ID查询员工
@Test
public void testGetById(){
Emp emp = empMapper.getById(20);
System.out.println(emp);
}
注意:若数据库表中列名与java对象类中定义的属性名不一致,则会出现emp中不一致名字的属性值为null,比如说若数据库表中有一列名为dept_id的数据,而Emp类定义的属性名为deptId,则使用以上方法会导致emp.getDeptId的值为null,想要解决该问题,有以下三个解决方案:
(1)方案一:给字段起别名,让别名与实体类属性名一致
//方案一:给字段起别名,让别名与实体类属性名一致
@Select("select id, username, password, name, gender, image, job, entrydate, dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id}")
public Emp getById(Integer id);
(2)方案二:通过@Results,@Result注解手动映射封装
//方案二:通过@Results,@Result注解手动映射封装
@Results({
@Result(column = "dept_id", property = "deptId"),
@Result(column = "create_time", property = "createTime"),
@Result(column = "update_time", property = "updateTime")
})
@Select("select * from emp where id = #{id}")
public Emp getById(Integer id);
(3)方案三:开启mybatis的驼峰命名自动映射开关 —a_cloumn ----->aCloumn
先在application.properties配置文件中添加以下指令:
#开启mybatis的驼峰命名自动映射开关 ---a_cloumn ----->aCloumn
mybatis.configuration.map-underscore-to-camel-case=true
然后正常使用@Select
注解即可
@Select("select * from emp where id = #{id}")
public Emp getById(Integer id);
5.查询(条件查询)
若想要进行模糊查询,则需要使用到like "%(关键字)%"
格式的SQL查询语句,此时若使用#{…}的格式即like "%#{属性名}%"
,SQL语句在预编译的时候,会以字符串的形式编译双引号“”里面的内容,并不会解析#{…},解决方案如下:
方案一:使用${…},参数值拼接到SQL语句中
//条件查询员工
@Select("select * from emp where name like '%${name}%' and gender = #{gender} and " + "entrydate between #{begin} and #{end} order by update_time desc")
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
方案二:使用SQL语句中的concat函数拼接字符串
Concat函数:将字符串拼接
select concat('hello', 'mysql', 'world');
即可以修改条件查询方法代码如下:
//条件查询员工
@Select("select * from emp where name like concat('%', #{name}, '%') and gender = #{gender} and " +
"entrydate between #{begin} and #{end} order by update_time desc")
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
这样做的好处是程序可以进行预编译,性能更高也更安全
6.参数说明
原因:在SpringBoot1.x版本以及单独使用mybatis的时候,在对mapper接口进行编译的过程当中,并不会保留方法的形参名称,例如上图中的name、gender、begin、end并不会在编译后的字节码文件当中保留。在生成的字节码文件当中,最终的形参的名字变成下图中的形参名字:var1、var2、var3、var4。