Spring Boot 教程 Mybatis 1. 什麼是Mybatis? MyBatis 是一款優秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設置參數和獲取結果集的工作。MyBatis 可以通過簡單的 XML 或註解來配置和映射原 ...
Spring Boot 教程 - Mybatis
1. 什麼是Mybatis?
MyBatis 是一款優秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設置參數和獲取結果集的工作。MyBatis 可以通過簡單的 XML 或註解來配置和映射原始類型、介面和 Java POJO(Plain Old Java Objects,普通老式 Java 對象)為資料庫中的記錄。
2. Spring Boot 整合 Mybatis
-
2.1 引入maven坐標
<!--springboot父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!--springboot框架web組件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency> <!--mybatis整合springboot組件--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <!--mysql資料庫連接驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <!--lombok組件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> </dependencies> <build> <!--springboot的maven插件--> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>
-
2.2 編寫代碼
-
2.2.1 創建如圖所示的目錄結構
記得兩個mapper文件的路徑要一致,在編譯之後mybatis的映射文件和介面文件才會在同一個目錄下。
-
2.2.2 代碼編寫
-
2.2.2.1 創建配置文件和啟動類
application.yml
:butterflytri: databaseurl-port: 127.0.0.1:3306 # 資料庫埠 database-name: student_db # 資料庫名 server: port: 8080 # 應用埠 servlet: context-path: /butterflytri # 應用映射 spring: application: name: mybatis # 應用名稱 datasource: url: jdbc:mysql://${butterflytri.databaseurl-port}/${butterflytri.database-name}?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver username: root password: root mybatis: type-aliases-package: org.wjf.entity # entity別名 mapper-locations: classpath:org/wjf/mapper/*Mapper.xml # mapper映射包掃描
啟動類
MybatisApplication.java
:package org.butterflytri; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author: WJF * @date: 2020/5/16 * @description: MybatisApplication */ @SpringBootApplication public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class,args); } }
-
2.2.2.2 創建實體類Student
在entity包下創建
Student.java
實體類。package org.butterflytri.entity; import lombok.Getter; import lombok.Setter; import lombok.ToString; import java.io.Serializable; /** * @author: WJF * @date: 2020/5/16 * @description: Student */ @ToString @Getter @Setter public class Student implements Serializable { private Long id; private String studentName; private String studentNo; private String sex; private Integer age; }
-
2.2.2.3 編寫mybatis介面文件
在mapper包下創建
StudentMapper.java
介面。package org.butterflytri.mapper; import org.apache.ibatis.annotations.Mapper; import org.butterflytri.entity.Student; import java.util.List; /** * @author: WJF * @date: 2020/5/16 * @description: */ @Mapper public interface StudentMapper { /** * 查詢所有學生信息 * @return List<Student> */ List<Student> findAll(); /** * 通過id查詢學生信息 * @param id * @return Student */ Student findOne(Long id); /** * 通過學號查詢學生信息 * @param studentNo * @return Student */ Student findByStudentNo(String studentNo); }
-
2.2.2.4 創建mybatis映射文件
在mapper包下創建
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.butterflytri.mapper.StudentMapper"> <sql id="propertyMapper"> `ID` AS id, `STUDENT_NAME` AS studentName, `STUDENT_NO` AS studentNo, `SEX` AS sex, `AGE` AS age </sql> <select id="findAll" resultType="org.butterflytri.entity.Student"> SELECT <include refid="propertyMapper"></include> FROM `t_student` </select> <select id="findOne" parameterType="java.lang.Long" resultType="org.butterflytri.entity.Student"> SELECT <include refid="propertyMapper"></include> FROM `t_student` WHERE `ID` = #{id} </select> <select id="findByStudentNo" parameterType="java.lang.String" resultType="org.butterflytri.entity.Student"> SELECT <include refid="propertyMapper"></include> FROM `t_student` WHERE `STUDENT_NO` = #{studentNo} </select> </mapper>
-
2.2.2.5 Service層
在service包下創建
StudentService.java
介面,在impl包下創建StudentServiceImpl.Java
類並繼承StudentService.Java
介面。StudentService.java
:package org.butterflytri.service; import org.butterflytri.entity.Student; import java.util.List; /** * @author: WJF * @date: 2020/5/16 * @description: StudentService */ public interface StudentService { public List<Student> findAll(); public Student findOne(Long id); public Student findByStudentNo(String studentNo); }
StudentServiceImpl.java
:package org.butterflytri.service.impl; import org.butterflytri.entity.Student; import org.butterflytri.mapper.StudentMapper; import org.butterflytri.service.StudentService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @author: WJF * @date: 2020/5/16 * @description: StudentServiceImpl */ @Service public class StudentServiceImpl implements StudentService { @Resource private StudentMapper studentMapper; @Override public List<Student> findAll() { return studentMapper.findAll(); } @Override public Student findOne(Long id) { return studentMapper.findOne(id); } @Override public Student findByStudentNo(String studentNo) { return studentMapper.findByStudentNo(studentNo); } }
-
2.2.2.6 Controller層
在controller包下創建
StudentController.java
控制器類。package org.butterflytri.controller; import org.butterflytri.entity.Student; import org.butterflytri.service.StudentService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; /** * @author: WJF * @date: 2020/5/16 * @description: StudentController */ @RestController @RequestMapping("/student") public class StudentController { @Resource private StudentService studentService; @RequestMapping("/findAll") public List<Student> findAll() { return studentService.findAll(); } @RequestMapping("/findOne") public Student findOne(Long id) { return studentService.findOne(id); } @RequestMapping("/findByStudentNo") public Student findByStudentNo(String studentNo) { return studentService.findByStudentNo(studentNo); } }
-
2.2.2.7 啟動工程,訪問路徑
啟動工程,訪問三個介面:
findAll
:findOne
:findByStudentNo
:
-
-
3. 項目地址
本項目傳送門:spring-boot-mybatis
此教程會一直更新下去,覺得博主寫的可以的話,關註一下,也可以更方便下次來學習。