mybatis-config
作者:互联网
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局参数 --> <settings> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="true"/> <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true --> <setting name="useColumnLabel" value="true"/> <!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false --> <setting name="useGeneratedKeys" value="false"/> <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) --> <setting name="defaultExecutorType" value="SIMPLE"/> <!-- 使用驼峰命名法转换字段。 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) default:session --> <setting name="localCacheScope" value="SESSION"/> <!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型 --> <setting name="jdbcTypeForNull" value="NULL"/> <!-- 打印SQL--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- <typeHandlers>--> <!-- <typeHandler handler="com.zte.iec.config.sys.JSONArrayTypeHandler"/>--> <!-- </typeHandlers>--> </configuration>
@Configuration @MapperScan(basePackages = "xxxxxx.repository", sqlSessionFactoryRef = "sqlSessionFactory") public class MyBatisConfig { /** * Primary 系列: masterDataSource * * @param masterDataSource the master data source * @return sql session factory * @throws Exception the exception */ @SneakyThrows @Primary @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dataSource") DataSource masterDataSource) { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); bean.setDataSource(masterDataSource); // 添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("classpath*:mybatis/**/*.xml"); bean.setMapperLocations(resources); bean.setPlugins(this.configPageHelper()); return bean.getObject(); } /** * Sql session template sql session template. * * @param sqlSessionFactory the sql session factory * @return the sql session template * @throws Exception the exception */ @Primary @Bean(name = "sqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } /** * Transaction manager data source transaction manager. * * @param masterDataSource the master data source * @return the data source transaction manager * @throws Exception the exception */ @Primary @Bean(name = "transactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource masterDataSource) { return new DataSourceTransactionManager(masterDataSource); } private Interceptor[] configPageHelper() { com.github.pagehelper.PageInterceptor pageHelper = new com.github.pagehelper.PageInterceptor(); Properties p = new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "false"); pageHelper.setProperties(p); return new Interceptor[]{pageHelper}; } }
@Configuration public class DataSourceConfig{ @Setter(onMethod_ = @Autowired) private Environment env; /** * datasource * * @return the data source * @throws Exception the exception */ @Bean(name = "dataSource") public DataSource dataSource() throws Exception { Properties props = new Properties(); this.setCommonJdbcProperties(props); props.put("url", env.getProperty("jdbc1.url")); props.put("username", env.getProperty("jdbc1.username")); props.put("password", XXX.decrypt(env.getProperty("jdbc1.password"))); props.put("driverClassName", env.getProperty("jdbc1.driverClassName")); return DruidDataSourceFactory.createDataSource(props); } /** * 设置共用的jdbc属性 */ private void setCommonJdbcProperties(Properties props) { props.put("initialSize", env.getProperty("jdbc.initialSize")); props.put("minIdle", env.getProperty("jdbc.minIdle")); props.put("maxActive", env.getProperty("jdbc.maxActive")); props.put("maxWait", env.getProperty("jdbc.maxWait")); props.put("validationQuery", env.getProperty("jdbc.validationQuery")); props.put("testOnBorrow", env.getProperty("jdbc.testOnBorrow")); props.put("testOnReturn", env.getProperty("jdbc.testOnReturn")); props.put("testWhileIdle", env.getProperty("jdbc.testWhileIdle")); props.put("timeBetweenEvictionRunsMillis", env.getProperty("jdbc.timeBetweenEvictionRunsMillis")); props.put("minEvictableIdleTimeMillis", env.getProperty("jdbc.minEvictableIdleTimeMillis")); props.put("removeAbandoned", env.getProperty("jdbc.removeAbandoned")); props.put("removeAbandonedTimeout", env.getProperty("jdbc.removeAbandonedTimeout")); props.put("logAbandoned", env.getProperty("jdbc.logAbandoned")); props.put("poolPreparedStatements", env.getProperty("jdbc.poolPreparedStatements")); props.put("maxPoolPreparedStatementPerConnectionSize", env.getProperty("jdbc.maxPoolPreparedStatementPerConnectionSize")); props.put("filters", env.getProperty("jdbc.filters")); } }
标签:jdbc,getProperty,put,env,props,mybatis,return,config 来源: https://www.cnblogs.com/tekikesyo/p/15974538.html