版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36553913/article/details/81414965
说明
springboot集成mybatis和使用c3p0连接池,当初学习时过程还是比较艰难的,以翟永超的博客为基础,再找各种资料博客进行学习,还有在学习mybatis自动代码生成插件时,遇到的问题也不少,今天在这里进行回顾总结下。
正文
构建springboot项目,添加依赖mybatis和mysql-connector-java,start.spring.io在构建也不支持c3p0的自动添加,需要到maven资源库找c3p0的依赖。这里同样放到本地的tomcat中运行,添加web依赖。使用自动插件,要在pom.xml中添加插件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--mybatis generator 自动生成代码-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
创建好项目后,需要对c3p0和mybatis进行配置,首先在application.properties文件中配置mysql
c3p0.datasource.driverClass=com.mysql.jdbc.Driver
c3p0.datasource.jdbcUrl=jdbc:mysql://localhost:3306/springboot
c3p0.datasource.user=root
c3p0.datasource.password=123
c3p0.datasource.minPoolSize=3
c3p0.datasource.maxPoolSize=100
c3p0.datasource.initialPoolSize=3
DataSourceConfig
@Configuration
public class DataSourceConfig {
@ConfigurationProperties(prefix = "c3p0.datasource")
@Primary
@Bean(name = "dataSource")
public DataSource dataSource(){
return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();
}
}
MybatisConfig
@Configuration
public class MybatisConfig {
@Autowired
private DataSource dataSource;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext) throws IOException {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage("com.example.mybatis.bean");
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath*:com/example/mybatis/mapper/*Mapper.xml");
sessionFactory.setMapperLocations(resources);
return sessionFactory;
}
}
配置好c3p0和mybatis后,需要对插件进行配置,创建generatorConfig.xml
更详细的配置请见:http://www.mybatis.org/generator/configreference/xmlconfig.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="./src/main/resources/lib/mysql-connector-java-5.1.25-bin.jar"></classPathEntry>
<context id="DB2Tables" targetRuntime="Mybatis3">
<commentGenerator>
<!--是否不生成注释 默认为false 当不指定时也会生成注释,true不生成 true时所有代码合并将不能使用-->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/springboot"
userId="root"
password="123"
/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--实体类生成的包名和位置-->
<javaModelGenerator targetPackage="com.example.mybatis.bean" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--映射文件生成的包名和位置-->
<sqlMapGenerator targetPackage="com.example.mybatis.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--DAO生成的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mybatis.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="t_teacher" domainObjectName="Teacher" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<!--<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
</context>
</generatorConfiguration>
配置好后需要在maven的插件中执行,也可以创建maven执行快捷方式
源码地址:https://github.com/Edenwds/springboot_study/tree/master/mybatis