SringBoot整合Mybatis-Plus其实并不难
先贴上我的项目路径,方便理解:
1、导入对应依赖
<!-- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
2、在pom文件中配置数据库mysql和mybatis-plus的信息
spring:
datasource:
# 这里配置自己的数据库信息
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis-plus 配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/**/*Mapper.xml
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
#
auto-mapping-behavior: full
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3、在启动类添加MapperScan包扫描dao层
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = {"com.example.demo.dao"}) //添加扫描dao层
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4、创建entity类
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("student")
public class Student {
/** 学号*/
private java.lang.Integer id;
/*** 学生姓名*/
private java.lang.String name;
/** 学生年龄*/
private java.lang.Integer age;
/** 成绩*/
private java.lang.Double scope;
}
5、创建dao层interface接口
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Student;
public interface StudentDao extends BaseMapper<Student> {
}
6、创建service以及它的实现类
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.Student;
public interface StudentService extends IService<Student> {
}
实现类:
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.StudentDao;
import com.example.demo.entity.Student;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao,Student> implements StudentService {
}
这个时候我们就已经有mybatis-plus自带的所有方法了,我们可以在controller测试一下:
7、创建测试controller层
这里我使用了QueryWrapper条件拼装查询器,并且用了IPage分页插件,小伙伴不清楚的话可以直接使用mybatis-plus。
package com.example.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import com.example.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RequestMapping("/hello")
@RestController
public class hello {
@Autowired
private StudentService studentService;
@RequestMapping("/list")
public Result<?> queryByList(Student student,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
QueryWrapper<Student> queryWrapper = new QueryWrapper(student);
Page page = new Page<Student>(pageNo,pageSize);
IPage<Student> pageList = studentService.page(page,queryWrapper);
return Result.success(pageList);
}
}
这边使用了我自己随便写的一个通用返回类,也一并放在这里,仅供参考:
package com.example.demo.util;
import lombok.Data;
@Data
public class Result<T> {
/** 返回标志*/
private boolean flag = true;
/** 返回代码 */
private java.lang.Integer code;
/** 返回信息 */
private java.lang.String massage;
/** 返回的数据对象*/
private T data;
/** 返回时间戳 */
private long timestamp = System.currentTimeMillis();
public Result(){}
public static Result<Object> success(){
Result<Object> r = new Result<>();
r.setFlag(true);
r.setCode(200);
r.setMassage("操作成功!");
return r;
}
public static Result<Object> error(){
Result<Object> r = new Result<>();
r.setFlag(false);
r.setCode(500);
r.setMassage("操作失败!");
return r;
}
public static Result<Object> success(Object data){
Result<Object> r = new Result<>();
r.setFlag(true);
r.setCode(200);
r.setData(data);
r.setMassage("操作成功!");
return r;
}
public static Result<Object> error(Object data){
Result<Object> r = new Result<>();
r.setFlag(false);
r.setCode(500);
r.setData(data);
r.setMassage("操作失败!");
return r;
}
}
有了controller层,我们就可以在postman测试一下自己写的接口,至此,SpringBoot整合mybatis-plus就完成了!
8、自定义xml文件编写sql语句
如果mybatis-plus自带的方法不能满足现有的业务,我们还可以创建xml文件,自己编写符合业务逻辑的sql语句。
在resources文件夹下创建mapper文件夹,里面编写xml文件,这里我创建了一个StudentMapper.xml(注意:这个 扫描mapper文件的配置在mybatis-plus配置文件里面可以配置)
比如我加一个业务,现在我要查询成绩最高的同学的姓名和成绩。
<?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.example.demo.dao.StudentDao">
<select id="queryNameByMaxScope" resultType="com.example.demo.entity.Student">
select name,scope from student order by scope desc limit 0,1
</select>
</mapper>
namespace对应的是dao层的interface接口。
在dao层的接口里面我们可以定义一下方法:
/**
* 查询出最高分的同学的信息
* @return
*/
Student queryNameByMaxScope();
接着在service里面同步定义一下方法的实现
接口类中:
/**
* 查询出最高分的同学的信息
* @return
*/
Student queryNameByMaxScope();
实现类中:
@Autowired
private StudentDao studentDao;
@Override
public Student queryNameByMaxScope() {
return this.studentDao.queryNameByMaxScope();
}
最后,我们就可以在controller中测试一下,写一个方法:
@GetMapping("/queryNameByMaxScope")
public Result<?> queryNameByMaxScope(){
Student student = studentService.queryNameByMaxScope();
return Result.success(student);
}
最后在postman测试一下接口。
至此,我们就实现了自定义xml编写sql文件,项目的结构在最开始的图上面全部展示。