转载自:http://blog.csdn.net/goldenfish1919/article/details/51706316
1.MyBatisConfig.java
- @Configuration
- @EnableTransactionManagement
- public class MyBatisConfig implements TransactionManagementConfigurer {
- @Autowired
- DataSource dataSource;
- @Bean(name = "sqlSessionFactory")
- public SqlSessionFactory sqlSessionFactoryBean() {
- SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
- bean.setDataSource(dataSource);
- //分页插件
- PageHelper pageHelper = new PageHelper();
- Properties props = new Properties();
- props.setProperty("reasonable", "true");
- props.setProperty("supportMethodsArguments", "true");
- props.setProperty("returnPageInfo", "check");
- props.setProperty("params", "count=countSql");
- pageHelper.setProperties(props);
- //添加插件
- bean.setPlugins(new Interceptor[]{pageHelper});
- try {
- ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
- bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
- return bean.getObject();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- @Bean
- public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
- return new SqlSessionTemplate(sqlSessionFactory);
- }
- @Bean
- @Override
- public PlatformTransactionManager annotationDrivenTransactionManager() {
- return new DataSourceTransactionManager(dataSource);
- }
- }
然后就是mybatis-config.xml:
- <?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>
- <mappers>
- <mapper resource="mapper/UserMapper.xml"/>
- </mappers>
- </configuration>
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.test.xjs.demo.mapper.UserMapper">
- <resultMap id="BaseResultMap" type="com.test.xjs.demo.domain.User">
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- </resultMap>
- <select id="selectById" resultMap="BaseResultMap">
- select * from user_info where id = #{id}
- </select>
- <select id="list" resultMap="BaseResultMap">
- select * from user_info
- </select>
- </mapper>
然后就找到了UserMapper.java
- @Mapper
- public interface UserMapper {
- public User selectById(@Param("id") int id);
- public List<User> list();
- }
- @RestController
- public class UserController {
- @Autowired
- UserMapper userMapper;
- @RequestMapping("/query/{page}/{pageSize}")
- public PageInfo query(@PathVariable Integer page, @PathVariable Integer pageSize) {
- if(page!= null && pageSize!= null){
- PageHelper.startPage(page, pageSize);
- }
- List<User> users = userMapper.list();
- return new PageInfo(users);
- }
- }
看下pom:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.test.xjs.demo</groupId>
- <artifactId>mybatis</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <modules>
- <module>../simple</module>
- </modules>
- <packaging>pom</packaging>
- <name>mybatis</name>
- <url>http://maven.apache.org</url>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.3.0.RELEASE</version>
- <relativePath />
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>1.3.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.1.1</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.5</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.25</version>
- </dependency>
- <!--分页插件-->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- <version>4.1.1</version>
- </dependency>
- </dependencies>
- </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