1、添加依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <!--mybatisplus融合--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </exclusion> </exclusions> </dependency>
2、添加代码开发
package com.goldcard.lifecycle.dao.config; import com.alibaba.druid.pool.DruidDataSource; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; /** * db数据源配置 * * @author xiaozhilei */ @Configuration @MapperScan(basePackages = "com.dao.mapper", sqlSessionFactoryRef = "dbSqlSessionFactory") @Slf4j public class DataSourceConfig { @Value("${spring.datasource.db.url}") private String dbUrl; @Value("${spring.datasource.db.password}") private String password; @Value("${spring.datasource.db.username}") private String username; /** * 包名 */ private static final String TypeAliasesPackage = "com.dao.domain"; /** * mapper扫面包路径 */ private static final String MapperLocations = "classpath*:mapper/*.xml"; @Primary @Bean("dbDataSource") @ConfigurationProperties(prefix = "spring.datasource.db") public DataSource getDbDataSource() { log.info("数据库环境配置地址.{}",dbUrl); log.info("数据库环境配置用户名.{}",username); log.info("数据库环境配置密码.{}",password); return new DruidDataSource(); //return DataSourceBuilder.create().build(); } @Primary @Bean("dbSqlSessionFactory") public SqlSessionFactory dbSqlSessionFactory(@Qualifier("dbDataSource") DataSource dataSource) throws Exception { //SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage(TypeAliasesPackage); bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:/config/mybatis-config.xml")); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MapperLocations)); return bean.getObject(); } @Primary @Bean("dbSqlSessionTemplate") public SqlSessionTemplate dbSqlSessionTemplate(@Qualifier("dbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
3 添加相关依赖
在:resources目录下,创建:mybatis-config.xml
然后添加如下代码:配置mybatis的驼峰命名等
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
标签:springboot,spring,springframework,bean,plus,org,mybatis,import From: https://www.cnblogs.com/xzlnuli/p/18181696