MyBatis-Plus 快速入门-SpringMVC(一)

MyBatis-Plus 快速开始示例(Spring MVC 版本)

掌握程度

我们将通过一个简单的 Demo 来阐述 MyBatis-Plus 的强大功能,在此之前,我们假设您已经:

  • 拥有 Java 开发环境以及相应 IDE
  • 熟悉 SpringBoot
  • 熟悉 Maven

脚本初始化

数据库 Schema-h2.sql 脚本如下:

1
2
3
4
5
6
7
8
9
10
DROP TABLE IF EXISTS user;

CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);

其对应的数据库 Data-h2.sql 脚本如下:

1
2
3
4
5
6
7
8
DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

初始化工程

可以使用 Spring Initializr 快速初始化一个 Spring Boot 工程

创建一个空的 Spring Boot 工程(工程将以 H2 作为默认数据库进行演示)

添加依赖

引入 mybatis-plus-boot-starter、lombok、h2 等依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!--添加h2、 lombok、 Mybatis-Plus 等依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>

创建实体类

编写实体类 User.java(此处使用了 Lombok 简化代码)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.ray.mybatisplusstudyquickstartspringmvc.entity;

import lombok.Data;

/**
* @author Ray
* @date 2018/9/5 0005
* 用户实体类
*/
@Data
public class User {

private Long id;
private String name;
private Integer age;
private String email;
}

创建数据访问层

编写Mapper类 UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.ray.mybatisplusstudyquickstartspringmvc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ray.mybatisplusstudyquickstartspringmvc.entity.User;

/**
* @author Ray
* @date 2018/9/5 0005
* 数据访问层
*/
public interface UserMapper extends BaseMapper<User> {
}

SpringMVC

演示 SpringMVC 方式使用Mybatis-Plus

项目结构

H2 数据库的相关配置

config.properties

1
2
3
h2.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:db/schema-h2.sql'\\;RUNSCRIPT FROM 'classpath:db/data-h2.sql'
h2.username=root
h2.password=root

注意:\\(必须)

Spring配置文件

spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--1.Import Properties-->
<context:property-placeholder location="config.properties" />

<!--2.H2 Data Source Config-->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="jdbcUrl" value="${h2.url}" />
<property name="username" value="${h2.username}" />
<property name="password" value="${h2.password}" />
</bean>

<!--3.SqlSessionFactory Config-->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>

<!--4.MyBatis Mapper Scan Config-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ray.mybatisplusquickstart.mapper"/>
</bean>
</beans>

测试类

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.ray.mybatisplusstudyquickstartspringmvc;

import com.ray.mybatisplusstudyquickstartspringmvc.entity.User;
import com.ray.mybatisplusstudyquickstartspringmvc.mapper.UserMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath*:spring.xml"})
public class MybatisPlusStudyQuickstartSpringmvcApplicationTests {

@Autowired
private UserMapper userMapper;

@Test
public void testSelect(){
System.out.println("----- selectAll method test -----");
// UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper,所以不填写(null)就是无任何条件
List<User> list = userMapper.selectList(null);
Assert.assertEquals(5, list.size());
list.forEach(System.out::println);
}

}

测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
----- selectAll method test -----
16:32:15.141 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
16:32:15.160 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c5474f5] was not registered for synchronization because synchronization is not active
16:32:15.229 [main] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
16:32:15.229 [main] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [HikariProxyConnection@1888924788 wrapping conn0: url=jdbc:h2:mem:test user=ROOT] will not be managed by Spring
16:32:15.230 [main] DEBUG com.ray.mybatisplusstudyquickstartspringmvc.mapper.UserMapper.selectList - ==> Preparing: SELECT id,name,age,email FROM user
16:32:15.261 [main] DEBUG com.ray.mybatisplusstudyquickstartspringmvc.mapper.UserMapper.selectList - ==> Parameters:
16:32:15.285 [main] DEBUG com.ray.mybatisplusstudyquickstartspringmvc.mapper.UserMapper.selectList - <== Total: 5
16:32:15.285 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4c5474f5]
16:32:15.285 [main] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
-------------- 本文结束  感谢您的阅读 --------------
0%