三、springboot集成达梦
1、数据库中创建表
1、数据库中创建表 CREATE TABLE TEST_BLOCK_T ( BLOCK_ID VARCHAR2(10 BYTE) PRIMARY KEY, --编码 BLOCK_NAME VARCHAR2(200 BYTE) --资源名称 ); Insert into TEST_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1', '哈哈哈'); COMMIT;
2、找到达梦数据库的驱动文件
达梦的安装路径下找到驱动文件
/***************************************
* 达梦8 JDBC驱动版本说明
/***************************************
1. DmJdbcDriver16 对应 Jdk1.6 及以上环境
2. DmJdbcDriver17 对应 Jdk1.7 及以上环境
3. DmJdbcDriver18 对应 Jdk1.8 及以上环境
3、引入驱动文件
引入驱动文件有两种方法
方法一
jar包放入工程里
依赖文件这么写
<dependency> <groupId>com.dmgroupId> <artifactId>Dm8JdbcDriverartifactId> <version>1.8version> <scope>systemscope> <systemPath>${project.basedir}/src/lib/DmJdbcDriver18.jarsystemPath> dependency>
方法二
使用mvn的命令将jar包安装包本地仓库里
Mvn install:install-file -Dfile=D:\DmJdbcDriver18.jar -DgroupId=com.dm -DartifactId=Dm8JdbcDriver -Dversion=1.8 -Dpackaging=jar
依赖文件这么写
<dependency> <groupId>com.dmgroupId> <artifactId>Dm8JdbcDriverartifactId> <version>1.8version> dependency>
4、pom.xml文件
<dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> <version>2.3.0.RELEASEversion> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <version>2.3.0.RELEASEversion> <scope>testscope> <exclusions> <exclusion> <groupId>org.junit.vintagegroupId> <artifactId>junit-vintage-engineartifactId> exclusion> exclusions> dependency> <dependency> <groupId>com.baomidougroupId> <artifactId>mybatis-plus-boot-starterartifactId> <version>3.3.2version> dependency> <dependency> <groupId>com.dmgroupId> <artifactId>Dm8JdbcDriverartifactId> <version>1.8version> dependency> dependencies>
5、配置文件
# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080
spring.datasource.driverClassName=dm.jdbc.driver.DmDriver
spring.datasource.url=jdbc:dm://192.168.0.100:5236/BASE_USER
spring.datasource.username=BASE_USER
spring.datasource.password=BASE_PASSWORD
#mybatis-plus控制台打印sql
mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
6、工程结构
7、源码
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") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
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 List index() { //直接调用BaseMapper封装好的CRUD方法,就可实现无条件查询数据 Listlist = blockMapper.selectList(null); //循环获取用户数据 for (Block block:list){ //获取用户名称 System.out.println(block.getBlockName()); } return list; } }
package com.example.demo.domain; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @TableName(value = "TEST_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 + '\'' + '}'; } }
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{ }