SpringMVC集成MyBatis-Plus 3.x版本

Updated on in 编程技术 with 0 views and 0 comments

对于集成新版本的 MyBatis-Plus,Spring Boot 版本官方提供了详细的集成方案,但是 Spring MVC 版本显得相对来说就有点儿简单、不完善,毕竟官方推荐使用 Spring Boot 方式集成。研究了一下,做个记录。

1 介绍

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。具体的介绍不用多说,直接看官方文档介绍就行:https://mybatis.plus/guide/

这里集成版本使用的是 MyBatis-Plus 目前的最新版本:3.2.0 版本。

2.x 版本集成教程网上有很多,但是 2.x 版本的 JDK 最高只能 1.7

2 集成

2.1 要求

全新的 MyBatis-Plus 3.0 版本基于 JDK8,提供了 lambda 形式的调用,所以安装集成 MP3.0 要求如下:

  • JDK 8+

  • Maven or Gradle

IDE 这里选择的是 IntelliJ IDEA,其他也无所谓,大同小异。

Spring 版本这里使用的是 5.2.0.RELEASE

2.2 创建 maven 项目

创建项目比较简单,如图:

image.png

填写相关信息,完成创建。

这里给个创建项目的参考链接:https://blog.csdn.net/MaNongXf/article/details/83418353

看到输出 Hello World 页面这一步,项目创建就算是完成了。

在 main 下新建 Directory:Java,选中 Java 鼠标右键,选择如下:

image.png

同理创建 resources,test 两个文件夹,分别标记为 Resources Root 和 Test Sources Root

项目结构如下:

image.png

其中,Java > resources 下的 mapper 文件夹是用于存放自定义的 MyBatis 的 XML 文件

MyBatis 文件夹用于存放 MyBatis 的配置文件

2.3 配置文件

2.3.1 pom.xml

  1<?xml version="1.0" encoding="UTF-8"?>
  2
  3<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5    <modelVersion>4.0.0</modelVersion>
  6
  7    <groupId>com.ssm.plus</groupId>
  8    <artifactId>ssm-plus</artifactId>
  9    <version>1.0-SNAPSHOT</version>
 10    <packaging>war</packaging>
 11
 12    <name>ssm-plus Maven Webapp</name>
 13    <!-- FIXME change it to the project's website -->
 14    <url>http://www.example.com</url>
 15
 16    <properties>
 17        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18        <maven.compiler.source>1.8</maven.compiler.source>
 19        <maven.compiler.target>1.8</maven.compiler.target>
 20        <servlet.version>3.1.0</servlet.version>
 21        <jsp.version>2.3.1</jsp.version>
 22        <jstl.version>1.2</jstl.version>
 23        <mybatisplus.version>3.2.0</mybatisplus.version>
 24        <spring.version>5.2.0.RELEASE</spring.version>
 25        <mysql.version>5.1.40</mysql.version>
 26        <log4j.version>1.2.17</log4j.version>
 27        <lombok.version>1.18.10</lombok.version>
 28        <fastjson.version>1.2.62</fastjson.version>
 29        <dbcp2.version>2.6.0</dbcp2.version>
 30        <fileupload.version>1.3.3</fileupload.version>
 31        <io.version>2.6</io.version>
 32    </properties>
 33
 34    <dependencies>
 35        <dependency>
 36            <groupId>junit</groupId>
 37            <artifactId>junit</artifactId>
 38            <version>4.12</version>
 39            <scope>test</scope>
 40        </dependency>
 41        <!-- 加入servlet的依赖 -->
 42        <dependency>
 43            <groupId>javax.servlet</groupId>
 44            <artifactId>javax.servlet-api</artifactId>
 45            <version>${servlet.version}</version>
 46            <scope>provided</scope>
 47        </dependency>
 48        <!-- 加入jsp的依赖 -->
 49        <dependency>
 50            <groupId>javax.servlet.jsp</groupId>
 51            <artifactId>javax.servlet.jsp-api</artifactId>
 52            <version>${jsp.version}</version>
 53            <scope>provided</scope>
 54        </dependency>
 55        <!-- 加入jstl的依赖 -->
 56        <dependency>
 57            <groupId>javax.servlet</groupId>
 58            <artifactId>jstl</artifactId>
 59            <version>${jstl.version}</version>
 60        </dependency>
 61        <!-- 加入mybatis-plus的依赖 -->
 62        <dependency>
 63            <groupId>com.baomidou</groupId>
 64            <artifactId>mybatis-plus</artifactId>
 65            <version>${mybatisplus.version}</version>
 66        </dependency>
 67        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
 68        <dependency>
 69            <groupId>org.projectlombok</groupId>
 70            <artifactId>lombok</artifactId>
 71            <version>${lombok.version}</version>
 72            <scope>provided</scope>
 73        </dependency>
 74        <!-- 加入spring的依赖 -->
 75        <dependency>
 76            <groupId>org.springframework</groupId>
 77            <artifactId>spring-context</artifactId>
 78            <version>${spring.version}</version>
 79        </dependency>
 80        <dependency>
 81            <groupId>org.springframework</groupId>
 82            <artifactId>spring-test</artifactId>
 83            <version>${spring.version}</version>
 84        </dependency>
 85        <dependency>
 86            <groupId>org.springframework</groupId>
 87            <artifactId>spring-aspects</artifactId>
 88            <version>${spring.version}</version>
 89        </dependency>
 90        <dependency>
 91            <groupId>org.springframework</groupId>
 92            <artifactId>spring-aop</artifactId>
 93            <version>${spring.version}</version>
 94        </dependency>
 95        <dependency>
 96            <groupId>org.springframework</groupId>
 97            <artifactId>spring-core</artifactId>
 98            <version>${spring.version}</version>
 99        </dependency>
