項目結構:
集成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();
}
}
訪問項目,能看到數據即爲環境搭建完成。