目录
1.编写目的
2.几个常用的配置类
解决跨域问题的配置类CorsConfig.java
有关mybatis自动生成的配置文件generatorConfig.xml
配置阿里巴巴数据库连接池
本博客主要是对这两个月的学习springboot的记录,对学习内容的梳理,同时加强自己的记忆
package com.thpeng.bookshop.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
corsConfiguration.addAllowedHeader("*"); // 2允许任何头
corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等)
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4
return new CorsFilter(source);
}
}
要想使用mybatis的自动生成还需要其他配置文件
mybatis全局配置文件mybatis-config.xml
在pom中添加mybatis插件
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.5
src/main/resources/generatorConfig.xml
true
true
Generate MyBatis Artifacts
generate
generate-sources
mysql
mysql-connector-java
runtime
5.0.5
添加依赖
mysql
mysql-connector-java
5.0.5
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.springframework.boot
spring-boot-starter-jdbc
com.alibaba
druid-spring-boot-starter
1.1.9
org.mybatis.generator
mybatis-generator-core
1.3.5
使用yml文件进行配置application.yml,因为yml 文件相比xml和Properties在书写上方便很多,最直观感觉就是简洁
spring:
datasource:
#使用druid连接池
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
#配置基本属性
url: jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true&useSSL=false
username: root
password: 123456
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
#配置初始化大小/最小/最大
initialSize: 1
minIdle: 1
maxActive: 20
#获取连接等待超时时间
maxWait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
timeBetweenEvictionRunsMillis: 60000
#一个连接在池中最小生存的时间
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
poolPreparedStatements: false
maxPoolPreparedStatementPerConnectionSize: 20
这里有些坑,关于yml的书写格式问题
然后进行数据源配置,DataSourceConfig.java
package com.thpeng.bookshop.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
//import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.Properties;
/**
* MyBatis基础配置
*
* @author Administrator
*/
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "testDsSqlSessionFactory")
public class DataSourceConfig {
// 精确到 cluster 目录,以便跟其他数据源隔离
//mapper接口所在的包
static final String PACKAGE = "com.thpeng.bookshop.mapper";
//映射文件目录
private static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
//哪个包下面的类自动配置别名
private static final String ALIAS = "com.thpeng.bookshop.model";
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverClass;
public DataSourceConfig() {
}
/**
* 创建数据源对象
*
* @return
* @throws
*/
@Primary
@Bean(name = "dataSource")
public DataSource dataSourceConfig() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(user);
datasource.setPassword(password);
datasource.setDriverClassName(driverClass);
return datasource;
}
@Bean(name = "testTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() throws PropertyVetoException {
return new DataSourceTransactionManager(dataSourceConfig());
}
@Bean(name = "testDsSqlSessionFactory")
public SqlSessionFactory testDsSqlSessionFactory(@Qualifier("dataSource") DataSource clusterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource);
// 加载全局的配置文件
sessionFactory.setConfigLocation(
new DefaultResourceLoader().getResource("classpath:mybatis-config.xml"));
// 配置类型别名
sessionFactory.setTypeAliasesPackage(ALIAS);
//设置mapper映射文件所在的地址
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(DataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
最后使用maven中的插件即可完成mybatis的自动生成
需要注意的是:最好自动生成只执行一次,如果你多次执行的话,生成的*mapper.xml文件是不会覆盖的,它会在内部一直添加如果执行失败的话,应该首先将原本的xml文件删除,再执行,但如果你修改了里面的sql语句,自动生成是不会更新,甚至连mapper中自定义的函数也会清除哦,博主就吃过大亏,写好了的语句,突然要新增一个表,运行自动生成,额……全没了手动/大哭,所以,尽量的话,不要运行第二次。
持续更新中……