在項目中mybaties配置數據源並指定mapper包的掃描路徑和SessionFactory對象
package boot.spring.config;
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.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 配置數據源,指定mapper包的掃描路徑和SessionFactory對象
*/
@Configuration
@MapperScan(value = "boot.spring.mapper",sqlSessionFactoryRef = "sqlSessionFactory")
public class MyDataSource {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.ssm.driver-class-name}")
private String driverClassName;
@Bean
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
return datasource;
}
@Bean
public SqlSessionFactory sqlSessionFactory( ) throws Exception {
// 設置mapper的xml文件路徑
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource());
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/*.xml");
factoryBean.setMapperLocations(resources);
// 設置mybatis-config.xml的路徑
Resource config = new PathMatchingResourcePatternResolver()
.getResource("classpath:mybatis-config.xml");
factoryBean.setConfigLocation(config);
return factoryBean.getObject();
}
@Bean
public DataSourceTransactionManager primaryTransactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
// 使用上面配置的Factory
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory());
return template;
}
}
mybaties配置文件:
<?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>
<plugins>
<!-- com.github.pagehelper爲PageHelper類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 4.0.0以後版本可以不設置該參數 -->
<property name="dialect" value="mysql"/>
<!-- 該參數默認爲false -->
<!-- 設置爲true時,會將RowBounds第一個參數offset當成pageNum頁碼使用 -->
<!-- 和startPage中的pageNum效果一樣-->
<property name="offsetAsPageNum" value="true"/>
<!-- 該參數默認爲false -->
<!-- 設置爲true時,使用RowBounds分頁會進行count查詢 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 設置爲true時,如果pageSize=0或者RowBounds.limit = 0就會查詢出全部的結果 -->
<!-- (相當於沒有執行分頁查詢,但是返回結果仍然是Page類型)-->
<property name="pageSizeZero" value="true"/>
<!-- 3.3.0版本可用 - 分頁參數合理化,默認false禁用 -->
<!-- 啓用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最後一頁 -->
<!-- 禁用合理化時,如果pageNum<1或pageNum>pages會返回空數據 -->
<property name="reasonable" value="false"/>
<!-- 3.5.0版本可用 - 爲了支持startPage(Object params)方法 -->
<!-- 增加了一個`params`參數來配置參數映射,用於從Map或ServletRequest中取值 -->
<!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默認值 -->
<!-- 不理解該含義的前提下,不要隨便複製該配置 -->
<property name="params" value="pageNum=pageHelperStart;pageSize=pageHelperRows;"/>
<!-- 支持通過Mapper接口參數來傳遞分頁參數 -->
<property name="supportMethodsArguments" value="false"/>
<!-- always總是返回PageInfo類型,check檢查返回類型是否爲PageInfo,none返回Page -->
<property name="returnPageInfo" value="none"/>
</plugin>
</plugins>
</configuration>