SpringBoot--整合Mybatis和Redis

来源:互联网 发布:java能调用api 编辑:程序博客网 时间:2024/05/16 11:45

1 SpringBoot并没有提供整合Mybatis的工具包
第一种:使用mybatis官方提供的Spring Boot整合包实现,下载地址:https://github.com/mybatis/spring-boot-starter
第二种:自己整合,与Spring整合Mybatis类似。

2 配置JDBC,将数据源添加到容器中。

/** * JDBC配置 * @author Tang  * 2017年11月18日 */@Configuration@PropertySource(value = { "jdbc.properties" })//读取外部配置文件public class JdbcConfig {    // 数据库配置开始    @Value("${driverClassName}")    private String driverClassName;    @Value("${url}")    private String url;    @Value("${username}")    private String username;    @Value("${password}")    private String password;    @Bean(destroyMethod = "close")    public BasicDataSource dataSource() {        BasicDataSource basicDataSource = new BasicDataSource();        basicDataSource.setDriverClassName(driverClassName);        basicDataSource.setUrl(url);        basicDataSource.setUsername(username);        basicDataSource.setPassword(password);        return basicDataSource;    }}

需要读取外部的配置文件:
jdbc.properties

driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=trueusername=rootpassword=123123

3 整合Mybatis

/** * Mybatis的配置 * @author Tang * 2017年11月18日 */@Configuration@AutoConfigureAfter(JdbcConfig.class)//保证在JDBC配置完成后实例化public class MybatisConfig {    @Autowired    private DataSource dataSource;    @Bean    @ConditionalOnMissingBean //当容器里没有指定的Bean的情况下创建该对象    public SqlSessionFactoryBean sqlSessionFactory() {        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();        // 设置数据源        sqlSessionFactoryBean.setDataSource(dataSource);        // 设置mybatis的主配置文件        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();        Resource mybatisConfigXml = resolver.getResource("classpath:mybatis-config.xml");        sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);        return sqlSessionFactoryBean;    }    /**     * 定义扫描     * @return     */    @Bean    public MapperScannerConfigurer mapperScannerConfigurer() {        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();        mapperScannerConfigurer.setBasePackage("com.text.springboot.dao");        return mapperScannerConfigurer;    }}

通过Spring的自动注入,注入上一步配置好的JDBC数据源。

4 配置声明式事务管理
在Spring Boot中推荐使用@Transactional注解来申明事务。
首先需要导入依赖:

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-jdbc</artifactId>        </dependency>

当引入jdbc依赖之后,Spring Boot会自动默认分别注入DataSourceTransactionManager或JpaTransactionManager,所以我们不需要任何额外配置就可以用@Transactional注解进行事务的使用。
在需要事务的Service中添加@Transactional注解即可,具体可看Spring的官方文档,关于声明式事务。

5 整合Redis

/** * Redis相关配置 * @author Tang * 2017年11月18日 */@Configuration@PropertySource(value="classpath:redis.properties")public class RedisConfig {    @Value("${redis.url}")    private String url;    @Value("${redis.port}")    private Integer port;    @Value("${redis.maxTotal}")    private Integer maxTotal;    @Bean    public ShardedJedisPool shardedJedisPool() {        List<JedisShardInfo> jedisShardInfos = new ArrayList<JedisShardInfo>();        jedisShardInfos.add(new JedisShardInfo(url, port));        return new ShardedJedisPool(jedisPoolConfig(), jedisShardInfos);    }    /**     * 线程池配置     * @return     */    private JedisPoolConfig jedisPoolConfig() {        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();        jedisPoolConfig.setMaxTotal(maxTotal);        return jedisPoolConfig;    }}

配置文件:

redis.url=127.0.0.1redis.port=6379redis.maxTotal=10

和JDBC类似。读取配置文件,映射到变量中,然后初始化对象到SpringBoot容器中。
部分摘自某智播客。