springboot自学

1、创建maven项目,目录结构如下:

pom文件中引入所需的依赖

[html]  view plain  copy
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.test.ouyang</groupId>  
  4.   <artifactId>springboot</artifactId>  
  5.   <version>0.0.1</version>  
  6.   <packaging>jar</packaging>  
  7.   <name>springboot</name>  
  8.   <description>Demo project for Spring Boot</description>  
  9.   
  10.     <parent>  
  11.         <groupId>org.springframework.boot</groupId>  
  12.         <artifactId>spring-boot-starter-parent</artifactId>  
  13.         <version>1.5.3.RELEASE</version>  
  14.         <relativePath/> <!-- lookup parent from repository -->  
  15.     </parent>  
  16.   
  17.     <properties>  
  18.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  19.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  20.         <java.version>1.8</java.version>  
  21.     </properties>  
  22.   
  23.     <dependencies>  
  24.         <dependency>  
  25.             <groupId>org.springframework.boot</groupId>  
  26.             <artifactId>spring-boot-starter-web</artifactId>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>org.springframework.boot</groupId>  
  30.             <artifactId>spring-boot-starter-thymeleaf</artifactId>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>org.springframework.boot</groupId>  
  34.             <artifactId>spring-boot-starter-jdbc</artifactId>  
  35.         </dependency>  
  36.         <!--     添加mybatis依赖 -->  
  37.          <dependency>  
  38.             <groupId>org.mybatis</groupId>  
  39.             <artifactId>mybatis-spring</artifactId>  
  40.             <version>1.2.2</version>  
  41.         </dependency>  
  42.         <dependency>  
  43.             <groupId>org.mybatis</groupId>  
  44.             <artifactId>mybatis</artifactId>  
  45.             <version>3.2.8</version>  
  46.         </dependency>  
  47.         <!--   添加mysql的jar     -->  
  48.         <dependency>  
  49.             <groupId>c3p0</groupId>  
  50.             <artifactId>c3p0</artifactId>  
  51.             <version>0.9.1.2</version>  
  52.         </dependency>  
  53.   
  54.         <dependency>  
  55.             <groupId>mysql</groupId>  
  56.             <artifactId>mysql-connector-java</artifactId>  
  57.         </dependency>  
  58.           
  59.     </dependencies>  
  60.   
  61.     <build>  
  62.         <plugins>  
  63.             <plugin>  
  64.                 <groupId>org.springframework.boot</groupId>  
  65.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  66.             </plugin>  
  67.         </plugins>  
  68.     </build>  
  69.   
  70. </project>  

Application负责加载springdatasource并启动springboot项目

 

