Spring Boot 통합 MyBatis 와 페이지 플러그 인

5237 단어 자바spring
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);
    }
}

그리고 my batis - config. xml:

        



    
        
    

이렇게 해서 xml, UserMapper. xml 을 찾 았 습 니 다.




    
        
        
    

    

    



그리고 UserMapper. java 를 찾았어 요.
@Mapper
public interface UserMapper {
    public User selectById(@Param("id") int id);
    public List list();
}
mapper 참조:
@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 users = userMapper.list();
        return new PageInfo(users);
    }
}

pom 보기:

  4.0.0

  com.test.xjs.demo
  mybatis
  0.0.1-SNAPSHOT
  
    ../simple
  
  pom
  mybatis
  http://maven.apache.org

  
    org.springframework.boot
    spring-boot-starter-parent
    1.3.0.RELEASE
    
  


  
    UTF-8
  


  

    
      org.springframework.boot
      spring-boot-starter-web
      1.3.0.RELEASE
    

    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.1.1
    

    
      com.alibaba
      druid
      1.0.5
    

    
      mysql
      mysql-connector-java
      5.1.25
    

    
    
      com.github.pagehelper
      pagehelper
      4.1.1
    

  




참고:
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

좋은 웹페이지 즐겨찾기