Spring Boot集成MyBatis与分页插件

转载自:http://blog.csdn.net/goldenfish1919/article/details/51706316


1.MyBatisConfig.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Configuration  
  2. @EnableTransactionManagement  
  3. public class MyBatisConfig implements TransactionManagementConfigurer {  
  4.   
  5.     @Autowired  
  6.     DataSource dataSource;  
  7.   
  8.     @Bean(name = "sqlSessionFactory")  
  9.     public SqlSessionFactory sqlSessionFactoryBean() {  
  10.         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();  
  11.         bean.setDataSource(dataSource);  
  12.         //分页插件  
  13.         PageHelper pageHelper = new PageHelper();  
  14.         Properties props = new Properties();  
  15.         props.setProperty("reasonable""true");  
  16.         props.setProperty("supportMethodsArguments""true");  
  17.         props.setProperty("returnPageInfo""check");  
  18.         props.setProperty("params""count=countSql");  
  19.         pageHelper.setProperties(props);  
  20.         //添加插件  
  21.         bean.setPlugins(new Interceptor[]{pageHelper});  
  22.         try {  
  23.             ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();  
  24.             bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));  
  25.             return bean.getObject();  
  26.         } catch (Exception e) {  
  27.             e.printStackTrace();  
  28.             return null;  
  29.         }  
  30.     }  
  31.   
  32.     @Bean  
  33.     public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {  
  34.         return new SqlSessionTemplate(sqlSessionFactory);  
  35.     }  
  36.   
  37.     @Bean  
  38.     @Override  
  39.     public PlatformTransactionManager annotationDrivenTransactionManager() {  
  40.         return new DataSourceTransactionManager(dataSource);  
  41.     }  
  42. }  

然后就是mybatis-config.xml:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.           
  3. <!DOCTYPE configuration  
  4.         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  5.         "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  6. <configuration>  
  7.   
  8.     <mappers>  
  9.         <mapper resource="mapper/UserMapper.xml"/>  
  10.     </mappers>  
  11.   
  12. </configuration>  
这样就找到了xml,UserMapper.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3.         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="com.test.xjs.demo.mapper.UserMapper">  
  6.   
  7.     <resultMap id="BaseResultMap" type="com.test.xjs.demo.domain.User">  
  8.         <id column="id" property="id" jdbcType="INTEGER" />  
  9.         <result column="name" property="name" jdbcType="VARCHAR" />  
  10.     </resultMap>  
  11.   
  12.     <select id="selectById" resultMap="BaseResultMap">  
  13.         select * from user_info where id = #{id}  
  14.     </select>  
  15.   
  16.     <select id="list" resultMap="BaseResultMap">  
  17.         select * from user_info  
  18.     </select>  
  19.   
  20. </mapper>  

然后就找到了UserMapper.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Mapper  
  2. public interface UserMapper {  
  3.     public User selectById(@Param("id"int id);  
  4.     public List<User> list();  
  5. }  
引用mapper:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @RestController  
  2. public class UserController {  
  3.       
  4.     @Autowired  
  5.     UserMapper userMapper;  
  6.   
  7.     @RequestMapping("/query/{page}/{pageSize}")  
  8.     public PageInfo query(@PathVariable Integer page, @PathVariable Integer pageSize) {  
  9.         if(page!= null && pageSize!= null){  
  10.             PageHelper.startPage(page, pageSize);  
  11.         }  
  12.         List<User> users = userMapper.list();  
  13.         return new PageInfo(users);  
  14.     }  
  15. }  

看下pom:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <groupId>com.test.xjs.demo</groupId>  
  6.   <artifactId>mybatis</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <modules>  
  9.     <module>../simple</module>  
  10.   </modules>  
  11.   <packaging>pom</packaging>  
  12.   <name>mybatis</name>  
  13.   <url>http://maven.apache.org</url>  
  14.   
  15.   <parent>  
  16.     <groupId>org.springframework.boot</groupId>  
  17.     <artifactId>spring-boot-starter-parent</artifactId>  
  18.     <version>1.3.0.RELEASE</version>  
  19.     <relativePath />  
  20.   </parent>  
  21.   
  22.   
  23.   <properties>  
  24.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  25.   </properties>  
  26.   
  27.   
  28.   <dependencies>  
  29.   
  30.     <dependency>  
  31.       <groupId>org.springframework.boot</groupId>  
  32.       <artifactId>spring-boot-starter-web</artifactId>  
  33.       <version>1.3.0.RELEASE</version>  
  34.     </dependency>  
  35.   
  36.     <dependency>  
  37.       <groupId>org.mybatis.spring.boot</groupId>  
  38.       <artifactId>mybatis-spring-boot-starter</artifactId>  
  39.       <version>1.1.1</version>  
  40.     </dependency>  
  41.   
  42.     <dependency>  
  43.       <groupId>com.alibaba</groupId>  
  44.       <artifactId>druid</artifactId>  
  45.       <version>1.0.5</version>  
  46.     </dependency>  
  47.   
  48.     <dependency>  
  49.       <groupId>mysql</groupId>  
  50.       <artifactId>mysql-connector-java</artifactId>  
  51.       <version>5.1.25</version>  
  52.     </dependency>  
  53.   
  54.     <!--分页插件-->  
  55.     <dependency>  
  56.       <groupId>com.github.pagehelper</groupId>  
  57.       <artifactId>pagehelper</artifactId>  
  58.       <version>4.1.1</version>  
  59.     </dependency>  
  60.   
  61.   </dependencies>  
  62.   
  63.   
  64. </project>  

参考:

1.集成mybatis,多数据源 http://blog.csdn.net/xiaoyu411502/article/details/48164311

2.集成mybatis,分页插件 http://blog.csdn.net/isea533/article/details/50359390

3.mybatis模糊查询 http://blog.csdn.net/zhang98722/article/details/6956571

4.spring boot静态资源处理 http://blog.csdn.net/isea533/article/details/50412212


源码下载:http://download.csdn.net/detail/goldenfish1919/9553362


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值