springboot+mybatis+达梦数据库最简demo
1.需求
公司最近需要使⽤国产数据库,因为oracle,mysql,pg等库都是国外的,战略上不安全,所以需要调研⼀下达梦,看看和mybatis的适配,可能是由于springboot实在太⽅便了,所以每次新项⽬总是拷过来拷过去的,这导致很多臃肿⽆⽤的配置或者冗余了很多⽆⽤或⽆效配置。为了以后⽅便,⾃⼰搞⼀个简洁版的了,博⽂⽬的是记录mybatis最简⽅式,顺带着也把达梦的连接带着了。
2.⽅案
1)项⽬⽬录结构如下:截图这个主要是为了⽅便后续的代码介绍⼀⽬了然
2) pom⽂件依赖构成如下
<!--ali的德鲁伊数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<!--达梦驱动-->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm8JdbcDriver16</artifactId>
<version>8.1.1.49</version>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--整合springboot与junit测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
3)l配置如下,这⾥我把mybatis的sql打印放开了,这样⽅便看sql语句
###服务启动端⼝号
server:
port: 9092
spring:
application:
name: provide-service
#使⽤德鲁伊数据连接池
datasource:
name: dm8
type: com.alibaba.druid.pool.DruidDataSource
druid:
driverClassName: dm.jdbc.driver.DmDriver
url: jdbc:dm://192.168.23.81:5236/TEST_ZY?useUnicode=true&characterEncoding=utf-8
username: SYSDBA
password: SYSDBA
# ⽇志配置
logging:
level:
com:
zy:
mapper: debug
4) 启动类,包扫描不赘述
@SpringBootApplication(scanBasePackages = "")
5)数据源和⼯⼚等配置,本项⽬中我⽤了两个配置类,这⼀段也才是我想写这个博⽂的初衷,这⾥⾯实际上就是三个地⽅需要注
意,mabatis三件套 mapper(接⼝),xml⽂件(sql),实体类(⽤于映射),相信你通过我的最初的⽬录结构和以下这两个配置代码段应该可以搞明⽩我的意思 。
fig;
import com.alibaba.druid.pool.DruidDataSource;
batis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.fig.PropertiesFactoryBean;
import t.annotation.Bean;
import t.annotation.ComponentScan;
import t.annotation.Configuration;
import io.support.PathMatchingResourcePatternResolver;
import org.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* @author zy
* @version V1.0
jdbctemplate和mybatis* @ClassName: TestConfig
* @Function: TODO
* @Date: 2020/11/4 17:05
*/
@Configuration
public class MybatisDBConfig {
@Value("${spring.datasource.druid.driverClassName}")
private String driver;
@Value("${spring.datasource.druid.url}")
private String url;
@Value("${spring.datasource.druid.username}")
private String username;
@Value("${spring.datasource.druid.password}")
private String password;
@Bean
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMaxActive(30);
dataSource.setInitialSize(10);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(true);
return dataSource;
}
@Bean
public DataSourceTransactionManager dataSourceTransactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
}
@Bean(name = "mySqlSessionFactoryBean")
public SqlSessionFactoryBean getsqlSessionFactory() throws Exception{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.Resources("classpath*:mapping/*.xml"));
sqlSessionFactoryBean.setTypeAliasesPackage("ity");
return sqlSessionFactoryBean;
}
/**
* 配置⼀个Gather jdbc 灵活查询
* @return
*/
@Bean(name = "jdbcTemplateForSystem")
public JdbcTemplate jdbcTemplateForGather() {
return new JdbcTemplate(dataSource());
}
}
fig;
batis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import t.annotation.Bean;
import t.annotation.Configuration;
/**
* @author zy
* @version V1.0
* @ClassName: MyBatisMapperScannerConfig
* @Function: TODO
* @Date: 2020/11/4 17:06
*/
@Configuration
@AutoConfigureAfter(MybatisDBConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("apper.*");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("mySqlSessionFactoryBean");
return mapperScannerConfigurer;
}
}
3.补充说明
达梦我测试了简单的增删改查,发现了两点 。 1.⼤⼩写敏感 (schema名字和表名和列名都是⼤写的,我通过达梦⾃带的⼯具把mysql 的数据导⼊达梦库,现象如上,这说明达梦公司的理念是这样的,所以在对某些⾃有项⽬进⾏更改的时候,要谨慎) 2.需要带schema名(和postgresql好像)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论