SpringBoot - 整合Mybatis-Plus
阿新 • • 發佈:2021-09-07
目錄
- End -
﹀
﹀
﹀
夢想是鹹魚
關注一下吧
以上為本篇文章的主要內容,希望大家多提意見,如果喜歡記得點個推薦哦
作者:Maggieq8324
出處:https://www.cnblogs.com/maggieq8324/
本文版權歸作者和部落格園共有,歡迎轉載,轉載時保留原作者和文章地址即可。
前言
Mybatis-Plus
是Mybatis
的增強,Mybatis-Plus
在Mybatis
的基礎上借鑑了很多JPA
的做法,記錄下SpringBoot
下的整合方法。
環境
SpringBoot2.53 + Mybatis-Plus3.3.0
具體實現
專案結構
專案配置
pom.xml
<!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.0</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency>
application.yml
spring: application: name: mybatis-learn # 檔案編碼 UTF8 mandatory-file-encoding: UTF-8 # 資料來源配置,請修改為你專案的實際配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cms?useSSL=false&serverTimezone=UTC&characterEncoding=UTF8 username: root password: sunday # mybatis-plus配置 mybatis-plus: configuration: # 開啟下劃線轉駝峰 map-underscore-to-camel-case: true # mapper路徑位置 mapper-locations: classpath:mapper/*.xml server: port: 8084
Table
CREATE TABLE `product` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`create_time` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
實現程式碼
- 啟動類
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.coisini.mybatisplus.mapper") // mapper掃描位置
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
ProductController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/product")
@RestController
public class ProductController {
@Autowired
private ProductService productService;
/**
* 查詢Products
* @return
*/
@GetMapping("/select")
public List<Product> selectProducts() {
return productService.getProducts();
}
}
ProductService.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductService {
@Autowired
private ProductMapper productMapper;
/**
* 查詢products
* @return
*/
public List<Product> getProducts() {
return productMapper.selectList(null);
}
}
ProductMapper.java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductMapper extends BaseMapper<Product> {
// 繼承 BaseMapper,與JPA類似
}
Product.java
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
@Getter
@Setter
@TableName("product")
public class Product {
private Integer id;
private String title;
private Date createTime;
}