package com.ssm.yjblogs.config; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer; //import redis.clients.jedis.JedisPoolConfig; @Configuration
//定义Spring 扫描的包
@ComponentScan(value= "com.*", includeFilters= {@Filter(type = FilterType.ANNOTATION, value ={Service.class})})
//使用事务驱动管理器
@EnableTransactionManagement
//实现接口TransactionManagementConfigurer,这样可以配置注解驱动事务
public class RootConfig implements TransactionManagementConfigurer { private DataSource dataSource = null; /**
* 配置数据库.
* @return 数据连接池
*/
@Bean(name = "dataSource") //使用@Bean装配数据源 P245
public DataSource initDataSource() {
if (dataSource != null) {
return dataSource;
}
Properties props = new Properties();
props.setProperty("driverClassName", "com.mysql.jdbc.Driver");//jdbc驱动
props.setProperty("url", "jdbc:mysql://localhost:3306/chapter22");//url
props.setProperty("username", "yujie");//数据库账号
props.setProperty("password", "123456");//数据库密码
props.setProperty("maxActive", "200");//最大连接数量
props.setProperty("maxIdle", "20");//最大的空闲连接数量
props.setProperty("maxWait", "30000");//最大的等待时间,单位是毫秒
try {
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
} /***
* 配置SqlSessionFactoryBean
* @return SqlSessionFactoryBean
*/
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean initSqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(initDataSource());
//配置MyBatis配置文件
//src下的mybatis配置文件
Resource resource = new ClassPathResource("mybatis/mybatis-config.xml");
sqlSessionFactory.setConfigLocation(resource);
return sqlSessionFactory;
} /***
* 通过自动扫描,发现MyBatis Mapper接口
* @return Mapper扫描器
*/
//P325
@Bean
public MapperScannerConfigurer initMapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
//以com.开头的
msc.setBasePackage("com.*");
//设置SqlSessionFactoryBean名字
msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
//把注解为@Repository的接口扫描为Mapper对象,存放在容器中,对于多个包的扫描可以用半角逗号分隔开来
//注解为@Repository的接口表示数据访问层(DAO, Data Access Object)
msc.setAnnotationClass(Repository.class);
return msc;
} /**
* 实现接口方法,注册注解事务,当@Transactional 使用的时候产生数据库事务
*/
@Override
@Bean(name="annotationDrivenTransactionManager")
//P334
public PlatformTransactionManager annotationDrivenTransactionManager() {
DataSourceTransactionManager transactionManager =
new DataSourceTransactionManager();
transactionManager.setDataSource(initDataSource());
return transactionManager;
} // @Bean(name = "redisTemplate")
// public RedisTemplate initRedisTemplate() {
// JedisPoolConfig poolConfig = new JedisPoolConfig();
// //最大空闲数
// poolConfig.setMaxIdle(50);
// //最大连接数
// poolConfig.setMaxTotal(100);
// //最大等待毫秒数
// poolConfig.setMaxWaitMillis(20000);
// //创建Jedis链接工厂
// JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig);
// connectionFactory.setHostName("localhost");
// connectionFactory.setPort(6379);
// //调用后初始化方法,没有它将抛出异常
// connectionFactory.afterPropertiesSet();
// //自定Redis序列化器
// RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
// RedisSerializer stringRedisSerializer = new StringRedisSerializer();
// //定义RedisTemplate,并设置连接工程[修改为:工厂]
// RedisTemplate redisTemplate = new RedisTemplate();
// redisTemplate.setConnectionFactory(connectionFactory);
// //设置序列化器
// redisTemplate.setDefaultSerializer(stringRedisSerializer);
// redisTemplate.setKeySerializer(stringRedisSerializer);
// redisTemplate.setValueSerializer(stringRedisSerializer);
// redisTemplate.setHashKeySerializer(stringRedisSerializer);
// redisTemplate.setHashValueSerializer(stringRedisSerializer);
// return redisTemplate;
// } }
无行号版本:
package com.ssm.yjblogs.config; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer; //import redis.clients.jedis.JedisPoolConfig; @Configuration
//定义Spring 扫描的包
@ComponentScan(value= "com.*", includeFilters= {@Filter(type = FilterType.ANNOTATION, value ={Service.class})})
//使用事务驱动管理器
@EnableTransactionManagement
//实现接口TransactionManagementConfigurer,这样可以配置注解驱动事务
public class RootConfig implements TransactionManagementConfigurer { private DataSource dataSource = null; /**
* 配置数据库.
* @return 数据连接池
*/
@Bean(name = "dataSource") //使用@Bean装配数据源 P245
public DataSource initDataSource() {
if (dataSource != null) {
return dataSource;
}
Properties props = new Properties();
props.setProperty("driverClassName", "com.mysql.jdbc.Driver");//jdbc驱动
props.setProperty("url", "jdbc:mysql://localhost:3306/chapter22");//url
props.setProperty("username", "yujie");//数据库账号
props.setProperty("password", "123456");//数据库密码
props.setProperty("maxActive", "200");//最大连接数量
props.setProperty("maxIdle", "20");//最大的空闲连接数量
props.setProperty("maxWait", "30000");//最大的等待时间,单位是毫秒
try {
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
} /***
* 配置SqlSessionFactoryBean
* @return SqlSessionFactoryBean
*/
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean initSqlSessionFactory() {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(initDataSource());
//配置MyBatis配置文件
//src下的mybatis配置文件
Resource resource = new ClassPathResource("mybatis/mybatis-config.xml");
sqlSessionFactory.setConfigLocation(resource);
return sqlSessionFactory;
} /***
* 通过自动扫描,发现MyBatis Mapper接口
* @return Mapper扫描器
*/
//P325
@Bean
public MapperScannerConfigurer initMapperScannerConfigurer() {
MapperScannerConfigurer msc = new MapperScannerConfigurer();
//以com.开头的
msc.setBasePackage("com.*");
//设置SqlSessionFactoryBean名字
msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
//把注解为@Repository的接口扫描为Mapper对象,存放在容器中,对于多个包的扫描可以用半角逗号分隔开来
//注解为@Repository的接口表示数据访问层(DAO, Data Access Object)
msc.setAnnotationClass(Repository.class);
return msc;
} /**
* 实现接口方法,注册注解事务,当@Transactional 使用的时候产生数据库事务
*/
@Override
@Bean(name="annotationDrivenTransactionManager")
//P334
public PlatformTransactionManager annotationDrivenTransactionManager() {
DataSourceTransactionManager transactionManager =
new DataSourceTransactionManager();
transactionManager.setDataSource(initDataSource());
return transactionManager;
} // @Bean(name = "redisTemplate")
// public RedisTemplate initRedisTemplate() {
// JedisPoolConfig poolConfig = new JedisPoolConfig();
// //最大空闲数
// poolConfig.setMaxIdle(50);
// //最大连接数
// poolConfig.setMaxTotal(100);
// //最大等待毫秒数
// poolConfig.setMaxWaitMillis(20000);
// //创建Jedis链接工厂
// JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig);
// connectionFactory.setHostName("localhost");
// connectionFactory.setPort(6379);
// //调用后初始化方法,没有它将抛出异常
// connectionFactory.afterPropertiesSet();
// //自定Redis序列化器
// RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
// RedisSerializer stringRedisSerializer = new StringRedisSerializer();
// //定义RedisTemplate,并设置连接工程[修改为:工厂]
// RedisTemplate redisTemplate = new RedisTemplate();
// redisTemplate.setConnectionFactory(connectionFactory);
// //设置序列化器
// redisTemplate.setDefaultSerializer(stringRedisSerializer);
// redisTemplate.setKeySerializer(stringRedisSerializer);
// redisTemplate.setValueSerializer(stringRedisSerializer);
// redisTemplate.setHashKeySerializer(stringRedisSerializer);
// redisTemplate.setHashValueSerializer(stringRedisSerializer);
// return redisTemplate;
// } }