博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring boot集成Mybatis
阅读量:2394 次
发布时间:2019-05-10

本文共 5025 字,大约阅读时间需要 16 分钟。

上一篇文章:,写了Spring怎么集成Mybatis Dao,由于Spring boot的快速发展,现在记录下Spring boot如何集成mybatis。

一、首先项目引入Spring boot

org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-logging
test
org.springframework.boot
spring-boot-starter-log4j2
二、pom中引入Mybatis

org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.36
三、Mybatis 启动config类

@EnableTransactionManagement@Configuration@PropertySource({"classpath:jdbc.properties"})public class DatabaseConfig {    private static final Logger logger = LoggerFactory.getLogger(DatabaseConfig.class);    @Value("${jdbc.driverClassName}")    private String jdbcDriver;    @Value("${jdbc.databaseURL}")    private String dbUrl;    @Value("${jdbc.username}")    private String username;    @Value("${jdbc.password}")    private String password;    /**     * 最小连接池数量     */    @Value("${jdbc.minIdle}")    private Integer minIdle;    /**     * 最小连接池数量     */    @Value("${jdbc.maxIdle}")    private Integer maxIdle;    /**     * 最大连接池数量,默认8     */    @Value("${jdbc.maxActive}")    private Integer maxActive;    /**     * 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,     * 并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。     */    @Value("${jdbc.maxWait}")    private Integer maxWait;    @Value("${jdbc.validationQuery}")    private String validationQuery;    @Value("${jdbc.testOnBorrow}")    private boolean testOnBorrow;    @Value("${jdbc.testOnReturn}")    private boolean testOnReturn;    /**     * 描述 : 初始化配置文件必须写的方法,否则获取不到配置文件值     * @return     *///    @Bean//    public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {//        logger.info("初始化配置文件......");//        return new PropertySourcesPlaceholderConfigurer();//    }    @Bean(name = "dataSource")    public DataSource dataSource() {        logger.info("chentian610平台数据库连接池初始化开始:URL:"+dbUrl);        DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName(jdbcDriver);        dataSource.setUrl(dbUrl);        dataSource.setUsername(username);        dataSource.setPassword(password);        dataSource.setMinIdle(minIdle);        dataSource.setMaxActive(maxActive);        dataSource.setMaxWait(maxWait);        dataSource.setValidationQuery(validationQuery);        dataSource.setTestOnBorrow(testOnBorrow);        dataSource.setTestOnReturn(testOnReturn);        return dataSource;    }    @Bean    public DataSourceTransactionManager txManager() {        return new DataSourceTransactionManager(dataSource());    }    @Bean    public SqlSessionFactory sqlSessionFactory() throws Exception {        logger.info("chentian610平台MyBatis初始化开始.................");        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();        sessionFactory.setDataSource(dataSource());//        sessionFactory.setConfigLocation(new ClassPathResource("mybatisframework.xml"));        sessionFactory.setTypeAliasesPackage("com.ninesky");        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:com/chentian610/**/config/*.xml");        logger.info("chentian610平台MyBatis成功加载Mapper文件数量:"+resources.length);        sessionFactory.setMapperLocations(resources);        return sessionFactory.getObject();    }    @Bean    public SqlSessionTemplate sqlSessionTemplate() throws Exception {        return new SqlSessionTemplate(sqlSessionFactory());    }
四、jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.databaseURL=jdbc:mysql://192.168.2.186:3306/db_name?useUnicode=true&autoReconnect=true&characterEncoding=utf8jdbc.username=db_userjdbc.password=db_passwordjdbc.jdbc_dbname=APP4#最大空闲数:空闲链接数大于maxIdle时,将进行回收jdbc.maxIdle=100jdbc.minIdle=10#最大连接数:能够同时建立的“最大链接个数”jdbc.maxActive=300#最大等待时间:单位msjdbc.maxWait=1000#使用连接时,检测连接是否成功jdbc.testOnBorrow=truejdbc.testOnReturn=true#当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能jdbc.timeout=10000jdbc.validationQuery=select 1 from dual
五、OK,欢迎大家讨论留言,一起交流

转载地址:http://uuwob.baihongyu.com/

你可能感兴趣的文章
Java并发--concurrent包的实现
查看>>
Java并发--happens-before详解
查看>>
ActiveMQ--概述
查看>>
Docker--配置ActiveMQ
查看>>
ActiveMQ--发送消息到队列、从队列接收消息
查看>>
数据结构--数组、使用数组表示矩阵
查看>>
数据结构--三角矩阵的压缩存储
查看>>
数据结构--稀疏矩阵常用的三种压缩存储(顺序、行单链表、十字链表)
查看>>
Java并发--监视器(monitor)、等待/通知机制
查看>>
Java--管道输入/输出流
查看>>
Java并发--ThreadLocal
查看>>
ActiveMQ--topic
查看>>
ActiveMQ--JMS、消息的组成
查看>>
数据结构--广义表
查看>>
Java并发--队列同步器(AbstractQueuedSynchronizer)
查看>>
Java并发--队列同步器(AQS)的实现分析
查看>>
Java并发--线程间交换数据的Exchanger
查看>>
ActiveMQ--SpringBoot整合ActiveMQ队列
查看>>
ActiveMQ--SpringBoot整合ActiveMQ主题
查看>>
ActiveMQ--传输协议
查看>>