1.新建一个springboot项目,选择gradle管理
2.gradle添加以下依赖,gradle版本7.4
dependencies {
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
implementation 'org.springframework.boot:spring-boot-starter-web:2.6.6'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter
implementation 'org.springframework.boot:spring-boot-starter:2.6.3'
implementation 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
implementation 'com.dameng:DmJdbcDriver18:8.1.1.193'
implementation 'com.dameng:DmDialect-for-hibernate5.3:8.1.1.193'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
implementation 'org.hibernate:hibernate-core:5.4.21.Final'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.7.5'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.6.3'
// https://mvnrepository.com/artifact/junit/junit
testImplementation 'junit:junit:4.12'
}
3.application.yml配置文件
server:
port: 8085
spring:
datasource:
driver-class-name: dm.jdbc.driver.DmDriver
url: jdbc:dm://localhost:5236/FDW?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
username: FDW
password: FDW
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.DmDialect
show-sql: true
hibernate:
ddl-auto: none
4.达梦数据库新建表空间FDWSPACE,新建用户FDW绑定FDWSPACE
5.在FDW模式下新建一张表test
CREATE TABLE "FDW"."test"
(
"ID" INTEGER,
"stu_name" VARCHAR(50),
"stu_age" INTEGER) ;
6.新建实体类Student
package com.fdw.study.bean;
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name = "test")
@Data
public class Student {
@Id
@Column(name = "ID")
private Integer ID;
@Column(name = "stu_name")
private String name;
@Column(name = "stu_age")
private Integer age;
}
7.DAO接口类StuDao
package com.fdw.study.dao;
import com.fdw.study.bean.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface StuDao extends JpaRepository<Student,Integer> {
List<Student> getByNameAndAge(String name, int age);
List<Student> getByName(String name);
@Query(value = "select * from test where stu_age > 1 and stu_name like %:keyword%", nativeQuery = true)
List<Student> findUserListByAgeAndNameLike(@Param("keyword") String keyword);
}
8.Service接口类StudentService
package com.fdw.study.service;
import com.fdw.study.bean.Student;
public interface StudentService {
void saveStudent(Student student);
void queryAll();
void getByNameAndAge(String name, int age);
void getByName(String name);
void findUserListByAgeAndNameLike(String keyword);
}
9.Service接口实现类StudentServiceImpl
package com.fdw.study.service;
import com.fdw.study.bean.Student;
import com.fdw.study.dao.StuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
StuDao stuDao;
@Override
public void saveStudent(Student student) {
Student save = stuDao.save(student);
System.out.println("新增或更新一条数据");
System.out.println(save);
}
@Override
public void queryAll() {
List<Student> all = stuDao.findAll();
for (Student s : all) {
System.out.println(s);
}
}
@Override
public void getByNameAndAge(String name, int age) {
List<Student> byNameAndAge = stuDao.getByNameAndAge(name, age);
for (Student s : byNameAndAge) {
System.out.println(s);
}
}
@Override
public void getByName(String name) {
List<Student> byName = stuDao.getByName(name);
for (Student s : byName) {
System.out.println(s);
}
}
public void findUserListByAgeAndNameLike(String keyword){
List<Student> byName = stuDao.findUserListByAgeAndNameLike(keyword);
for (Student s : byName) {
System.out.println(s);
}
}
}
10.单元测试
package com.fdw;
import com.fdw.study.bean.Student;
import com.fdw.study.dao.StuDao;
import com.fdw.study.service.StudentServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = MyApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.fdw.study")
class StudyApplicationTests {
@Autowired
StudentServiceImpl studentService;
@Test
void contextLoads() {
Student student = new Student();
student.setID(3);
student.setAge(10);
student.setName("aaa");
studentService.saveStudent(student);
}
@Test
void contextLoads1() {
studentService.queryAll();
}
@Test
void contextLoads2() {
studentService.getByNameAndAge("bbb", 10);
}
@Test
void contextLoads3() {
studentService.getByName("bbb");
}
@Test
void contextLoads4() {
studentService.findUserListByAgeAndNameLike("b");
}
}
结果展示