東莞常平網(wǎng)站建設(shè)引流推廣是什么意思
今日內(nèi)容:搭建mybatis ? ORM ? ?配置數(shù)據(jù)源 ? $#的區(qū)別 ? ?增刪改查
搭建mybatis
? ? ? ? 在原有maven項(xiàng)目基礎(chǔ)配置上進(jìn)行:
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文件夾創(chuàng)建Application.yml文件下配置數(shù)據(jù)源
# 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的數(shù)據(jù)源 DataSource
spring: datasource: url: jdbc:mysql://localhost:3306/easydatausername: rootpassword: tcwjy2021driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:mapper-locations: classpath*:/mapper/*.xml
#配置后,MyBatis在初始化時(shí)會(huì)自動(dòng)掃描并加載這些XML文件,使你可以利用其中定義的SQL映射語(yǔ)句和操作。
#debug日志打印在控制臺(tái)
logging:level:com.easy.dao: debug
ORM對(duì)象關(guān)系映射
ORM 的工作原理:
- 映射配置:通過(guò)注解或XML配置文件,定義類與數(shù)據(jù)庫(kù)表之間的映射關(guān)系。
- 會(huì)話管理:創(chuàng)建與數(shù)據(jù)庫(kù)的會(huì)話,管理事務(wù)和緩存。
- 數(shù)據(jù)訪問(wèn):通過(guò)ORM框架提供的方法或查詢構(gòu)建器來(lái)查詢和操作數(shù)據(jù)。
- 數(shù)據(jù)持久化:將對(duì)象狀態(tài)同步到數(shù)據(jù)庫(kù),包括創(chuàng)建、更新和刪除操作。
<?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"><!-- 這里寫(xiě)sql語(yǔ)句 和接口對(duì)應(yīng) id對(duì)應(yīng)方法名 --><select id="getNow" resultType="string">select now()</select><!-- '${}'把值放入 #{}把值和類型放入(先確定類型,然后放入值) 并且可以防止sql注入 )--><!-- 預(yù)編譯,將sql語(yǔ)句確定,之后無(wú)論傳遞任何參數(shù)都不會(huì)改變sql語(yǔ)句語(yǔ)義,可以有效防止sql注入 --><!-- 也可以加快批處理的效率,編譯好一次一直執(zhí)行就行 --><!-- sql語(yǔ)句執(zhí)行分為兩個(gè)過(guò)程 編譯和執(zhí)行指令 --><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>
$#的區(qū)別
? ? 1.在Spring框架中,${}
還用于屬性文件的占位符替換,而 #{}
用于表達(dá)式語(yǔ)言(SpEL)。在MyBatis中,${}
和 #{}
的作用與Spring不同,專門(mén)用于SQL語(yǔ)句的構(gòu)建和參數(shù)處理。
? ? 2.sql語(yǔ)句執(zhí)行分為兩個(gè)過(guò)程:編譯和執(zhí)行指令,?'${}'直接把值放入然后編譯,存在sql注入風(fēng)險(xiǎn);#{}將#{}
中的參數(shù)與SQL語(yǔ)句一起發(fā)送到數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)會(huì)根據(jù)參數(shù)的實(shí)際類型來(lái)處理它們,可以防止sql注入 。
預(yù)編譯好處
????????利用預(yù)編譯,將sql語(yǔ)句確定,之后無(wú)論傳遞任何參數(shù)都不會(huì)改變sql語(yǔ)句語(yǔ)義,可以有效防止sql注入;也可以加快批處理的效率,編譯好一次一直執(zhí)行就行 。
?
簡(jiǎn)單的增刪改查實(shí)現(xiàn)
bean包下實(shí)體類
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"(數(shù)據(jù)訪問(wèn)對(duì)象))包下定義數(shù)據(jù)訪問(wèn)接口
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層接收前臺(tái)參數(shù)時(shí):
? ? ? ?1. 使用 Map 傳遞參數(shù)會(huì)導(dǎo)致業(yè)務(wù)可讀性的喪失,繼而導(dǎo)致后續(xù)擴(kuò)展和維護(hù)的困難,所以在實(shí)際應(yīng)用中我們應(yīng)該果斷廢棄該方式。
? ? ? ? 2.使用 @Param 注解傳遞參數(shù)會(huì)受到參數(shù)個(gè)數(shù)的影響。當(dāng) n≤5 時(shí),它是最佳的傳參方式,因?yàn)樗又庇^;當(dāng) n>5 時(shí),多個(gè)參數(shù)將給調(diào)用帶來(lái)困難。
? ? ? ? 3.當(dāng)參數(shù)個(gè)數(shù)大于 5 個(gè)時(shí),建議使用 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 {@AutowiredIDepartmentDao dao;@PostMappingpublic 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";}@PutMappingpublic String editDepartment(@RequestBody Department dep) {dao.editDepartment(dep);return "EDIT sucess";}@GetMapping("{id}")public Department selectDepartment(@PathVariable int id) {return dao.selectDepartment(id);}
}