MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
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
:
本项目传送门:spring-boot-mybatis
此教程会一直更新下去,觉得博主写的可以的话,关注一下,也可以更方便下次来学习。
原文:https://www.cnblogs.com/Butterfly-Tri/p/12902945.html