7、SpringBoot-mybatis-plus引入
系列导航
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等工具来调用接口了。
后端控制台输出
资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!