100        <dependency>
101            <groupId>org.springframework</groupId>
102            <artifactId>spring-expression</artifactId>
103            <version>${spring.version}</version>
104        </dependency>
105        <dependency>
106            <groupId>org.springframework</groupId>
107            <artifactId>spring-jdbc</artifactId>
108            <version>${spring.version}</version>
109        </dependency>
110        <dependency>
111            <groupId>org.springframework</groupId>
112            <artifactId>spring-tx</artifactId>
113            <version>${spring.version}</version>
114        </dependency>
115        <!-- 加入springmvc的依赖 -->
116        <dependency>
117            <groupId>org.springframework</groupId>
118            <artifactId>spring-webmvc</artifactId>
119            <version>${spring.version}</version>
120        </dependency>
121        <!-- 加入mysql的依赖 -->
122        <dependency>
123            <groupId>mysql</groupId>
124            <artifactId>mysql-connector-java</artifactId>
125            <version>${mysql.version}</version>
126        </dependency>
127        <!-- 加入log4j的依赖 -->
128        <dependency>
129            <groupId>log4j</groupId>
130            <artifactId>log4j</artifactId>
131            <version>${log4j.version}</version>
132        </dependency>
133        <!-- 加入dbcp2数据库依赖 -->
134        <dependency>
135            <groupId>org.apache.commons</groupId>
136            <artifactId>commons-dbcp2</artifactId>
137            <version>${dbcp2.version}</version>
138        </dependency>
139        <!-- 加入fileupload上传依赖 -->
140        <dependency>
141            <groupId>commons-fileupload</groupId>
142            <artifactId>commons-fileupload</artifactId>
143            <version>${fileupload.version}</version>
144        </dependency>
145        <!-- 加入io依赖 -->
146        <dependency>
147            <groupId>commons-io</groupId>
148            <artifactId>commons-io</artifactId>
149            <version>${io.version}</version>
150        </dependency>
151        <!-- FastJson -->
152        <dependency>
153            <groupId>com.alibaba</groupId>
154            <artifactId>fastjson</artifactId>
155            <version>${fastjson.version}</version>
156        </dependency>
157    </dependencies>
158
159    <build>
160        <finalName>ssm-plus</finalName>
161        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
162            <plugins>
163                <plugin>
164                    <artifactId>maven-clean-plugin</artifactId>
165                    <version>3.1.0</version>
166                </plugin>
167                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
168                <plugin>
169                    <artifactId>maven-resources-plugin</artifactId>
170                    <version>3.0.2</version>
171                </plugin>
172                <plugin>
173                    <artifactId>maven-compiler-plugin</artifactId>
174                    <version>3.8.0</version>
175                </plugin>
176                <plugin>
177                    <artifactId>maven-surefire-plugin</artifactId>
178                    <version>2.22.1</version>
179                </plugin>
180                <plugin>
181                    <artifactId>maven-war-plugin</artifactId>
182                    <version>3.2.2</version>
183                </plugin>
184                <plugin>
185                    <artifactId>maven-install-plugin</artifactId>
186                    <version>2.5.2</version>
187                </plugin>
188                <plugin>
189                    <artifactId>maven-deploy-plugin</artifactId>
190                    <version>2.8.2</version>
191                </plugin>
192            </plugins>
193        </pluginManagement>
194    </build>
195</project>
196

