Before the order
MybatisPlus general CRUD interface MybatisPlus conditional constructor MybatisPlus code generator tutorial details The configuration of MybatisPlus
1. Spring integration with MP
1.1 Creating a Project
1.2 Importing Dependencies
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.1.6. RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<! -- Mybatis - Plus plugin dependencies -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.0</version>
</dependency>
<! --Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<! -- Connection pool -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<! A toolkit for simplifying bean code
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Copy the code
1.3 write JDBC properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=JDBC: mysql: / / 127.0.0.1:3306 / mp? serverTimezone=GMT%2B8&useSSL=false
jdbc.username=root
jdbc.password=root
Copy the code
1.4 Compiling mybatis configuration file sqlmapconfig.xml
<! DOCTYPEconfiguration
PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
</plugin>
</plugins>
</configuration>
Copy the code
1.5 Writing the Spring configuration file applicationContext.xml
<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">
<!-- 引入properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<! -- Data source -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<! Create sqlSessionFactory ();
<! SqlSessionFactory = sqlSessionFactory = sqlSessionFactory = sqlSessionFactory = sqlSessionFactory
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<! --2. Mapper mapping -->
<! -- Scan mapper interface, using mybatis native scanner -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mybatis.mapper"/>
</bean>
</beans>
Copy the code
1.6 Writing the User object and UserMapper interface
@Data // getter setter toString
@NoArgsConstructor // Generate a no-parameter construct
@AllArgsConstructor // Generate a full-parameter construct
@TableName("user")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
Copy the code
public interface UserMapper extends BaseMapper<User> {
List<User> findAll(a);
}
Copy the code
1.7 Writing Test Cases
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestSpringMP {
@Autowired
private UserMapper userMapper;
@Test
public void test(a){
List<User> users = userMapper.selectList(null);
for(User user : users) { System.out.println(user); }}}Copy the code
At this point, Spring integration of Mybatis-Plus basic process is completed.
2. SpringBoot integrates MP
2.1 Creating a SpringBoot Project
2.2 Importing Dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<! -- Simplify code toolkit -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<! -- Springboot support for Mybatis -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<! - mysql driver - >
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Copy the code
2.3 the log4j properties
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
Copy the code
2.4 written application. The properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=JDBC: mysql: / / 127.0.0.1:3306 / mp? useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
Copy the code
2.5 Write the User object and UserMapper interface
@Data // getter setter toString
@NoArgsConstructor // Generate a no-parameter construct
@AllArgsConstructor // Generate a full-parameter construct
@TableName("tb_user")
public class User {}Copy the code
public interface UserMapper extends BaseMapper<User>{}Copy the code
2.6 Writing startup classes
@MapperScan("com.mybatis.mp.mapper") // Set scan packets for the mapper interface
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }}Copy the code
2.7 Writing test cases
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect(a) {
List<User> userList = userMapper.selectList(null);
for(User user : userList) { System.out.println(user); }}}Copy the code
2.8 the results
This concludes the SpringBoot MybatisPlus tutorial