前言:安装完达梦数据库后,需要初始化实例,在初始化实例时,需要注意大小写敏感的设置。大小写敏感只能在初始化数据库的时候设置,默认为大小写敏感,一旦设置成功就无法修改,如果想要修改,只能重新初始化实例。因为mybatisPlus+springboot查询时会自动去转换大小写,其中字段会转成带有“”的“field”,不如然会报错;
查询达梦数据库大小写是否敏感:
结果为 1 是大小写敏感;结果为 0 是不敏感。
SELECT CASE_SENSITIVE();
一丶.创建idea项目
新建一个项目,使用Spring Initializr初始化一个SpringBoot项目
二丶添加POM依赖
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.puwang</groupId>
<artifactId>post-power-message</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>post-power-grid</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--getter setter-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<!--連接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm8JdbcDriver18</artifactId>
<version>8.1.1.49</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</project>
三丶修改application.yml配置文件
server:
port: 12001
spring:
datasource:
driver-class-name: dm.jdbc.driver.DmDriver
url: jdbc:dm://127.0.0.1:5236?schema=PERSON&compatibleMode=mysql&characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
username: SYSDBA
password: SYSDBA
mybatis-plus:
configuration:
# 驼峰转换 从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: false
# 是否开启缓存
cache-enable: false
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
#call-setters-on-nulls: true
# 打印sql
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
四丶编写entity,注意TableName 把模式名称加上
@TableName(value = "AIJOB.PRODUCT")
@Data
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "ID", type = IdType.AUTO)
private Integer id;
private String productName;
private String bigData;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
}
五丶这个模式下分页可能会失效,加上分页的配置
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
六丶添加业务代码
/**
* @author xy丶
* @version 1.0.0
* @Date 2024/05/23 15:40
**/
@RestController
@RequestMapping("/content/aiGlobalRate")
public class ProductController {
@Autowired
private ProductService productService;
/**
* 查询所有
*/
@GetMapping("/selectAll")
public List<Product> selectAll() {
return productService.selectAll();
}
/**
* 添加
*/
@GetMapping("/addProduct")
public Boolean addProduct() {
Product product = new Product();
product.setProductName("我是产品!!!");
product.setBigData("我是产品!fdgfjgjkbdshjfgldfnkknsfbndsknfdknsfhjdsm xzmn,hjzm cnbzgjcnqwklguasknfnasgud abjasjgdsal;kdiofgsfhdisufjs,bjbvm,kaskkn!!");
product.setCreateTime(new Date());
product.setBigData(JSONObject.valueToString(product));
return productService.addProduct(product);
}
/**
* 批量添加 有问题
*/
@GetMapping("/batchAdd")
public Boolean batchAdd() {
List<Product> products = new ArrayList<>();
for (int i=0;i<=10;i++){
Product product = new Product();
product.setProductName("dsgdsg"+i+"放假时间!!!");
products.add(product);
}
return productService.batchAdd(products);
}
/**
* 查询单条
*/
@GetMapping("/getProductById")
public Product getProductById() {
return productService.getProductById(34);
}
/**
* 查询目标数据
*/
@GetMapping("/getTargetProduct")
public List<Product> getTargetProduct() {
List<Integer> integers = Arrays.asList(23, 34, 5, 6, 7);
return productService.getTargetProduct(integers);
}
/**
* 条件查询数据
*/
@GetMapping("/findProduct")
public List<Product> findProduct(String name) {
return productService.findProduct(name);
}
/**
* 分页查询
*/
@GetMapping("/productPage")
public IPage<Product> productPage(Page page, String name) {
return productService.productPage(page,name);
}
/**
* 删除单条
*/
@GetMapping("/delById")
public Boolean delById(Integer id) {
return productService.delById(id);
}
/**
* 批量删除
*/
@GetMapping("/batchDelById")
public Boolean batchDelById(String ids) {
List<String> strings = Arrays.asList(ids.split(","));
return productService.batchDelById(strings);
}
//====增删改查 分页查询条件查询
/**
* 更新单条
*/
@GetMapping("/updateProduct")
public Boolean updateProduct(Integer id,String name) {
Product product = new Product();
product.setId(id);
product.setProductName(name);
return productService.updateProduct(product);
}
/**
* 批量更新
*/
@GetMapping("/batchUpdateProduct")
public Boolean batchUpdateProduct() {
List<Product> products = new ArrayList<>();
Product product = new Product();
product.setId(4);
product.setProductName("dsjfds看难顶你是");
Product product2 = new Product();
product2.setId(5);
product2.setProductName("大飞冠的返回");
products.add(product);
products.add(product2);
return productService.batchUpdateProduct(products);
}
}
public interface ProductService extends IService<Product> {
List<Product> selectAll();
Boolean addProduct(Product product);
Boolean batchAdd(List<Product> products);
Product getProductById(Integer i);
List<Product> getTargetProduct(List<Integer> integers);
List<Product> findProduct(String name);
IPage<Product> productPage(Page page, String name);
Boolean delById(Integer id);
Boolean batchDelById(List<String> strings);
Boolean updateProduct(Product product);
Boolean batchUpdateProduct(List<Product> products);
}
@Service
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
@Autowired
private ProductMapper productMapper;
@Override
public List<Product> selectAll() {
return productMapper.selectList(Wrappers.lambdaQuery());
}
@Override
public Boolean addProduct(Product product) {
boolean save = save(product);
log.info("current id====>"+product.getId());
return save;
}
@Override
public Boolean batchAdd(List<Product> products) {
boolean b = false;
for (Product product : products) {
b = save(product);
log.info("current id====>"+product.getId());
}
return b;
}
@Override
public Product getProductById(Integer i) {
return getById(i);
}
@Override
public List<Product> getTargetProduct(List<Integer> integers) {
List<Product> products = list(Wrappers.<Product>lambdaQuery().in(Product::getId,integers));
return products;
}
@Override
public List<Product> findProduct(String name) {
List<Product> products = list(Wrappers.<Product>lambdaQuery().like(Strings.isNotBlank(name), Product::getProductName, name));
return products;
}
@Override
public IPage<Product> productPage(Page page,String name) {
IPage pageList = page(page, Wrappers.<Product>lambdaQuery().like(Strings.isNotBlank(name), Product::getProductName, name));
return pageList;
}
@Override
public Boolean delById(Integer id) {
return removeById(id);
}
@Override
public Boolean batchDelById(List<String> strings) {
return removeByIds(strings);
}
@Override
public Boolean updateProduct(Product product) {
return updateById(product);
}
@Override
public Boolean batchUpdateProduct(List<Product> products) {
return updateBatchById(products);
}
}
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
}
七丶测试
八丶采坑,使用mybatis-plus批量插入保存报错
解决办法:当前程序使用DmJdbc驱动包版本比较旧(8.1.1.49),更换为较新版本(2021年下半年以后的驱动包版本)解决。
结尾:喜欢的朋友点个赞加收藏吧谢谢!!!