在普通 SSM 项目中,需要引入 mybtaismybtais-spring 两个依赖,这里将这两个依赖替换成 mybatis-plus 的依赖,mybatis-plus 会自动引入这两个依赖,故这里不用重复引入,以免冲突。

2.3.2 mybatis-config.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
 3        "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4<!--
 5 |   plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
 6 |   properties?, settings?,
 7 |   typeAliases?, typeHandlers?,
 8 |   objectFactory?,objectWrapperFactory?,
 9 |   plugins?,
10 |   environments?, databaseIdProvider?, mappers?
11 |-->
12<configuration>
13    <!--
14     | 全局配置设置
15     |
16     | 可配置选项                   默认值,     描述
17     |
18     | aggressiveLazyLoading       true,     当设置为‘true’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。
19     | multipleResultSetsEnabled   true,     允许和不允许单条语句返回多个数据集(取决于驱动需求)
20     | useColumnLabel              true,     使用列标签代替列名称。不同的驱动器有不同的作法。参考一下驱动器文档,或者用这两个不同的选项进行测试一下。
21     | useGeneratedKeys            false,    允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。
22     | autoMappingBehavior         PARTIAL,  指定MyBatis 是否并且如何来自动映射数据表字段与对象的属性。PARTIAL将只自动映射简单的,没有嵌套的结果。FULL 将自动映射所有复杂的结果。
23     | defaultExecutorType         SIMPLE,   配置和设定执行器,SIMPLE 执行器执行其它语句。REUSE 执行器可能重复使用prepared statements 语句,BATCH执行器可以重复执行语句和批量更新。
24     | defaultStatementTimeout     null,     设置一个时限,以决定让驱动器等待数据库回应的多长时间为超时
25     | -->
26    <settings>
27        <!-- 这个配置使全局的映射器启用或禁用缓存 -->
28        <setting name="cacheEnabled" value="true"/>
29        <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载 -->
30        <setting name="lazyLoadingEnabled" value="true"/>
31        <setting name="multipleResultSetsEnabled" value="true"/>
32        <setting name="useColumnLabel" value="true"/>
33        <setting name="defaultExecutorType" value="REUSE"/>
34        <setting name="defaultStatementTimeout" value="25000"/>
35    </settings>
36
37</configuration>

2.3.3 spring.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans xmlns="http://www.springframework.org/schema/beans"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4       xmlns:context="http://www.springframework.org/schema/context"
 5       xmlns:util="http://www.springframework.org/schema/util"
 6       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
 7
 8    <!-- 引入属性文件 -->
 9    <context:property-placeholder location="classpath:jdbc.properties"/>
10    <util:properties id="configProperties" location="classpath:jdbc.properties" />
11
12    <!-- Service包(自动注入) -->
13    <context:component-scan base-package="com.ssm.plus.service"/>
14
15    <import resource="classpath:spring/spring-mybatis.xml"/>
16</beans>

2.3.4 spring-mvc.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans xmlns="http://www.springframework.org/schema/beans"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4       xmlns:context="http://www.springframework.org/schema/context"
 5       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 6
 7    <mvc:default-servlet-handler/>
 8
 9    <!-- Controller包(自动注入) -->
10    <context:component-scan base-package="com.ssm.plus.controller"/>
11
12    <!-- 注解映射器,适配器 -->
13    <mvc:annotation-driven >
14        <mvc:message-converters register-defaults="true">
15            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
16            <!-- FastJson -->
17            <bean id="fastJsonHttpMessageConverter"
18                  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
19                <property name="supportedMediaTypes">
20                    <list>
21                        <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
22                        <value>text/html;charset=UTF-8</value>
23                        <value>application/json;charset=UTF-8</value>
24                    </list>
25                </property>
26                <property name="features">
27                    <array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
28                        <!-- 避免循环引用 -->
29                        <value>DisableCircularReferenceDetect</value>
30                        <!-- 是否输出值为null的字段 -->
31                        <value>WriteMapNullValue</value>
32                        <!-- 数值字段如果为null,输出为0,而非null -->
33                        <value>WriteNullNumberAsZero</value>
34                        <!-- 字符类型字段如果为null,输出为"",而非null  -->
35                        <value>WriteNullStringAsEmpty</value>
36                        <!-- List字段如果为null,输出为[],而非null -->
37                        <value>WriteNullListAsEmpty</value>
38                        <!-- Boolean字段如果为null,输出为false,而非null -->
39                        <value>WriteNullBooleanAsFalse</value>
40                    </array>
41                </property>
42            </bean>
43        </mvc:message-converters>
44    </mvc:annotation-driven>
45
46    <!-- 资源映射 -->
47    <mvc:resources location="/resources/" mapping="/resources/**" />
48
49    <!-- 视图解析器 -->
50    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
51        <property name="prefix" value="/WEB-INF/jsp/" />
52        <property name="suffix" value=".jsp" />
53    </bean>
54
55    <!-- 定义文件上传解析器 -->
56    <bean id="multipartResolver"
57          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
58        <!-- 设定默认编码 -->
59        <property name="defaultEncoding" value="UTF-8"/>
60        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
61        <property name="maxUploadSize" value="5242880"/>
62    </bean>
63
64    <!-- 拦截器 -->
65    <!--<mvc:interceptors>
66        &lt;!&ndash; 多个拦截器,顺序执行
67        用户身份校验的拦截器 &ndash;&gt;
68        <mvc:interceptor>
69            <mvc:mapping path="/admin/**" />
70            <bean class="com.ssm.plus.interceptor.LoginInterceptor"></bean>
71        </mvc:interceptor>
72
73    </mvc:interceptors>-->
74</beans>

