mybatis基本操作-crud
动态获取参数,在SQL语句内通过占位符 #{ 参数名 }获取方法内部参数,以实现外界传递参数给方法,方法传递参数给SQL语句
数据库相关类
package com.itheima.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDate;
import java.time.LocalDateTime;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {private Integer id;private String username;private String password;private String name;private Short gender;private String image;private Short job;private LocalDate entrydate;private Integer deptId;private LocalDateTime createTime;private LocalDateTime updateTime;
}
删:
mapper接口
package com.itheima.mapper;import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import com.itheima.pojo.Emp;import java.util.List;@Mapper
public interface EmpMapper {@Delete("delete from emp where id = #{Myid}")public void delete(Integer Myid);
}
测试类启动入口
package com.itheima.springboot_mybatis_crud;import com.itheima.mapper.EmpMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringbootMybatisCrudApplicationTests {@Autowiredprivate EmpMapper empMapper;@Testpublic void testDelete() {empMapper.delete(17);}}
预编译SQL
观察mybatis底层执行的SQL语句:通过在配置文件中配置mybatis日志输出到控制台进行观察
#配置mybatis的日志,输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
性能更高
Java操作MySQL流程

防止SQL注入
SQL注入:通过输入的数据修改实现定义好的SQL语句,以此攻击服务器
未预编译日志

预编译后日志

占位符(面试题)

增:
接口方法
@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +"values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")void insert(Emp emp);
主键返回
添加Options注解
@Options(useGeneratedKeys = true,keyProperty = "id")//true表示要获取主键值,keyProperty表示将返回的主键值赋值给封装好的对象的某个属性@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +"values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")void insert(Emp emp);更新
@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);
@Testpublic void testUpdate() {Emp emp = new Emp();emp.setId(20);emp.setUsername("Tom");emp.setPassword("123456");emp.setName("Tom Jerry");emp.setGender((short) 1);emp.setImage("1.jpg");emp.setJob((short) 2);emp.setEntrydate(LocalDate.of(2022, 5, 1));emp.setDeptId(2);emp.setUpdateTime(LocalDateTime.now());empMapper.update(emp);}查询
@Select("select * from emp where id = #{id}")public Emp selectById(Integer id);返回结果中,存在无法自动封装的属性。如创建时间与更新时间
![]()
原因

mybatis自动封装问题解决
方案一:SQL语句中给字段名不一致的起别名

方案2:通过results和result注解

方案三:mybatis驼峰命名自动映射
对mybatis进行配置

spring.application.name=springboot_mybatis_crud
#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=123456
#配置mybatis的日志,输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#mybatis驼峰命名自动映射开关
mybatis.configuration.map-underscore-to-camel-case=true!需要保证数据库字段名为严格下划线方式命名,实体类属性为严格的驼峰命名
条件查询
@Select("select * from emp where name like '%${张}%' and gender = #{gender} and " +"entrydate between #{start} and #{end} order by update_time desc")public List<Emp> selectList();若使用#{}占位,则预编译SQL语句时会将 %#{}% 替换为? ?不允许存在于''中即#{}不允许出现在引号中,因此用${}符替代#{}符,通过字符串拼接将%与${张}拼接在一起,避免了?替代占位符的错误出现(有可能造成SQL注入)

推荐解决方案:concat函数拼接字符串
@Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and " +"entrydate between #{start} and #{end} order by update_time desc")public List<Emp> selectList(String name, Short gender, LocalDate start, LocalDate end);