今日内容:搭建mybatis ORM 配置数据源 $#的区别 增删改查
搭建mybatis
在原有maven项目基础配置上进行:
pom文件添加依赖
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
在resources文件夹创建Application.yml文件下配置数据源
# spring boot的主配置文件
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
#spring.mvc.view.prefix: /html/
#spring.mvc.view.suffix: .html
#配置mybatis的数据源 DataSource
spring:
datasource:
url: jdbc:mysql://localhost:3306/easydata
username: root
password: tcwjy2021
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath*:/mapper/*.xml
#配置后,MyBatis在初始化时会自动扫描并加载这些XML文件,使你可以利用其中定义的SQL映射语句和操作。
#debug日志打印在控制台
logging:
level:
com.easy.dao: debug
ORM对象关系映射
ORM 的工作原理:
- 映射配置:通过注解或XML配置文件,定义类与数据库表之间的映射关系。
- 会话管理:创建与数据库的会话,管理事务和缓存。
- 数据访问:通过ORM框架提供的方法或查询构建器来查询和操作数据。
- 数据持久化:将对象状态同步到数据库,包括创建、更新和删除操作。
<?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">
<mapper namespace="com.easy.dao.IStaffDao">
<!-- 这里写sql语句 和接口对应 id对应方法名 -->
<select id="getNow" resultType="string">
select now()
</select>
<!-- '${}'把值放入 #{}把值和类型放入(先确定类型,然后放入值) 并且可以防止sql注入 )-->
<!-- 预编译,将sql语句确定,之后无论传递任何参数都不会改变sql语句语义,可以有效防止sql注入 -->
<!-- 也可以加快批处理的效率,编译好一次一直执行就行 -->
<!-- sql语句执行分为两个过程 编译和执行指令 -->
<insert id="addStaff">
insert into staff(code,name,salary,username,password)
value(#{code},#{name},#{salary},#{username},#{password})
</insert>
<delete id="delStaff">
delete from staff where id=#{id}
</delete>
<update id="editStaff">
update staff set name=#{name}, salary=#{salary},username=#{username},userpass=#{userpass}
where id=#{id};
</update>
</mapper>
$#的区别
1.在Spring框架中,${}
还用于属性文件的占位符替换,而 #{}
用于表达式语言(SpEL)。在MyBatis中,${}
和 #{}
的作用与Spring不同,专门用于SQL语句的构建和参数处理。
2.sql语句执行分为两个过程:编译和执行指令, '${}'直接把值放入然后编译,存在sql注入风险;#{}将#{}
中的参数与SQL语句一起发送到数据库,数据库会根据参数的实际类型来处理它们,可以防止sql注入 。
预编译好处
利用预编译,将sql语句确定,之后无论传递任何参数都不会改变sql语句语义,可以有效防止sql注入;也可以加快批处理的效率,编译好一次一直执行就行 。
简单的增删改查实现
bean包下实体类
package com.easy.bean;
public class Department {
private int id;
private String code;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
mapper包下定义SQL映射
<?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">
<mapper namespace="com.easy.dao.IDepartmentDao">
<insert id="addDepartment">
insert into department(code,name)
value(#{code},#{name})
</insert>
<delete id="delDepartment">
delete from department where id=#{id}
</delete>
<update id="editDepartment">
update department set name=#{name}
where id=#{id};
</update>
<select id="selectDepartment" resultType="com.easy.bean.Department">
select * from department where id=#{id}
</select>
</mapper>
dao(Data Access Object"(数据访问对象))包下定义数据访问接口
package com.easy.dao;
import org.apache.ibatis.annotations.Mapper;
import com.easy.bean.Department;
@Mapper
public interface IDepartmentDao {
int addDepartment(Department dep);
int delDepartment(int id);
int editDepartment(Department dep);
Department selectDepartment(int id);
}
controller包下定义控制类
controller层接收前台参数时:
1. 使用 Map 传递参数会导致业务可读性的丧失,继而导致后续扩展和维护的困难,所以在实际应用中我们应该果断废弃该方式。
2.使用 @Param 注解传递参数会受到参数个数的影响。当 n≤5 时,它是最佳的传参方式,因为它更加直观;当 n>5 时,多个参数将给调用带来困难。
3.当参数个数大于 5 个时,建议使用 JavaBean 方式。
package com.easy.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.easy.bean.Department;
import com.easy.dao.IDepartmentDao;
@RestController
@RequestMapping("department")
public class DepartmentController {
@Autowired
IDepartmentDao dao;
@PostMapping
public String addDepartment(@RequestBody Department dep) {
dao.addDepartment(dep);
return "Add sucess";
}
@DeleteMapping("{id}")
public String delDepartment(@PathVariable int id) {
dao.delDepartment(id);
return "DEL sucess";
}
@PutMapping
public String editDepartment(@RequestBody Department dep) {
dao.editDepartment(dep);
return "EDIT sucess";
}
@GetMapping("{id}")
public Department selectDepartment(@PathVariable int id) {
return dao.selectDepartment(id);
}
}