标签:查询 整合 root jar [] body UI log mysql
springboot整合mybatis可以使用springboot配置文件的形式,但是配置不了mybatis-config.xml文件(能够配置,但是不扫描),因此数据源和mybatis使用bean的形式处理,实现分页。
一、添加数据源bean,代码如下
package tjresearch; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.sql.SQLException; @Configuration public class DataSourcesConfig { /** * druid初始化 * @return * @throws SQLException */ @Primary //默认数据源 @Bean(name = "dataSource",destroyMethod = "close") public DruidDataSource Construction() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mysql://localhost:3306/website?useUnicode=true&characterEncoding=utf8"); dataSource.setUsername("root"); dataSource.setPassword("root"); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); //配置最大连接 dataSource.setMaxActive(20); //配置初始连接 dataSource.setInitialSize(1); //配置最小连接 dataSource.setMinIdle(1); //连接等待超时时间 dataSource.setMaxWait(60000); //间隔多久进行检测,关闭空闲连接 dataSource.setTimeBetweenEvictionRunsMillis(60000); //一个连接最小生存时间 dataSource.setMinEvictableIdleTimeMillis(300000); //用来检测是否有效的sql dataSource.setValidationQuery("select ‘x‘"); dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); //打开PSCache,并指定每个连接的PSCache大小 dataSource.setPoolPreparedStatements(true); dataSource.setMaxOpenPreparedStatements(20); //配置sql监控的filter dataSource.setFilters("stat,wall,log4j"); try { dataSource.init(); } catch (SQLException e) { throw new RuntimeException("druid datasource init fail"); } return dataSource; } }
二、配置mybatis
package tjresearch; import java.util.Properties; import javax.annotation.Resource; import javax.sql.DataSource; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.TransactionManagementConfigurer; import com.github.pagehelper.PageHelper; /** * mybatis配置类 * @author liuwei * */ @Configuration @EnableTransactionManagement public class MybatisConfig implements TransactionManagementConfigurer{ @Resource(name = "dataSource") DataSource dataSource; /** * 可以通过这个类,详细配置mybatis * @return */ // @Bean // public org.apache.ibatis.session.Configuration mybatisSetting(){ // org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); // // return null; // } @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("tjresearch.pojo"); //分页插件,插件无非是设置mybatis的拦截器 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("reasonable", "true"); properties.setProperty("supportMethodsArguments", "true"); properties.setProperty("returnPageInfo", "check"); properties.setProperty("params", "count=countSql"); pageHelper.setProperties(properties); //添加插件 bean.setPlugins(new Interceptor[]{pageHelper}); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { //设置xml扫描路径 bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*.xml")); return bean.getObject(); } catch (Exception e) { throw new RuntimeException("sqlSessionFactory init fail",e); } } /** * 用于实际查询的sql工具,传统dao开发形式可以使用这个,基于mapper代理则不需要注入 * @param sqlSessionFactory * @return */ @Bean(name = "sqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } /** * 事务管理,具体使用在service层加入@Transactional注解 */ @Bean(name = "transactionManager") @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }
三、controller可以实现分页效果,需要分页查询的代码紧贴PageHelper.startPage()方法。
@RequestMapping("/showItem") @ResponseBody public PageItem showItem(@RequestParam(required = true, defaultValue = "1") Integer pageNum, @RequestParam(required = true, defaultValue = "20") Integer pageSize){ PageHelper.startPage(pageNum, pageSize);// 默认从第一页开始,每页五条 List<Item> itemList = showService.findAllItem(); PageInfo<Item> pageUser = new PageInfo<Item>(itemList); int allPage = pageUser.getPages(); long total = pageUser.getTotal(); List<Item> list = pageUser.getList(); PageItem pageItem = new PageItem(); pageItem.setAllPage(allPage); pageItem.setTotal(total); pageItem.setList(list); return pageItem; }
四、pom导入jar
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency>
标签:查询 整合 root jar [] body UI log mysql
原文地址:http://www.cnblogs.com/fengmao/p/7447365.html