系列導航
6、SpringBoot-mybatis分頁實現pagehelper
9、SpringBoot-mybatis-druid多源數據多源數據
10、SpringBoot-mybatis-plus-druid多源數據
11、SpringBoot-mybatis-plus-druid多源數據事務
12、SpringBoot-mybatis-plus-ehcache
14、SpringBoot-easyexcel導出excle
完結
SpringBoot連接數據庫引入mybatis-plus
1、 數據准備(oracle數據庫)
Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1001', 'RabbitMQ'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1002', 'ZeroMQ'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1003', 'ActiveMQ'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1004', 'RocketMQ'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1005', 'Apollo'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1', '后端開發'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('2', '前端開發'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('3', '前端框架'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('4', '后端框架'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('5', '數據庫'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('6', 'NoSql'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('7', '對象存儲'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('8', '大數據'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('9', '操作系統'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('10', '消息隊列'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('100', 'Python'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('101', 'Java'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('102', 'PHP'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('103', 'C'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('104', 'C++'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('105', 'C#'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('106', 'PHP'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('107', 'go'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('108', 'Visual Basic'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('201', 'JavaScript'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('202', 'css'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('203', 'swift'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('204', 'html5'); Insert into XY_DIC_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('300', 'Vue'); commit;
2、pom.xml文件
<?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> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.1.17.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 集成mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- oracle驅動 --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> </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> </dependencies> </project>
3、application.properties配置
為了簡化配置這里就沒有引入druid,需要的朋友可以根據前面的博客自行添加
# 應用名稱
spring.application.name=demo
# 應用服務 WEB 訪問端口
server.port=8080
# 數據庫設置
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.1.100:1521:orcl
spring.datasource.username=zy
spring.datasource.password=1
#mybatis-plus控制台打印sql
mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4、工程目錄
5、代碼部分
啟動類
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.mapper") //這里要注意掃描mapper文件 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
實體類
package com.example.demo.domain; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @TableName(value = "XY_DIC_BLOCK_T") public class Block { private static final long serialVersionUID = 1L; @TableId private String blockId; private String blockName; public String getBlockId() { return blockId; } public void setBlockId(String blockId) { this.blockId = blockId; } public String getBlockName() { return blockName; } public void setBlockName(String blockName) { this.blockName = blockName; } @Override public String toString() { return "XyDicBlockT{" + "blockId='" + blockId + '\'' + ", blockName='" + blockName + '\'' + '}'; } }
mapper類
package com.example.demo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.demo.domain.Block; import org.apache.ibatis.annotations.Mapper; @Mapper public interface BlockMapper extends BaseMapper<Block> { }
控制器類
package com.example.demo.controller; import com.example.demo.domain.Block; import com.example.demo.mapper.BlockMapper; 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.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/hello") public class HelloController { @Autowired private BlockMapper blockMapper; @GetMapping("/list") @ResponseBody public String index() { //直接調用BaseMapper封裝好的CRUD方法,就可實現無條件查詢數據 List<Block> list = blockMapper.selectList(null); //循環獲取用戶數據 for (Block block:list){ //獲取用戶名稱 System.out.println(block.getBlockName()); } return "sucess"; } }
6、啟動項目訪問項目
很簡單,使用mybatis-plus 大部分的操作都不用寫xml文件了,省事好多
注:因為是get請求可以用瀏覽器來調用,如果是post的請求就只能用postman等工具來調用接口了。
后端控制台輸出