手把手教你SpringBoot整合Mybatis
Mybatis的簡單介紹
? MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,并且改名為MyBatis 。2013年11月遷移到Github。
? iBATIS一詞來源于“internet”和“abatis”的組合,是一個基于Java的持久層框架。iBATIS提供的持久層框架包括SQL Maps和Data Access Objects(DAOs)
? MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設(shè)置參數(shù)以及獲取結(jié)果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java對象)映射成數(shù)據(jù)庫中的記錄。
? 思想:ORM:Object Relational Mapping
? Mybatis基本框架:
1 環(huán)境搭建
新建Spring Boot項目,引入依賴
pom.xml
<!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--Druid數(shù)據(jù)源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <!--測試--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
DB-sql
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
項目結(jié)構(gòu)
2 整合方式一:注解版
2.1 配置
server: port: 8081 spring: datasource: # 配置數(shù)據(jù)庫 username: root password: 12345 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test #數(shù)據(jù)庫名 type: com.alibaba.druid.pool.DruidDataSource
2.2 編碼
/** * 注解版 */ @Mapper @Repository public interface JavaStudentMapper { /** * 添加一個學(xué)生 * * @param student * @return */ @Insert("insert into student(name, age) " + "values (#{name}, #{age})") public int saveStudent(Student student); /** * 根據(jù)ID查看一名學(xué)生 * * @param id * @return */ @Select("select * " + "from student " + "where id = #{id}") public Student findStudentById(Integer id); /** * 查詢?nèi)繉W(xué)生 * * @return */ @Select("select * " + "from student") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") }) public List<Student> findAllStudent(); /** * 根據(jù)ID刪除一個 * * @param id * @return */ @Delete("delete " + "from student " + "where id = #{id}") public int removeStudentById(Integer id); /** * 根據(jù)ID修改 * * @param student * @return */ @Update("update set name=#{name},age=#{age} " + "where id=#{id}") public int updateStudentById(Student student); }
2.3 測試
@Autowired private JavaStudentMapper studentMapper; @Test void testJavaMapperInsert() { Student student = new Student("張三", 22); System.out.println(studentMapper.saveStudent(student)); } @Test void testJavaMapperFind() { System.out.println(studentMapper.findStudentById(2)); } @Test void testJavaMapperFindAll() { System.out.println(studentMapper.findAllStudent()); } @Test void testJavaMapperUpdate() { Student student = new Student(2, "張三", 22); System.out.println(studentMapper.updateStudentById(student)); } @Test void testJavaMapperDelete() { System.out.println(studentMapper.removeStudentById(2)); }
3 整合方式二:XML版
3.1 配置
server: port: 8081 spring: application: name: hospitalManager #項目名 datasource: # 配置數(shù)據(jù)庫 username: root password: 12345 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test #數(shù)據(jù)庫名 type: com.alibaba.druid.pool.DruidDataSource mybatis: mapper-locations: classpath:mapper/*Mapper.xml #掃描resources下的mapper文件夾下的xml文件 type-aliases-package: org.ymx.sp_mybatis.pojo #實體類所在包,定義別名
主啟動類:
@SpringBootApplication @MapperScan("org.ymx.sp_mybatis.xmlMapper") //掃描的mapper public class SpMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpMybatisApplication.class, args); } }
3.2 編碼
/** * xml版 */ @Mapper @Repository public interface XmlStudentMapper { /** * 添加一個學(xué)生 * * @param student * @return */ public int saveStudent(Student student); /** * 根據(jù)ID查看一名學(xué)生 * * @param id * @return */ public Student findStudentById(Integer id); /** * 查詢?nèi)繉W(xué)生 * * @return */ public List<Student> findAllStudent(); /** * 根據(jù)ID刪除一個 * * @param id * @return */ public int removeStudentById(Integer id); /** * 根據(jù)ID修改 * * @param student * @return */ public int updateStudentById(Student student); }
xml文件(StudentMapper.xml)
<?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="org.ymx.sp_mybatis.dao.xmlMapper.XmlStudentMapper"> <!-- 添加學(xué)生--> <insert id="saveStudent" parameterType="org.ymx.sp_mybatis.pojo.Student" useGeneratedKeys="true" keyProperty="id"> insert into student(name, age) values (#{name}, #{age}) </insert> <!--查看學(xué)生根據(jù)ID--> <select id="findStudentById" parameterType="integer" resultType="org.ymx.sp_mybatis.pojo.Student"> select * from student where id = #{id} </select> <!--查看全部學(xué)生--> <select id="findAllStudent" resultMap="studentResult"> select * from student </select> <!--刪除學(xué)生根據(jù)ID--> <delete id="removeStudentById" parameterType="integer"> delete from student where id = #{id} </delete> <!--修改學(xué)生根據(jù)ID--> <update id="updateStudentById" parameterType="org.ymx.sp_mybatis.pojo.Student"> update student set name=#{name}, age=#{age} where id = #{id} </update> <!--查詢結(jié)果集--> <resultMap id="studentResult" type="org.ymx.sp_mybatis.pojo.Student"> <result column="id" javaType="INTEGER" jdbcType="INTEGER" property="id"/> <result column="name" javaType="STRING" jdbcType="VARCHAR" property="name"/> <result column="age" javaType="INTEGER" jdbcType="INTEGER" property="age"/> </resultMap> </mapper>
3.3 測試
@Autowired private XmlStudentMapper studentMapper; @Test void testJavaMapperInsert() { Student student = new Student("張三", 22); System.out.println(studentMapper.saveStudent(student)); } @Test void testJavaMapperFind() { System.out.println(studentMapper.findStudentById(2)); } @Test void testJavaMapperFindAll() { System.out.println(studentMapper.findAllStudent()); } @Test void testJavaMapperUpdate() { Student student = new Student(2, "張三", 22); System.out.println(studentMapper.updateStudentById(student)); } @Test void testJavaMapperDelete() { System.out.println(studentMapper.removeStudentById(2)); }
4 總結(jié)
基本步驟:
注意事項:
(1)相比注解方式,更推薦使用xml方式,因為注解方式將SQL語句嵌套到Java代碼中,一旦需要修改則需要重新編譯項目,而xml方式則不需要重新編譯項目
(2)xml方式需要在主啟動函數(shù)或配置類中配置接口的掃描路徑
到此這篇關(guān)于手把手教你SpringBoot整合Mybatis的文章就介紹到這了,更多相關(guān)SpringBoot整合Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA中WebService生成Java代碼并調(diào)用外部接口實現(xiàn)代碼
這篇文章主要介紹了IDEA中WebService生成Java代碼并調(diào)用外部接口實現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05Spring?Data?JPA框架快速入門之自定義Repository接口
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的自定義Repository接口2022-04-04SpringBoot?自定義注解實現(xiàn)涉密字段脫敏
關(guān)于數(shù)據(jù)脫敏,網(wǎng)上的文章都是硬編碼規(guī)則,比如對身份證,手機(jī)號,郵件地址等固定寫法脫敏。本文在此基礎(chǔ)上,拓展動態(tài)從數(shù)據(jù)庫查出涉密關(guān)鍵字執(zhí)行脫敏操作。感興趣的同學(xué)可以參考閱讀2023-03-03基于spring boot實現(xiàn)一個全局異常處理器
在項目開發(fā)中,我們可以基于spring boot提供的切面特性,來很輕松的實現(xiàn)全局異常的處理,所以本文主要為大家介紹了如何基于spring boot實現(xiàn)一個全局異常處理器,有需要的可以參考下2023-09-09淺談springboot項目中定時任務(wù)如何優(yōu)雅退出
這篇文章主要介紹了淺談springboot項目中定時任務(wù)如何優(yōu)雅退出?具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09