环境介绍
软件 | 版本 |
DM数据库 | dm8_20230418_x86_win_64 |
IDEA | IntelliJ IDEA 2022.2.1 |
JDK | 1.8 |
Spring Boot | 2.7.13 |
mybatis | 2.3.1 |
达梦(国产数据库)可以将数据库名和表名自动转换为大写(注意!!创建达梦数据库实例的时候配置是否有勾选大小写敏感)
DM8是达梦公司在总结DM系列产品研发与应用经验的基础上,坚持开放创新、简洁实用的理念,推出的新一代自研数据库。DM8吸收借鉴当前先进新技术思想与主流数据库产品的优点,融合了分布式、弹性计算与云计算的优势,对灵活性、易用性、可靠性、高安全性等方面进行了大规模改进,多样化架构充分满足不同场景需求,支持超大规模并发事务处理和事务-分析混合型业务处理,动态分配计算资源,实现更精细化的资源利用、更低成本的投入。一个数据库,满足用户多种需求,让用户能更加专注于业务发展。
pom.xml添加达梦驱动依赖
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm8JdbcDriver18</artifactId>
<version>8.1.1.49</version>
</dependency>
整体框架
application.properties
server.port=8082
server.servlet.context-path=/dm
serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
spring.datasource.url=jdbc:dm://127.0.0.1:5236/demo?
spring.datasource.username=SYSDBA
spring.datasource.password=SYSDBA
mybatis.mapper-locations=classpath:dao/*.xml
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
实体类
@Data
public class User {
private int id;
private String name;
private int age;
}
dao(mapper)接口
@Mapper
public interface UserDao {
List<User> selectALL();
Integer insertUser(User user);
}
service层UserService接口
public interface UserService {
List<User> selectALL();
int insertUser(User user);
}
UserService实现类
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> selectALL() {
List<User> users = userDao.selectALL();
return users;
}
@Override
public int insertUser(User user) {
int i = userDao.insertUser(user);
return i;
}
}
UserDao.xml文件
sql语句生成建议用达梦管理工具生成
如
select "id","name","age"
from "SYSDBA"."user";
insert into "SYSDBA"."user"("id", "name", "age")
VALUES(<"id", INT>, <"name", CHAR(20)>, <"age", INT>);
update "SYSDBA"."user"
set "id" = <"id", INT>, "name" = <"name", CHAR(20)>, "age" = <"age", INT>;
delete from "SYSDBA"."user";
与mysql不同,达梦是“模块名”.“表名”
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.UserDao">
<select id="selectALL" resultType="com.example.pojo.User">
select * from "SYSDBA"."user"
</select>
<select id="insertUser">
insert into "demo"."user"("id", "name", "age") VALUES(#{id}, #{name}, #{age});
</select>
</mapper>
controller
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/User")
public String user(){
List<User> users = userService.selectALL();
return users.toString();
}
}
Dm8Application
@SpringBootApplication
@MapperScan("com.example.dao")
public class Dm8Application {
public static void main(String[] args) {
SpringApplication.run(Dm8Application.class, args);
}
}
test类
package com.example;
import com.example.pojo.User;
import com.example.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class Dm8ApplicationTests {
@Autowired
UserService userService;
@Test
void selectAll() {
List<User> users = userService.selectALL();
for (User user : users) {
System.out.println(user.toString());
}
}
}