摘要:提供映射標(biāo)簽,支持對(duì)象與數(shù)據(jù)庫(kù)的字段關(guān)系映射提供對(duì)象關(guān)系映射標(biāo)簽,支持對(duì)象關(guān)系組建維護(hù)提供標(biāo)簽,支持編寫動(dòng)態(tài)。層實(shí)現(xiàn)類添加更新刪除根據(jù)查詢查詢所有的層構(gòu)建測(cè)試結(jié)果其他接口已通過測(cè)試,無問題。
微信公眾號(hào):一個(gè)優(yōu)秀的廢人前言
如有問題或建議,請(qǐng)后臺(tái)留言,我會(huì)盡力解決你的問題。
如題,今天介紹 SpringBoot 與 Mybatis 的整合以及 Mybatis 的使用,本文通過注解的形式實(shí)現(xiàn)。
什么是 MybatisMyBatis 是支持定制化 SQL、存儲(chǔ)過程以及高級(jí)映射的優(yōu)秀的持久層框架。MyBatis 避免了幾乎所有的 JDBC 代碼和手動(dòng)設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis 可以對(duì)配置和原生 Map 使用簡(jiǎn)單的 XML 或注解,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 對(duì)象)映射成數(shù)據(jù)庫(kù)中的記錄。
優(yōu)點(diǎn):
簡(jiǎn)單易學(xué):本身就很小且簡(jiǎn)單。沒有任何第三方依賴,最簡(jiǎn)單安裝只要兩個(gè) jar 文件+配置幾個(gè) sql 映射文件易于學(xué)習(xí),易于使用,通過文檔和源代碼,可以比較完全的掌握它的設(shè)計(jì)思路和實(shí)現(xiàn)。
靈活:mybatis 不會(huì)對(duì)應(yīng)用程序或者數(shù)據(jù)庫(kù)的現(xiàn)有設(shè)計(jì)強(qiáng)加任何影響。 sql 寫在 xml 里,便于統(tǒng)一管理和優(yōu)化。通過 sql 基本上可以實(shí)現(xiàn)我們不使用數(shù)據(jù)訪問框架可以實(shí)現(xiàn)的所有功能,或許更多。
解除 sql 與程序代碼的耦合:通過提供 DAL 層,將業(yè)務(wù)邏輯和數(shù)據(jù)訪問邏輯分離,使系統(tǒng)的設(shè)計(jì)更清晰,更易維護(hù),更易單元測(cè)試。sql 和代碼的分離,提高了可維護(hù)性。
提供映射標(biāo)簽,支持對(duì)象與數(shù)據(jù)庫(kù)的 orm 字段關(guān)系映射
提供對(duì)象關(guān)系映射標(biāo)簽,支持對(duì)象關(guān)系組建維護(hù)
提供xml標(biāo)簽,支持編寫動(dòng)態(tài) sql。
缺點(diǎn):
編寫 SQL 語(yǔ)句時(shí)工作量很大,尤其是字段多、關(guān)聯(lián)表多時(shí),更是如此。
SQL 語(yǔ)句依賴于數(shù)據(jù)庫(kù),導(dǎo)致數(shù)據(jù)庫(kù)移植性差,不能更換數(shù)據(jù)庫(kù)。
框架還是比較簡(jiǎn)陋,功能尚有缺失,雖然簡(jiǎn)化了數(shù)據(jù)綁定代碼,但是整個(gè)底層數(shù)據(jù)庫(kù)查詢實(shí)際還是要自己寫的,工作量也比較大,而且不太容易適應(yīng)快速數(shù)據(jù)庫(kù)修改。
二級(jí)緩存機(jī)制不佳
準(zhǔn)備工作IDEA
JDK1.8
SpringBoot 2.1.3
sql 語(yǔ)句,創(chuàng)建表,插入數(shù)據(jù):
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `age` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `student` VALUES ("1", "aaa", "21"); INSERT INTO `student` VALUES ("2", "bbb", "22"); INSERT INTO `student` VALUES ("3", "ccc", "23");pom.xml 文件配置依賴
application.yaml 配置文件4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.nasus mybatis 0.0.1-SNAPSHOT mybatis mybatis Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java runtime com.alibaba druid 1.1.9 org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.hibernate.javax.persistence hibernate-jpa-2.1-api 1.0.0.Final org.springframework.boot spring-boot-maven-plugin
spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver實(shí)體類
import javax.persistence.GeneratedValue; import javax.persistence.Id; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Student { @Id @GeneratedValue private Integer id; private String name; private Integer age; }
使用了 lombok 簡(jiǎn)化了代碼。
dao 層import com.nasus.mybatis.domain.Student; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; @Mapper public interface StudentMapper { @Insert("insert into student(name, age) values(#{name}, #{age})") int add(Student student); @Update("update student set name = #{name}, age = #{age} where id = #{id}") int update(@Param("name") String name, @Param("age") Integer age, @Param("id") Integer id); @Delete("delete from student where id = #{id}") int delete(int id); @Select("select id, name as name, age as age from student where id = #{id}") Student findStudentById(@Param("id") Integer id); @Select("select id, name as name, age as age from student") ListfindStudentList(); }
這里有必要解釋一下,@Insert 、@Update、@Delete、@Select 這些注解中的每一個(gè)代表了執(zhí)行的真實(shí) SQL。 它們每一個(gè)都使用字符串?dāng)?shù)組 (或多帶帶的字符串)。如果傳遞的是字符串?dāng)?shù)組,它們由每個(gè)分隔它們的多帶帶空間串聯(lián)起來。這就當(dāng)用 Java 代碼構(gòu)建 SQL 時(shí)避免了“丟失空間”的問題。 然而,如果你喜歡,也歡迎你串聯(lián)多帶帶 的字符串。屬性:value,這是字符串 數(shù)組用來組成多帶帶的 SQL 語(yǔ)句。
@Param 如果你的映射方法的形參有多個(gè),這個(gè)注解使用在映射方法的參數(shù)上就能為它們?nèi)∽远x名字。若不給出自定義名字,多參數(shù)(不包括 RowBounds 參數(shù))則先以 "param" 作前綴,再加上它們的參數(shù)位置作為參數(shù)別名。例如 #{param1},#{param2},這個(gè)是默認(rèn)值。如果注解是 @Param("id"),那么參數(shù)就會(huì)被命名為 #{id}。
service 層import com.nasus.mybatis.domain.Student; import java.util.List; public interface StudentService { int add(Student student); int update(String name, Integer age, Integer id); int delete(Integer id); Student findStudentById(Integer id); ListfindStudentList(); }
實(shí)現(xiàn)類:
import com.nasus.mybatis.dao.StudentMapper; import com.nasus.mybatis.domain.Student; import com.nasus.mybatis.service.StudentService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentMapper studentMapper; /** * 添加 Student * @param name * @param age * @return */ @Override public int add(Student student) { return studentMapper.add(student); } /** * 更新 Student * @param name * @param age * @param id * @return */ @Override public int update(String name, Integer age, Integer id) { return studentMapper.update(name,age,id); } /** * 刪除 Student * @param id * @return */ @Override public int delete(Integer id) { return studentMapper.delete(id); } /** * 根據(jù) id 查詢 Student * @param id * @return */ @Override public Student findStudentById(Integer id) { return studentMapper.findStudentById(id); } /** * 查詢所有的 Student * @return */ @Override public Listcontroller 層構(gòu)建 restful APIfindStudentList() { return studentMapper.findStudentList(); } }
import com.nasus.mybatis.domain.Student; import com.nasus.mybatis.service.StudentService; import java.util.List; 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.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/Student") public class StudentController { @Autowired private StudentService studentService; @PostMapping("") public int add(@RequestBody Student student){ return studentService.add(student); } @PutMapping("/{id}") public int updateStudent(@PathVariable("id") Integer id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "age", required = true) Integer age){ return studentService.update(name,age,id); } @DeleteMapping("/{id}") public void deleteStudent(@PathVariable("id") Integer id){ studentService.delete(id); } @GetMapping("/{id}") public Student findStudentById(@PathVariable("id") Integer id){ return studentService.findStudentById(id); } @GetMapping("/list") public List測(cè)試結(jié)果findStudentList(){ return studentService.findStudentList(); } }
其他接口已通過 postman 測(cè)試,無問題。
源碼下載:github 地址
后語(yǔ)以上為 SpringBoot 實(shí)戰(zhàn) (九) | 整合 Mybatis 的教程,除了注解方式實(shí)現(xiàn)以外,Mybatis 還提供了 XML 方式實(shí)現(xiàn)。想了解更多用法請(qǐng)移步官方文檔。
最后,對(duì) Python 、Java 感興趣請(qǐng)長(zhǎng)按二維碼關(guān)注一波,我會(huì)努力帶給你們價(jià)值,如果覺得本文對(duì)你哪怕有一丁點(diǎn)幫助,請(qǐng)幫忙點(diǎn)好看,讓更多人知道。
另外,關(guān)注之后在發(fā)送 1024 可領(lǐng)取免費(fèi)學(xué)習(xí)資料。資料內(nèi)容詳情請(qǐng)看這篇舊文:Python、C++、Java、Linux、Go、前端、算法資料分享
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/73369.html
摘要:引入了新的環(huán)境和概要信息,是一種更揭秘與實(shí)戰(zhàn)六消息隊(duì)列篇掘金本文,講解如何集成,實(shí)現(xiàn)消息隊(duì)列。博客地址揭秘與實(shí)戰(zhàn)二數(shù)據(jù)緩存篇掘金本文,講解如何集成,實(shí)現(xiàn)緩存。 Spring Boot 揭秘與實(shí)戰(zhàn)(九) 應(yīng)用監(jiān)控篇 - HTTP 健康監(jiān)控 - 掘金Health 信息是從 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...
摘要:如要運(yùn)行多次,請(qǐng)把上次生成的映射文件代碼刪除再運(yùn)行。層啟動(dòng)類掃描接口,必須加上提一嘴,這個(gè)注解非常的關(guān)鍵,這個(gè)對(duì)應(yīng)了項(xiàng)目中所對(duì)應(yīng)的包路徑,必須加上,否則會(huì)導(dǎo)致異常。另外,關(guān)注之后在發(fā)送可領(lǐng)取免費(fèi)學(xué)習(xí)資料。 微信公眾號(hào):一個(gè)優(yōu)秀的廢人如有問題或建議,請(qǐng)后臺(tái)留言,我會(huì)盡力解決你的問題。 前言 如題,今天介紹 SpringBoot 與 Mybatis 的整合以及 Mybatis 的使用,之前...
摘要:前提好幾周沒更新博客了,對(duì)不斷支持我博客的童鞋們說聲抱歉了。熟悉我的人都知道我寫博客的時(shí)間比較早,而且堅(jiān)持的時(shí)間也比較久,一直到現(xiàn)在也是一直保持著更新狀態(tài)。 showImg(https://segmentfault.com/img/remote/1460000014076586?w=1920&h=1080); 前提 好幾周沒更新博客了,對(duì)不斷支持我博客的童鞋們說聲:抱歉了!。自己這段時(shí)...
閱讀 945·2021-11-18 10:02
閱讀 1829·2019-08-30 15:56
閱讀 2624·2019-08-30 13:47
閱讀 2695·2019-08-29 12:43
閱讀 922·2019-08-29 11:19
閱讀 1859·2019-08-28 18:23
閱讀 2726·2019-08-26 12:23
閱讀 3080·2019-08-23 15:29