2.3.5 spring-mybatis.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans xmlns="http://www.springframework.org/schema/beans"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 4       xmlns:aop="http://www.springframework.org/schema/aop"
 5       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
 6
 7    <!-- 数据库连接池 -->
 8    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
 9        <property name="driverClassName" value="${jdbc.driver}" />
10        <property name="url" value="${jdbc.url}" />
11        <property name="username" value="${jdbc.username}" />
12        <property name="password" value="${jdbc.password}" />
13        <property name="maxTotal" value="10" />
14        <property name="minIdle" value="5" />
15        <property name="initialSize" value="5" />
16    </bean>
17
18    <!-- Spring整合Mybatis,更多查看文档:http://mp.baomidou.com -->
19    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
20        <property name="dataSource" ref="dataSource"/>
21        <!-- 自动扫描Mapping.xml文件 -->
22        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
23        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
24        <property name="typeAliasesPackage" value="com.ssm.plus.entity.*"/>
25        <!-- 分页 -->
26        <property name="plugins">
27            <array>
28                <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor" />
29            </array>
30        </property>
31        <!-- 全局配置注入 -->
32        <property name="globalConfig" ref="globalConfig" />
33    </bean>
34    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
35        <property name="dbConfig" ref="dbConfig"/>
36    </bean>
37    <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
38        <property name="keyGenerator" ref="keyGenerator"/>
39    </bean>
40    <bean id="keyGenerator" class="com.baomidou.mybatisplus.extension.incrementer.DB2KeyGenerator"/>
41
42    <!-- MyBatis 动态扫描  -->
43    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
44        <property name="basePackage" value="com.ssm.plus.mapper"/>
45    </bean>
46
47    <!-- 配置事务管理 -->
48    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
49        <property name="dataSource" ref="dataSource"/>
50    </bean>
51
52    <!-- 事务管理 属性 -->
53    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
54        <tx:attributes>
55            <tx:method name="add*" propagation="REQUIRED"/>
56            <tx:method name="append*" propagation="REQUIRED"/>
57            <tx:method name="save*" propagation="REQUIRED"/>
58            <tx:method name="update*" propagation="REQUIRED"/>
59            <tx:method name="modify*" propagation="REQUIRED"/>
60            <tx:method name="edit*" propagation="REQUIRED"/>
61            <tx:method name="insert*" propagation="REQUIRED"/>
62            <tx:method name="delete*" propagation="REQUIRED"/>
63            <tx:method name="remove*" propagation="REQUIRED"/>
64            <tx:method name="repair" propagation="REQUIRED"/>
65
66            <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
67            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
68            <tx:method name="load*" propagation="REQUIRED" read-only="true"/>
69            <tx:method name="search*" propagation="REQUIRED" read-only="true"/>
70            <tx:method name="datagrid*" propagation="REQUIRED" read-only="true"/>
71
72            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
73        </tx:attributes>
74    </tx:advice>
75
76    <!-- 配置切面 -->
77    <aop:config>
78        <aop:pointcut id="transactionPointcut" expression="execution(* com.ssm.plus.service..*.*(..))"/>
79        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/>
80    </aop:config>
81</beans>

2.3.6 web.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 4
 5  <display-name>ssm-plus</display-name>
 6
 7  <!-- 加载Spring配置文件 -->
 8  <context-param>
 9    <param-name>contextConfigLocation</param-name>
