Spring Boot+Mybatis+DM数据库
要在Spring Boot项目中集成MyBatis和DM数据库,你需要按照以下步骤操作:
- 在
pom.xml
中添加依赖:
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- DM (DmDB) Database -->
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.1.184</version>
</dependency>
- 在
application.properties
或application.yml
中配置数据源和MyBatis:
# 数据源配置
spring.datasource.url=jdbc:dm://localhost:5236
spring.datasource.username=SYSDBA
spring.datasource.password=SYSDBA
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
# MyBatis 配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.yourpackage.model
- 创建Mapper接口和Mapper XML文件:
// src/main/java/com/yourpackage/mapper/YourMapper.java
package com.yourpackage.mapper;
public interface YourMapper {
// 定义数据库操作方法
}
<!-- src/main/resources/mapper/YourMapper.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yourpackage.mapper.YourMapper">
<!-- SQL 映射语句 -->
</mapper>
- 配置MyBatis的SQL会话工厂和事务管理器(如果需要)。
// src/main/java/com/yourpackage/config/MyBatisConfig.java
package com.yourpackage.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.co
评论已关闭