SpringBoot特性随用随记
JDBC集成
pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
application.properties配置链接参数
spring.datasource.url=jdbc:mysql://localhost:3306/dbName?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=username spring.datasource.password=****** spring.datasource.driver-class-name=com.mysql.jdbc.Driver
bean使用
@Resource private JdbcTemplate jdbcTemplate; // org.springframework.jdbc.core.JdbcTemplate
mybatis Mapper注解
pom依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
注解
@Mapper
@Options
@Select
@Insert
需要sqlSessionTemplate或sqlSessionFactory bean:
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
return (SqlSessionFactory) sqlSessionFactory.getObject();
}
@Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource()); return (SqlSessionFactory) sqlSessionFactory.getObject(); }
spring.factories文件常用配置
org.springframework.cloud.bootstrap.BootstrapConfiguration=XxxClass
org.springframework.cloud.antfin.common.binder.BinderFactory=XxxClass
org.springframework.boot.autoconfigure.EnableAutoConfiguration=XxxClass