第一次发表博客,写的不好欢迎指出,一起学习。整个整合过程借鉴了大量的博客,再次先感谢前辈们的无私奉献。
工具idea maven
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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></groupId> <artifactId>login</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.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-test</artifactId> <scope>test</scope> </dependency> <!--包括依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!--druid数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.26</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <!--json格式--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> </project>
目录结构
application.properties是主要的配置文件,为了后期的方便一般不在里面写东西
application-dev.properties是开发模式 在application.properties指明选择开发模式
spring.profiles.active=dev
application-dev.properties的配置文件
#数据库的配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #mysql的版本太低 需要降低springboot的版本 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/login spring.datasource.username=root spring.datasource.password=root # 下面为连接池的补充设置,应用到上面所有数据源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 #验证数据库连接的有效性 spring.datasource.validationQuery=SELECT 1 spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false # 打开PSCache,并且指定每个连接上PSCache的大小 PSCache对Oracle的帮助很大 spring.datasource.poolPreparedStatements=false spring.datasource.maxPoolPreparedStatementPerConnectionSize=0 #mybatis的别名 mybatis.type-aliases-package=classpath:com.yjp.springb.*.pojo
下面配置数据源,springboot只是想xml的方式,变为了java类的形式
import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.ApplicationContextException; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.util.StringUtils; import java.sql.SQLException; import java.util.Arrays; /** * DruidDataSourceConfig.java读取数据源的配置 * * * Date : 17:07 2017/12/5 */ @Configuration @EnableTransactionManagement @MapperScan(value = "dao层的位置") public class DruidDataSourceConfig implements EnvironmentAware { private Environment environment; private RelaxedPropertyResolver propertyResolver; @Override public void setEnvironment(Environment environment) { this.environment = environment; this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource."); } /** * 注册dataSource * * @return * @throws SQLException */ @Bean public DruidDataSource dataSource() throws SQLException { if (StringUtils.isEmpty(propertyResolver.getProperty("url"))) { System.out.println("Your database connection pool configuration is incorrect!" + " Please check your Spring profile, current profiles are:" + Arrays.toString(environment.getActiveProfiles())); throw new ApplicationContextException( "Database connection pool is not configured correctly"); } DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name")); druidDataSource.setUrl(propertyResolver.getProperty("url")); druidDataSource.setUsername(propertyResolver.getProperty("username")); druidDataSource.setPassword(propertyResolver.getProperty("password")); druidDataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize"))); druidDataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle"))); druidDataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive"))); druidDataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait"))); druidDataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(propertyResolver.getProperty("timeBetweenEvictionRunsMillis"))); druidDataSource.setMinEvictableIdleTimeMillis(Long.parseLong(propertyResolver.getProperty("minEvictableIdleTimeMillis"))); druidDataSource.setValidationQuery(propertyResolver.getProperty("validationQuery")); druidDataSource.setTestWhileIdle(Boolean.parseBoolean(propertyResolver.getProperty("testWhileIdle"))); druidDataSource.setTestOnBorrow(Boolean.parseBoolean(propertyResolver.getProperty("testOnBorrow"))); druidDataSource.setTestOnReturn(Boolean.parseBoolean(propertyResolver.getProperty("testOnReturn"))); druidDataSource.setPoolPreparedStatements(Boolean.parseBoolean(propertyResolver.getProperty("poolPreparedStatements"))); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize"))); return druidDataSource; } /** * 拿到SqlSessionFactory * * @return * @throws Exception */ @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); return sqlSessionFactoryBean.getObject(); } @Bean public PlatformTransactionManager transactionManager() throws SQLException { return new DataSourceTransactionManager(dataSource()); } }
配置到此结束 在主程序中做测试
import com.yjp.springb.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication @ComponentScan(basePackages = {"XXX需要扫描的包"}) public class LoginApplication { @Autowired @Qualifier("userService") UserService userService; public static void main(String[] args) { SpringApplication.run(LoginApplication.class, args); } @RequestMapping("/login") public String login() { 这里可以调用service中的方法 return "success"; } }
到此结束,有问题可以留言一起讨论