[java]  view plain  copy
  1. package com.test.ouyang;  
  2.   
  3. import java.util.logging.Logger;  
  4.   
  5. import javax.sql.DataSource;  
  6.   
  7. import org.apache.ibatis.session.SqlSessionFactory;  
  8. import org.mybatis.spring.SqlSessionFactoryBean;  
  9. import org.mybatis.spring.annotation.MapperScan;  
  10. import org.springframework.boot.SpringApplication;  
  11. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  13. import org.springframework.boot.context.properties.ConfigurationProperties;  
  14. import org.springframework.context.annotation.Bean;  
  15. import org.springframework.context.annotation.ComponentScan;  
  16. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;  
  17. import org.springframework.jdbc.datasource.DataSourceTransactionManager;  
  18. import org.springframework.transaction.PlatformTransactionManager;  
  19.   
  20. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  21. import com.test.ouyang.banner.MyBannner;  
  22.   
  23. @EnableAutoConfiguration  
  24. @SpringBootApplication  
  25. @ComponentScan  
  26. @MapperScan("com.test.ouyang.mapper")  
  27. public class Application {  
  28.     public static Logger log_ = Logger.getLogger(Application.class.getName());  
  29.       
  30.     /** 
  31.      * 配置spring datasource 
  32.      * 使用C3P0连接池 
  33.      * 在application.properties中设置datasource的前缀必须由是spring.datasource开始 
  34.      * */  
  35.     @Bean  
  36.     @ConfigurationProperties(prefix="spring.datasource")  
  37.     public DataSource dataSource(){  
  38.         return  new ComboPooledDataSource();  
  39.     }  
  40.     /** 
  41.      * 读取mybatis配置文件并获取sessionFactory 
  42.      * */  
  43.     @Bean  
  44.     public SqlSessionFactory sqlSessionFactoryBean() throws Exception {  
  45.   
  46.         SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
  47.         sqlSessionFactoryBean.setDataSource(dataSource());  
  48.   
  49.         PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();  
  50.   
  51.         sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));  
  52.   
  53.         return sqlSessionFactoryBean.getObject();  
  54.     }  
  55.     /**  
  56.      * 添加事务管理  
  57.      * */  
  58.     @Bean  
  59.     public PlatformTransactionManager transactionManager() {  
  60.         return new DataSourceTransactionManager(dataSource());  
  61.     }  
  62.     /** 
  63.      * 服务启动 
  64.      */  
  65.     public static void main(String[] args) {  
  66.         SpringApplication application = new SpringApplication(Application.class);  
  67.         MyBannner bannner = new MyBannner();  
  68.         application.setBanner(bannner);  
  69.         application.run(args);  
  70.         log_.info("SpringBoot Start Success");  
  71.     }  
  72. }  

application.properties文件中添加系统的配置信息

[html]  view plain  copy
  1. #配置C3P0数据源URL  
  2. spring.datasource.jdbcUrl=jdbc\:mysql\://localhost\:3306/springboot?useUnicode\=true&characterEncoding\=utf8&zeroDateTimeBehavior\=convertToNull  
  3. #配置C3P0数据源用户名  
  4. spring.datasource.user=springboot  
  5. ##配置C3P0数据源密码  
  6. spring.datasource.password=oyh1203  
  7. #配置C3P0数据源JDBC驱动  
  8. spring.datasource.driverClass=com.mysql.jdbc.Driver  
  9. #初始化C3P0连接池数量  
  10. spring.datasource.initialPoolSize=5  
  11. #C3P0连接池最小数  
  12. spring.datasource.minPoolSize=2  
  13. #C3P0连接池最大数  
  14. spring.datasource.maxPoolSize=100  
  15. #C3P0检查超时时间  
  16. spring.datasource.checkoutTimeout=10000  
  17. #关闭thymeleaf缓存,避免开发过程中出现修改页面不生效的问题  
  18. spring.thymeleaf.cache=false  
  19. #设置页面所在路径  
  20. spring.thymeleaf.prefix=classpath\:/static/  
  21. #设置页面后缀名  
  22. spring.thymeleaf.suffix=.html  
  23. #设置编码集  
  24. spring.thymeleaf.encoding=UTF-8  
  25. spring.thymeleaf.content-type=text/html  


 


 


[html]  view plain  copy
  1. spring.datasource.jdbcUrl=jdbc\:mysql\://localhost\:3306/springboot?useUnicode\=true&characterEncoding\=utf8&zeroDateTimeBehavior\=convertToNull  
  2. spring.datasource.user=springboot  
  3. spring.datasource.password=oyh1203  
  4. spring.datasource.driverClass=com.mysql.jdbc.Driver  
  5. spring.datasource.initialPoolSize=5  
  6. spring.datasource.minPoolSize=2  
  7. spring.datasource.maxPoolSize=100  
  8. spring.datasource.checkoutTimeout=10000  
  9. spring.thymeleaf.cache=false  
  10. spring.thymeleaf.prefix=classpath\:/static/  
  11. spring.thymeleaf.suffix=.html  
  12. spring.thymeleaf.encoding=UTF-8  
  13. spring.thymeleaf.content-type=text/html  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值