[Mybatis-Plus] 实现SpringBoot集成Mybatis-Plus

实现SpringBoot集成Mybatis-Plus

项目环境

  1. MyBatis-Plus版本:3.1.2
  2. SpringBoot版本:2.1.0
  3. JDK版本:1.8

完整的pom.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>spring-boot-mybatis-plus</artifactId>
	<version>1.0.0</version>
	<packaging>jar</packaging>

	<name>Spring Boot MyBatis Plus</name>
	<description>Spring Boot 2 Demo</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.1.2</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-core</artifactId>
			<version>3.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
			<version>2.1.0</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.17</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.4.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.4.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>com.sun.xml.bind</groupId>
			<artifactId>jaxb-impl</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>com.sun.xml.bind</groupId>
			<artifactId>jaxb-core</artifactId>
			<version>2.3.0</version>
		</dependency>
		<dependency>
			<groupId>javax.activation</groupId>
			<artifactId>activation</artifactId>
			<version>1.1.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

配置文件内容application.yml

(其中,127.0.0.1为数据库所在IP地址,3306为端口号,manager_db为数据库名,username对应数据库用户名,password对应数据库mima,按实际情况填写即可)

# DataSource Config
spring:
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/manager_db?useUnicode=true&characterEncoding=utf8
        username: root
        password: 123456

# Logger Config
logging:
    level:
      com.neo: debug

数据库表结构

CREATE TABLE `users`  (
  `id` int(11) NOT NULL,
  `name` varchar(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `id`(`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;

数据库表数据

INSERT INTO `manager_db`.`users`(`id`, `name`, `age`) VALUES (1, 'xiaoming', 21);
INSERT INTO `manager_db`.`users`(`id`, `name`, `age`) VALUES (2, 'xiaodong', 22);
INSERT INTO `manager_db`.`users`(`id`, `name`, `age`) VALUES (3, 'mayun', 28);
INSERT INTO `manager_db`.`users`(`id`, `name`, `age`) VALUES (4, 'huateng', 21);
INSERT INTO `manager_db`.`users`(`id`, `name`, `age`) VALUES (5, 'xiaohong', 24);
INSERT INTO `manager_db`.`users`(`id`, `name`, `age`) VALUES (6, 'liming', 18);
INSERT INTO `manager_db`.`users`(`id`, `name`, `age`) VALUES (7, 'meiling', 20);

在项目下新建mapper和entity目录

(建完目录之后,分别在entity和mapper下创建UsersMapper接口和Users类)

在这里插入图片描述

新建UsersMapper接口

由于BaseMapper集成了基础的CRUD方法,因此这里我们不用去写XML文件,直接将其作为基类使用即可(此处可能会产生疑问:为啥BaseMapper中方法在一个没实现的情况下,却还能正常调用,why?————其实此处用到了动态代理的知识,有兴趣的可以去了解下);

package com.myplus.entity;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.myplus.mapper.Users;

public interface UsersMapper extends BaseMapper<Users> {
}

新建Users类

这里我们使用注解的方式去实现get和set方法,@TableName(“users”)注解参数为表名,@TableId用来标识主键;

package com.myplus.mapper;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@TableName("users")
public class Users {

    @TableId
    private Integer id;

    private String name;

    private Integer age;
}

在test.java.com目录下新建测试类

package com.myplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myplus.entity.UsersMapper;
import com.myplus.mapper.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MypTest {

    @Autowired
    private UsersMapper usersMapper;

    @Test
    public void testSelect() {
        List<Users> userList = usersMapper.selectList(new QueryWrapper<>());
        for (Users users : userList){
            System.out.println(users.getId() + " " + users.getName() + " " + users.getAge());
        }
    }
}

执行测试方法,验证是否配置成功

在这里插入图片描述

(由上图的执行日志可知,已完成二者的集成工作)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值