1.新建springboot项目
2.打开项目列表,找到pom.xml文件,导入mybatis,c3p0,mysql依赖文件
粘贴以下内容至dependencies标签内
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
3.在src/main/resources目录下建立application-c3p0.propertis文件内容如下
spring.datasource.c3p0.driverClass=com.mysql.cj.jdbc.Driver spring.datasource.c3p0.jdbcUrl=jdbc:mysql://localhost:3306/数据库名?serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&useLegacyDatetimeCode=false spring.datasource.c3p0.user=用户名 spring.datasource.c3p0.password=密码 spring.datasource.c3p0.initialPoolSize=5 spring.datasource.c3p0.minPoolSize=5 spring.datasource.c3p0.maxPoolSize=10 spring.datasource.c3p0.acquireIncrement=5
4.在src/main/resources目录下建立mybatis-config.xml文件内容如下
<?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="cacheEnabled" value="true"/> <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果--> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 允许使用列标签代替列名 --> <setting name="useColumnLabel" value="true"/> <!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成 --> <setting name="useGeneratedKeys" value="true"/> <!-- 对于批量更新操作缓存SQL以提高性能 --> <setting name="defaultExecutorType" value="SIMPLE"/> <!-- 数据库超过25000秒仍未响应则超时 --> <setting name="defaultStatementTimeout" value="25000"/> <!--开启驼峰式命名规则映射--> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
5.在application.propertis文件中添加
spring.profiles.active=c3p0 mybatis.mapperLocations=classpath:mappers/*.xml mybatis.configLocation=classpath:mybatis-config.xml
6.在src/main/java下创建包,包名为config,创建名为DataSourceConfiguration的类内容如下
@Configuration//声明为配置类 public class DataSourceConfiguration { @Bean(name = "dataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource.c3p0") public DataSource createDataSource() { return DataSourceBuilder.create() // 创建数据源构建对象 .type(ComboPooledDataSource.class) // 设置数据源类型 .build(); // 构建数据源对象 } }
7.如果你的mapper.xml在、src/main/java的目录下,请在pom.xml文件的bulid标签内复制如下内容
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> </resources>
8.建立你的实体类包,mapper类的包,例如下
这里切记,mapper包里面的**.xml和**接口的**要一致,否则会org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)