10    <param-value>classpath:spring/spring.xml</param-value>
11  </context-param>
12
13  <!-- 字符集 过滤器 -->
14  <filter>
15    <filter-name>encodingFilter</filter-name>
16    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
17    <init-param>
18      <param-name>encoding</param-name>
19      <param-value>UTF-8</param-value>
20    </init-param>
21  </filter>
22  <filter-mapping>
23    <filter-name>encodingFilter</filter-name>
24    <url-pattern>/*</url-pattern>
25  </filter-mapping>
26
27  <!-- Spring监听器 -->
28  <listener>
29    <description>Spring监听器</description>
30    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
31  </listener>
32  <!-- 防止Spring内存溢出监听器 -->
33  <listener>
34    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
35  </listener>
36
37  <!-- Spring MVC -->
38  <servlet>
39    <servlet-name>SpringMVC</servlet-name>
40    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
41    <init-param>
42      <description>SpringMVC</description>
43      <param-name>contextConfigLocation</param-name>
44      <param-value>classpath:spring/spring-mvc.xml</param-value>
45    </init-param>
46    <load-on-startup>1</load-on-startup>
47  </servlet>
48  <servlet-mapping>
49    <servlet-name>SpringMVC</servlet-name>
50    <url-pattern>/</url-pattern>
51  </servlet-mapping>
52
53  <!-- Session超时时间 -->
54  <session-config>
55    <session-timeout>15</session-timeout>
56  </session-config>
57</web-app>

2.3.7 jdbc.properties

1jdbc.driver=com.mysql.jdbc.Driver
2jdbc.url=jdbc:mysql://localhost:3306/db_ssmp?useUnicode=true&characterEncoding=utf-8&useSSL=false
3jdbc.username=
4jdbc.password=

2.3.8 log4j.properties

1# Global logging configuration
2log4j.rootLogger=DEBUG, stdout
3# MyBatis logging configuration...
4log4j.logger.org.mybatis.example.BlogMapper=TRACE
5# Console output...
6log4j.appender.stdout=org.apache.log4j.ConsoleAppender
7log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

2.4 测试

2.4.1 实体类 Admin.java

 1package com.ssm.plus.entity;
 2import com.baomidou.mybatisplus.annotation.TableName;
 3import lombok.Data;
 4
 5/**
 6 * Created by kangaroo on 2019/11/18 21:16.
 7 * QQ: 326170945
 8 * Description:
 9 */
10@Data
11@TableName("t_admin")
12public class Admin {
13    private long id;
14    private String name;
15    private String username;
16    private String password;
17}
  • 注:
  • @Data 注解是 lombok 提供的,需要开发工具安装 lombok 的插件才能使用,如果没有安装,就删除这个注解,手动生成 getter 和 setter 方法,并在 pom.xml 中删除 lombok 的依赖。
  • @TableName 注解是 mybatis-plus 提供的,用于指定数据库表名,默认实体类名。具体注解,可以参考官方文档。

2.4.2 dao 层接口 AdminMapper.java

 1package com.ssm.plus.mapper;
 2import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 3import com.ssm.plus.entity.Admin;
 4
 5/**
 6 * Created by kangaroo on 2019/11/18 21:15.
 7 * QQ: 326170945
 8 * Description:
 9 */
10public interface AdminMapper extends BaseMapper<Admin> {
11}

2.4.3 测试方法类 MpTest.java

 1package com.ssm.plus.test;
 2
 3import com.ssm.plus.entity.Admin;
 4import com.ssm.plus.mapper.AdminMapper;
 5import org.junit.Test;
 6import org.junit.runner.RunWith;
 7import org.springframework.beans.factory.annotation.Autowired;
 8import org.springframework.test.context.ContextConfiguration;
 9import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10
11import java.util.List;
12
13/**
14 * Created by kangaroo on 2019/11/18 19:52.
15 * QQ: 326170945
16 * Description:
17 */
18@RunWith(SpringJUnit4ClassRunner.class)
19@ContextConfiguration(locations = {"classpath*:spring/spring.xml"})
20public class MpTest {
21
22    @Autowired
23    private AdminMapper adminMapper;
24
25    @Test
26    public void select(){
27        System.out.println("查询测试:");
28        List<Admin> adminList = adminMapper.selectList(null);
29        System.out.println(adminList.size());
30        adminList.forEach(System.out::println);
31    }
32}

测试结果:

image.png

3 Demo 下载

demo 下载地址:https://github.com/kangaroo1122/ssm-plus


标题:SpringMVC集成MyBatis-Plus 3.x版本
作者:kangaroo1122
地址:https://www.kangaroohy.com/articles/2019/11/18/1574088540500.html
声明:如非特别说明,版权归kangaroo1122 所有,转载请注明出处,谢谢!
签名:No pains, no gains.