SpringBoot怎么整合mybatis+mybatis-plus
短信预约 -IT技能 免费直播动态提醒
本文小编为大家详细介绍“SpringBoot怎么整合mybatis+mybatis-plus”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot怎么整合mybatis+mybatis-plus”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
准备工作
准备如下一个数据表
CREATE TABLE `student` ( `id` varchar(32) NOT NULL, `gender` varchar(32) DEFAULT NULL, `age` int(12) DEFAULT NULL, `nick_name` varchar(32) DEFAULT NULL, `name` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入几条测试数据
整合步骤
工程的完整包结构如图所示
导入maven依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <mysql-connector-java.version>8.0.11</mysql-connector-java.version> <commons-lang3.version>3.7</commons-lang3.version> <fastjson.version>1.2.47</fastjson.version> <mybatis-plus-boot-starter.version>3.3.0</mybatis-plus-boot-starter.version> <mybatis-plus-generator.version>3.3.0</mybatis-plus-generator.version> <druid.version>1.1.14</druid.version> <lombok.version>1.18.0</lombok.version> <dubbo-spring-boot-starter.version>2.0.0</dubbo-spring-boot-starter.version> <swagger.version>2.9.2</swagger.version> <swagger-bootstrap-ui.version>1.9.6</swagger-bootstrap-ui.version> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mysql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> <!--阿里巴巴fastjosn依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!--阿里巴巴数据库连接池依赖--> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <!-- MyBatis增强工具--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus-boot-starter.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus-generator.version}</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!--swagger-ui--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>${swagger-bootstrap-ui.version}</version> </dependency> </dependencies>
配置文件 application.yml
server: port: 8083logging: config: classpath:logback-spring.xml #日志spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://IP:3306/school?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false username: root password: root druid: max-active: 100 initial-size: 10 max-wait: 60000 min-idle: 5 #设置单个文件最大上传大小 servlet: multipart: max-file-size: 20MBmybatis-plus: mapper-locations: classpath*:mapper@Configuration@EnableSwagger2public class ApiSwagger2 { @Bean public Docket createRestBmbsApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("users") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.congge.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("后端相关API") .version("1.0") .build(); } }
实体类
import com.baomidou.mybatisplus.annotation.TableField;import lombok.Data; @Datapublic class Student { @TableField("id") private String id; @TableField("name") private String name; @TableField("gender") private String gender; @TableField("age") private int age; @TableField("nick_name") private String nickName; }
dao接口,里面添加一个查询所有数据的方法
import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.congge.entity.Student;import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapperpublic interface StudentMapper extends BaseMapper<Student> { List<Student> queryAll(); }
mybatis层,写sql的文件
<?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.congge.dao.StudentMapper"> <select id="queryAll" resultType="com.congge.entity.Student">select * from student</select> </mapper>
业务实现类
在本次的业务实现中,同时可以使用mybatis的方式以及mybatis-plus的方式进行,具体使用的时候结合自身的需求进行选择;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.congge.dao.StudentMapper;import com.congge.entity.Student;import com.congge.service.StudentService;import org.springframework.stereotype.Service; import javax.annotation.Resource;import java.util.List; @Servicepublic class StudentServiceImpl implements StudentService { @Resource private StudentMapper studentMapper; @Override public List<Student> queryAllStudent() { QueryWrapper<Student> queryWrapper = new QueryWrapper(); List<Student> students = studentMapper.selectList(queryWrapper); return students; //return studentMapper.queryAll(); } @Override public List<Student> getByName(String name) { QueryWrapper<Student> queryWrapper = new QueryWrapper(); queryWrapper.like("name",name); return studentMapper.selectList(queryWrapper); } public Student getById(String id) { QueryWrapper<Student> queryWrapper = new QueryWrapper(); queryWrapper.like("id",id); return studentMapper.selectOne(queryWrapper); }}
添加一个测试接口
@RestControllerpublic class StudentController { @Autowired private StudentService studentService; @GetMapping("/getAll") public List<Student> getAll(){ return studentService.queryAllStudent(); } @GetMapping("/getByName") public List<Student> getByName(@RequestParam String name){ return studentService.getByName(name); } @GetMapping("/getById") public Student getById(@RequestParam String id){ return studentService.getById(id); }}
启动类
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublic class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } }
接下来,将工程运行起来做一下测试吧
启动之后,打开swagger界面
不妨随机测试两个接口吧,测试下获取所有学生的数据接口
读到这里,这篇“SpringBoot怎么整合mybatis+mybatis-plus”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341