1、创建maven项目,目录结构如下:
pom文件中引入所需的依赖
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.test.ouyang</groupId>
- <artifactId>springboot</artifactId>
- <version>0.0.1</version>
- <packaging>jar</packaging>
- <name>springboot</name>
- <description>Demo project for Spring Boot</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.3.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <!-- 添加mybatis依赖 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.2.8</version>
- </dependency>
- <!-- 添加mysql的jar -->
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
Application负责加载springdatasource并启动springboot项目
- package com.test.ouyang;
- import java.util.logging.Logger;
- import javax.sql.DataSource;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import org.springframework.transaction.PlatformTransactionManager;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- import com.test.ouyang.banner.MyBannner;
- @EnableAutoConfiguration
- @SpringBootApplication
- @ComponentScan
- @MapperScan("com.test.ouyang.mapper")
- public class Application {
- public static Logger log_ = Logger.getLogger(Application.class.getName());
- /**
- * 配置spring datasource
- * 使用C3P0连接池
- * 在application.properties中设置datasource的前缀必须由是spring.datasource开始
- * */
- @Bean
- @ConfigurationProperties(prefix="spring.datasource")
- public DataSource dataSource(){
- return new ComboPooledDataSource();
- }
- /**
- * 读取mybatis配置文件并获取sessionFactory
- * */
- @Bean
- public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
- SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
- sqlSessionFactoryBean.setDataSource(dataSource());
- PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
- sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
- return sqlSessionFactoryBean.getObject();
- }
- /**
- * 添加事务管理
- * */
- @Bean
- public PlatformTransactionManager transactionManager() {
- return new DataSourceTransactionManager(dataSource());
- }
- /**
- * 服务启动
- */
- public static void main(String[] args) {
- SpringApplication application = new SpringApplication(Application.class);
- MyBannner bannner = new MyBannner();
- application.setBanner(bannner);
- application.run(args);
- log_.info("SpringBoot Start Success");
- }
- }
application.properties文件中添加系统的配置信息
- #配置C3P0数据源URL
- spring.datasource.jdbcUrl=jdbc\:mysql\://localhost\:3306/springboot?useUnicode\=true&characterEncoding\=utf8&zeroDateTimeBehavior\=convertToNull
- #配置C3P0数据源用户名
- spring.datasource.user=springboot
- ##配置C3P0数据源密码
- spring.datasource.password=oyh1203
- #配置C3P0数据源JDBC驱动
- spring.datasource.driverClass=com.mysql.jdbc.Driver
- #初始化C3P0连接池数量
- spring.datasource.initialPoolSize=5
- #C3P0连接池最小数
- spring.datasource.minPoolSize=2
- #C3P0连接池最大数
- spring.datasource.maxPoolSize=100
- #C3P0检查超时时间
- spring.datasource.checkoutTimeout=10000
- #关闭thymeleaf缓存,避免开发过程中出现修改页面不生效的问题
- spring.thymeleaf.cache=false
- #设置页面所在路径
- spring.thymeleaf.prefix=classpath\:/static/
- #设置页面后缀名
- spring.thymeleaf.suffix=.html
- #设置编码集
- spring.thymeleaf.encoding=UTF-8
- spring.thymeleaf.content-type=text/html
- spring.datasource.jdbcUrl=jdbc\:mysql\://localhost\:3306/springboot?useUnicode\=true&characterEncoding\=utf8&zeroDateTimeBehavior\=convertToNull
- spring.datasource.user=springboot
- spring.datasource.password=oyh1203
- spring.datasource.driverClass=com.mysql.jdbc.Driver
- spring.datasource.initialPoolSize=5
- spring.datasource.minPoolSize=2
- spring.datasource.maxPoolSize=100
- spring.datasource.checkoutTimeout=10000
- spring.thymeleaf.cache=false
- spring.thymeleaf.prefix=classpath\:/static/
- spring.thymeleaf.suffix=.html
- spring.thymeleaf.encoding=UTF-8
- spring.thymeleaf.content-type=text/html