springboot 整合mybatis-plus mysql
阿新 • • 發佈:2019-05-03
【前言】注意mybatis-plus版本如果和springboot 版本有關係,我當時用的springboot-2.1.4.RELEASE版,mybatis-plus用的3.1.1版,直接翻車,讓我懷疑人生了都,後來springboot-2.0.5.RELEASE用了這版才可以。。。
其它的配置就很簡單了,簡單說下,主要是版本問題,用mybatis-plus的原因是因為簡單開發的時候無需引入xml,這一點比較贊,也支援xml共存,在xml中自定義方法。簡單的單標增刪改就很方便了,無需xml了
工程目錄結構如下
1.IDEA新建springboot工程,pom如下
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demoplus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demoplus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.application.yml配置mysql連結
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/data
username: root
password: hello
3.然後就是常規的實體類
package com.example.entity; public class Admin { private Long id; private String name; private Integer age; private String email; }
4.dao
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.Admin;
import java.util.List;
public interface AdminMapper extends BaseMapper<Admin> {
//結合xml中自定義方法
List<Admin> list();
}
5.測試類
package com.example;
import com.example.entity.Admin;
import com.example.mapper.AdminMapper;
import org.junit.Assert;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoplusApplicationTests {
@Autowired
private AdminMapper adminMapper;
//呼叫 mybatis-plus 自帶查詢方法
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<Admin> userList = adminMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
//這個是引入xml的自定義方法,xml檔案不是必須
@Test
public void testSelect2() {
System.out.println(("----- select222All method test ------"));
List<Admin> userList = adminMapper.list();
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}