<?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.xm</groupId>
<artifactId>Springboot_MybatisPlus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Springboot_MybatisPlus</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加MP依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3</version>
</dependency> <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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
开发工具:STS
代码下载链接:https://github.com/theIndoorTrain/Springboot_MybatisPlus
版本:
Springboot:1.5.14.RELEASE
使用2.0以上的Springboot,会报出一些异常。欢迎知道异常原因的大牛解惑。
MybatisPlus:2.3
参考文档:MybatisPlus官网
前言:
Mybatis是一款灵活的持久层框架,它解放了sql,把sql的控制完全交于开发者,保证了sql的灵活性。
MP(MybatisPlus)是对Mybatis的增强,它把通用的mapper帮我们实现好了,我们直接调用就可以。
它还提供了条件构造器、强大的代码生成、各种优秀的插件......
都会给我们的开发带来便利。
现在,我们开始使用MP。
搭建项目:
1.添加依赖
<?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.xm</groupId>
<artifactId>Springboot_MybatisPlus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Springboot_MybatisPlus</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加MP依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3</version>
</dependency> <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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2.建表
3.配置数据库连接:
spring:
datasource:
url: jdbc:mysql://10.1.51.31:3306/xm?useSSL=true
username: root
password: cube1501
driver-class-name: com.mysql.jdbc.Drive
4.添加Student实体:
package com.xm.pojo;
/**
* 学生实体
* @author xm
*
*/
public class Student { private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
} }
5.添加mapper接口:
package com.xm.mapper; import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.xm.pojo.Student; public interface StudentMapper extends BaseMapper<Student> { }
6.开启Mapper扫描:
package com.xm; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
//开启mapper包扫描
@MapperScan("com.xm.mapper")
public class SpringbootMybatisPlusApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
}
}
7.测试:
package com.xm; 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 com.xm.mapper.StudentMapper; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisPlusApplicationTests {
@Autowired
private StudentMapper studentMapper; @Test
public void contextLoads() {
System.out.println(studentMapper.selectById(1));
} }
8.运行结果
2018-07-19