SpringBoot Mybatis 框架整合笔记

pom文件jar包如下:


  4.0.0


  com
  springboot
  0.0.1-SNAPSHOT
  jar


  springboot
  http://maven.apache.org


    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.1.RELEASE
    
  
    
        UTF-8
        UTF-8
        1.8
    


  
  
 		
  			com.winbaoxian.bigdata.crawler
			small-spider
			1.1
  		 
		
			org.apache.httpcomponents
			httpcore
			4.4.6
		
    	
     		junit
      		junit
    		3.8.1
     		 test
    	
    	 
            org.springframework.boot
            spring-boot-devtools
            true
        
       
	
	 
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
		
		
  		  	mysql
    	  	mysql-connector-java
   	 	 	  5.1.6
   	    
   	 	 
		
    		commons-pool
    		commons-pool
    		1.6
		
   	 	 
		
   			 commons-dbcp
    		 commons-dbcp
    		 1.4
		
   	 	 
   	 	 	
   	 	 
    		org.jsoup
    		jsoup
   		    1.8.3
		
		
		
   		    commons-logging
   		    commons-logging
    		 1.1.1
		


   	 	 
  
   
        
            
                <groupId>org.springframework.boot
                spring-boot-maven-plugin
            
        
    



这里是通过mapping.xml文件操作数据库,所有数据源配置如下:

package com.study.crawler.config;


import javax.sql.DataSource;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;


/**
 * @author cqw
 * @Introduce mybatis信息配置 自动读取mapping配置文件
 * @Param
 * @Return
 * @Time 2018年3月31日
 */
@Configuration
@MapperScan(basePackages = "com.study.crawler.dao", sqlSessionTemplateRef = "spiderSqlSessionTemplate")//对应dao的位置
public class MybatisConfigTest {
	@Autowired
	private DataSource dataSource;


	@Bean(name = "spiderSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		// 显式指定数据源
		bean.setDataSource(dataSource);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		// 指定xml文件位置
		bean.setMapperLocations(resolver.getResources("classpath:/sqlMap/*.xml"));
		return bean.getObject();
	}


	@Bean(name = "spiderSqlSessionTemplate")
	public SqlSessionTemplate testSqlSessionTemplate(
			@Qualifier("spiderSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}

如果是多数据源 则还需要配置:

package com.study.crawler.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cqw
 * @date 2018年4月13日
 * @introduce:定义多数据源时读取application.properties中的配置信息
 */
@Configuration
public class DataSourceConfig {

	// 配置主数据源
	@Bean(name = "primaryDataSource")
	@Qualifier("primaryDataSource")
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource primaryDataSource() {
		return DataSourceBuilder.create().build();
	}

	// 配置第二数据源
	/*@Bean(name = "secondDataSource")
	@Qualifier("secondDataSource")
	@Primary
	@ConfigurationProperties(prefix = "spring.second.datasource")
	public DataSource secondDataSource() {
		return DataSourceBuilder.create().build();
	}*/

	
}

server.port=1996

com.study.ThreadNumber=4

#spring.datasource  在但数据源 boot会自动读取 不需要配置DataSourceConfig 配置类
spring.datasource.driver-class-name=   
spring.datasource.url=jdbc:mysql://localhost:3306/crawler?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=
spring.datasource.password=
最后 提一点:springboot的启动类 要放在最上层的路径,或者添加注解@ComponentScan(basePackages = { "com.study.crawler" })这样才会扫描包下的注解



你可能感兴趣的:(框架)