springboot
工程,选择引入对应的起步依赖(mybatis
、mysql驱动
、lombok
)application.properties
中引入数据库连接信息:数据库连接四要素@Mapper
public interface EmpMapper {
//根据ID删除数据
@Delete("delete from emp where id=#{id}") //将下列的id赋值给占位符
public void delete(Integer id);//传入参数,动态的删除员工id
//如果上述有返回值,则返回值代表操作所影响的记录数
}
@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
@Autowired
private EmpMapper empmaper;
@Test
public void testDelete() {
empmaper.delete(17);
}
}
application.properties
中,打开mybatis
的日志,并指定输出到控制台。#指定mybatis输出日志的位置,输出控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
//代码演示:在登录界面输入: ' or '1'='1
select count(*) from emp where username = 'wuieuwiueiwuiew' and password = '' or '1'='1'
insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)
values ('songyuanqiao','宋远桥',1,'71.jpg',2,2012-10-09',2,2022-10-0110:00:00', 2022-10-0110:00:00');
//在mapper类中键入代码:
@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})")
// 主键返回的实现
@Options(keyProperty = "id",useGeneratedKeys = true) //会自动将生成的主键值,赋值给emp对象的id属性
//新增数据
@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})")
public 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);
@Test
public void testUpdate() {
//构造员工对象
Emp emp = new Emp();
emp.setId(1);
emp.setUsername("Tom5");
emp.setName("汤姆5");
emp.setImage("1.jpg");
emp.setGender((short) 1);
emp.setJob((short) 1);
emp.setEntryDate(LocalDate.of(2000, 1, 1));
emp.setUpdateTime(LocalDateTime.now());
emp.setDeptId(1);
//执行更新员工操作
empmaper.update(emp);
}
//在mapper类中:
//根据id查询员工
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);
输出结果:
Emp(id=1, username=Tom5, password=123456, name=汤姆5, gender=1, image=1.jpg, job=1, entrydate=2000-01-01, deptId=null, createTime=null, updateTime=null)
// 给字段起别名,让别名与实体类属性一致
@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);
@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);
application.properties
文件中配置:#开启mybatis的驼峰命名自动映射开关 c__andy ----->cAndy
mybatis.configuration.map-underscore-to-camel-case=true
//在mapper类中:
//根据id查询员工
@Select("select * from emp where id=#{id}")
public Emp getById(Integer id);
//条件查询员工
@Select("select * from emp where name like '%${name}%' and gender = #{gender} and " +
"entrydate between #{begin} and #{end} order by update_time desc")
public List list(String name , Short gender , LocalDate begin ,LocalDate end);
//使用泛型接收多条数据
注:上述代码不能直接使用
#
会当成字符处理,使用$
–字符串拼接符替代,但是有安全隐患(SQL注入
),处理方法如下:
-- mysql 语法中有concat的字符串拼接函数
select concat ( ' hello',' mysql', ' world' ) ;
-- 输出:hellomysqlworld
所以可以将上述的select语句改变成:
//条件查询员工
@Select("select * from emp where name like concat('%',#{name},'%') '%${name}%' and gender = #{gender} and " +
"entrydate between #{begin} and #{end} order by update_time desc")
public List list(String name , Short gender , LocalDate begin ,LocalDate end);
//使用泛型接收多条数据
//根据条件查询员工
@Test
public void testList() {
List empList = empmaper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
System.out.println(empList);
}