项目结构:
集成mybatis-plus的坑挺多的,算是踩了一遍。
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 https://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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>MPTest</groupId>
<artifactId>mybatistest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatistest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<repositories><!-- 代码库 -->
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mp 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
坑1:首先用idea默认的远程仓库下载的jar都有问题,将远程仓库地址改为阿里云的。
坑2:mybatis-plus不能和mybatis并存。
坑3:有一个类似的mybatis-plus包,但不是支持springboot的。
数据库:
接下来是代码:
1.entity:
@TableName("user_test")对应的是数据库的表名,属性对应字段
@TableId对应主键,value为字段值,type为生成策略auto为自增
@TableField对应非主键
package mptest.mybatistest.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
@TableName("user_test")
public class Good implements Serializable {
@TableId(value = "id",type = IdType.AUTO)
private int id;
@TableField(value = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.mapper
继承BaseMapper<Good>,注意加上mapper注解或者在启动类加扫描mapper
package mptest.mybatistest.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import mptest.mybatistest.entity.Good;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface GoodDao extends BaseMapper<Good> {
}
3.service
继承IService<Good>
package mptest.mybatistest.service;
import com.baomidou.mybatisplus.extension.service.IService;
import mptest.mybatistest.entity.Good;
public interface IGoodService extends IService<Good> {
}
4.impl
继承extends ServiceImpl<GoodDao,Good>,两个参数一个为dao,一个为实体类
package mptest.mybatistest.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mptest.mybatistest.mapper.GoodDao;
import mptest.mybatistest.entity.Good;
import mptest.mybatistest.service.IGoodService;
import org.springframework.stereotype.Service;
@Service
public class GoodServiceImpl extends ServiceImpl<GoodDao,Good> implements IGoodService {
}
5.controller
goodService.list()为IService接口自带的方法。
package mptest.mybatistest.controller;
import mptest.mybatistest.service.IGoodService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/work")
public class GoodsController {
@Autowired
private IGoodService goodService;
@RequestMapping("/good")
public String index() {
List goodList = goodService.list();
JSONArray jsonArray= JSONArray.fromObject(goodList);
return jsonArray.toString();
}
}
访问项目,能看到数据即为环境搭建完成。