达梦数据库使用
📑前言
本文主要是【达梦数据库】——达梦数据库简单使用的文章,如果有什么需要改进的地方还请大佬指出⛺️
🎬作者简介:大家好,我是听风与他🥇
☁️博客首页:CSDN主页听风与他
🌄每日一句:狠狠沉淀,顶峰相见
目录
- 达梦数据库使用
- 📑前言
- 本实验主要是达梦数据库增删改查的简单案例
- 1.在pom.xml中导入对应的依赖
- 2.配置application.yml文件
- 3.实体类:Student
- 4.接口类StudentMapper
- 测试类:DaMengApplicationTests
- 达梦数据库增删改查语法
- 总结
- 📑文章末尾
本实验主要是达梦数据库增删改查的简单案例
- 实验环境采用JDK17,使用springboot+mybatis整合达梦数据库
1.在pom.xml中导入对应的依赖
<!--Mybatis依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.2</version></dependency>
<!--达梦数据库驱动--><dependency><groupId>com.dameng</groupId><artifactId>DmJdbcDriver18</artifactId><version>8.1.1.193</version></dependency>
<!--jdbc驱动--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
<!--web项目依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
2.配置application.yml文件
spring:datasource:username: SYSDBA #模式名password: SYSDBA #密码url: jdbc:dm://localhost:5236 #达梦数据库默认端口为5236,不用更改driver-class-name: dm.jdbc.driver.DmDriver
server:port: 8080 #tomcat端口使用8080
3.实体类:Student
package com.dameng.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {private int id;private String name;private int age;}
4.接口类StudentMapper
package com.dameng.mapper;import com.dameng.pojo.Student;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface StudentMapper {@Select("select * FROM \"student\"")List<Student> findAll();@Insert("insert INTO SYSDBA.\"student\" VALUES(#{name},#{age})")int insertStudent(Student student);@Delete("delete FROM SYSDBA.\"student\" WHERE \"student\".\"id\" == #{id}")int deleteStudent(int id);@Update("update SYSDBA.\"student\" SET SYSDBA.\"student\".\"age\" = #{age},SYSDBA.\"student\".\"name\"=#{name} WHERE \"student\".\"id\" = #{id}")int updateStudent(Student student);@Select("select * FROM SYSDBA.\"student\" WHERE \"student\".\"id\"=#{id}")Student findStudentById(int id);
}
测试类:DaMengApplicationTests
package com.dameng;import com.dameng.mapper.StudentMapper;
import com.dameng.pojo.Student;
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 DaMengApplicationTests {//注入接口对象@Autowiredprivate StudentMapper studentMapper;@Testvoid contextLoads() {//查询所有学生List<Student> list = studentMapper.findAll();for (Student i:list){System.out.println(i.toString());}//根据ID查询学生Student student = studentMapper.findStudentById(1);System.out.println(student.toString());//添加学生Student student1 = new Student(0,"赵哈哈",18);int i = studentMapper.insertStudent(student1);System.out.println("i:"+i);//修改学生信息Student student2 = new Student(4,"赵七",20);int i1 = studentMapper.updateStudent(student2);System.out.println("i1:"+i1);//根据ID删除学生int i2 = studentMapper.deleteStudent(5);System.out.println("i2:"+i2);}}
达梦数据库增删改查语法
- 最后操作完数据库之后记得commit;不然不会立即执行。
insert INTO SYSDBA."student" VALUES('王五',20);
select * FROM SYSDBA."student";
delete FROM SYSDBA."student" WHERE "student"."id" = 3;
update SYSDBA."student" SET SYSDBA."student"."age" = 10,SYSDBA."student"."name"='张强' WHERE "student"."id" = 1;
select * FROM SYSDBA."student" WHERE "student"."id"=2;
commit;
总结
- 达梦数据库是我国的一款优秀国产数据库,但是其自适应的DM管理工具确实没有Navicat等工具简洁明了,方便好用,但支持国产,相信达梦数据库,国产数据库会变得越来越好。