1 24 4.0.0 5 6com.xm 7Springboot_MybatisPlus 80.0.1-SNAPSHOT 9jar 10 11Springboot_MybatisPlus 12Demo project for Spring Boot 13 1415 20 21org.springframework.boot 16spring-boot-starter-parent 171.5.14.RELEASE 1819 22 26 27UTF-8 23UTF-8 241.8 2528 54 5529 32org.springframework.boot 30spring-boot-starter-jdbc 3133 36 37org.springframework.boot 34spring-boot-starter-web 3538 42 43com.baomidou 39mybatis-plus-boot-starter 402.3 4144 48mysql 45mysql-connector-java 46runtime 4749 53org.springframework.boot 50spring-boot-starter-test 51test 5256 63 64 6557 6258 61org.springframework.boot 59spring-boot-maven-plugin 60
开发工具:STS
代码下载链接:
版本:
Springboot:1.5.14.RELEASE
使用2.0以上的Springboot,会报出一些异常。欢迎知道异常原因的大牛解惑。
MybatisPlus:2.3
参考文档:
前言:
Mybatis是一款灵活的持久层框架,它解放了sql,把sql的控制完全交于开发者,保证了sql的灵活性。
MP(MybatisPlus)是对Mybatis的增强,它把通用的mapper帮我们实现好了,我们直接调用就可以。
它还提供了条件构造器、强大的代码生成、各种优秀的插件......
都会给我们的开发带来便利。
现在,我们开始使用MP。
搭建项目:
1.添加依赖
1 24 4.0.0 5 6com.xm 7Springboot_MybatisPlus 80.0.1-SNAPSHOT 9jar 10 11Springboot_MybatisPlus 12Demo project for Spring Boot 13 1415 20 21org.springframework.boot 16spring-boot-starter-parent 171.5.14.RELEASE 1819 22 26 27UTF-8 23UTF-8 241.8 2528 54 5529 32org.springframework.boot 30spring-boot-starter-jdbc 3133 36 37org.springframework.boot 34spring-boot-starter-web 3538 42 43com.baomidou 39mybatis-plus-boot-starter 402.3 4144 48mysql 45mysql-connector-java 46runtime 4749 53org.springframework.boot 50spring-boot-starter-test 51test 5256 63 64 6557 6258 61org.springframework.boot 59spring-boot-maven-plugin 60
2.建表
3.配置数据库连接:
1 spring:2 datasource:3 url: jdbc:mysql://10.1.51.31:3306/xm?useSSL=true4 username: root5 password: cube15016 driver-class-name: com.mysql.jdbc.Drive
4.添加Student实体:
1 package com.xm.pojo; 2 /** 3 * 学生实体 4 * @author xm 5 * 6 */ 7 public class Student { 8 9 private Integer id;10 private String name;11 public Integer getId() {12 return id;13 }14 public void setId(Integer id) {15 this.id = id;16 }17 public String getName() {18 return name;19 }20 public void setName(String name) {21 this.name = name;22 }23 24 @Override25 public String toString() {26 return "Student [id=" + id + ", name=" + name + "]";27 }28 29 30 31 }
5.添加mapper接口:
package com.xm.mapper;import com.baomidou.mybatisplus.mapper.BaseMapper;import com.xm.pojo.Student;public interface StudentMapper extends BaseMapper{}
6.开启Mapper扫描:
1 package com.xm; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 //开启mapper包扫描 9 @MapperScan("com.xm.mapper")10 public class SpringbootMybatisPlusApplication {11 12 public static void main(String[] args) {13 SpringApplication.run(SpringbootMybatisPlusApplication.class, args);14 }15 }
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)@SpringBootTestpublic class SpringbootMybatisPlusApplicationTests { @Autowired private StudentMapper studentMapper; @Test public void contextLoads() { System.out.println(studentMapper.selectById(1)); }}
8.运行结果
2018-07-19