Springboot如何实现mybatis与pagehelper的完美整合?
- 内容介绍
- 文章标签
- 相关推荐
本文共计997个文字,预计阅读时间需要4分钟。
Spring Boot 集成 MyBatis 和 PageHelper,在 pom.xml 中添加 Tomcat、MyBatis 和 MySQL 依赖:
xml org.springframework.boot spring-boot-starter-tomcat mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter
Springboot集成mybatis和pagehelperSpringboot集成mybatis
1、在pom.xml中添加tomcat,mybaits,mysql依赖
jar
UTF-8
UTF-8
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///springboot username: root password: admin #springboot支持c3p0,dbcp,如果需要使用其他的数据库连接池,指定type # type: 其他的使用指定的dataSource, 比如druid mybatis: type-aliases-package: org.com.cay.entity mapper-locations: - classpath:org/com/cay/mapper/*.xml configuration-properties: offsetAsPageNum: true rowBoundsWithCount: true reasonable: true IPersonMapper.java
package org.com.cay.dao; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; import org.com.cay.entity.Person; public interface IPersonMapper { @Select("select * from Person where username = #{username}") public List
likeUsernName(String username); @Select("select * from Person where id = #{id}") public Person getById(Integer id); @Select("select username from Person where id = #{id}") public String getPersonUsernameById(Integer id); public List
getAll(); @Insert("insert into person(username,password,age) values(#{username},#{password},#{age})") @Options(keyColumn="id", keyProperty="id", useGeneratedKeys=true) public void save(Person person); } IPersonMapper.xml
package org.com.cay.config; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.PageHelper; //@Configuration public class MybatisConfig { @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum", "true"); properties.setProperty("rowBoundsWithCount", "true"); properties.setProperty("reasonable", "true"); pageHelper.setProperties(properties); return pageHelper; } } PersonController.java
package org.com.cay.controller; import java.util.List; import org.com.cay.entity.Person; import org.com.cay.service.IPersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageHelper; @RestController @RequestMapping("/person") public class PersonController { @Autowired private IPersonService personService; @RequestMapping("/byusername") public List
likeUsernName(String username) { return personService.likeUsernName(username); } @RequestMapping("/{id}") public Person getById(@PathVariable Integer id) { return personService.getById(id); } @RequestMapping("/byid") public String getPersonUsernameById(Integer id) { return personService.getPersonUsernameById(id); } @RequestMapping("/list") public List
getAll(@RequestParam(defaultValue = "1", required = false, name = "pageNum") int pageNum, @RequestParam(defaultValue = "2", required = false, name = "pageSize") int pageSize) { // pageNum: 第几页 // pageSize: 每页多少条数据 PageHelper.startPage(pageNum, pageSize); return personService.getAll(); } @RequestMapping("/save") public Person save(){ Person person = new Person(); person.setUsername("aaaaa"); person.setPassword("111111"); person.setAge(23); return personService.save(person); } }
本文共计997个文字,预计阅读时间需要4分钟。
Spring Boot 集成 MyBatis 和 PageHelper,在 pom.xml 中添加 Tomcat、MyBatis 和 MySQL 依赖:
xml org.springframework.boot spring-boot-starter-tomcat mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter
Springboot集成mybatis和pagehelperSpringboot集成mybatis
1、在pom.xml中添加tomcat,mybaits,mysql依赖
jar
UTF-8
UTF-8
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///springboot username: root password: admin #springboot支持c3p0,dbcp,如果需要使用其他的数据库连接池,指定type # type: 其他的使用指定的dataSource, 比如druid mybatis: type-aliases-package: org.com.cay.entity mapper-locations: - classpath:org/com/cay/mapper/*.xml configuration-properties: offsetAsPageNum: true rowBoundsWithCount: true reasonable: true IPersonMapper.java
package org.com.cay.dao; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; import org.com.cay.entity.Person; public interface IPersonMapper { @Select("select * from Person where username = #{username}") public List
likeUsernName(String username); @Select("select * from Person where id = #{id}") public Person getById(Integer id); @Select("select username from Person where id = #{id}") public String getPersonUsernameById(Integer id); public List
getAll(); @Insert("insert into person(username,password,age) values(#{username},#{password},#{age})") @Options(keyColumn="id", keyProperty="id", useGeneratedKeys=true) public void save(Person person); } IPersonMapper.xml
package org.com.cay.config; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.PageHelper; //@Configuration public class MybatisConfig { @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum", "true"); properties.setProperty("rowBoundsWithCount", "true"); properties.setProperty("reasonable", "true"); pageHelper.setProperties(properties); return pageHelper; } } PersonController.java
package org.com.cay.controller; import java.util.List; import org.com.cay.entity.Person; import org.com.cay.service.IPersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageHelper; @RestController @RequestMapping("/person") public class PersonController { @Autowired private IPersonService personService; @RequestMapping("/byusername") public List
likeUsernName(String username) { return personService.likeUsernName(username); } @RequestMapping("/{id}") public Person getById(@PathVariable Integer id) { return personService.getById(id); } @RequestMapping("/byid") public String getPersonUsernameById(Integer id) { return personService.getPersonUsernameById(id); } @RequestMapping("/list") public List
getAll(@RequestParam(defaultValue = "1", required = false, name = "pageNum") int pageNum, @RequestParam(defaultValue = "2", required = false, name = "pageSize") int pageSize) { // pageNum: 第几页 // pageSize: 每页多少条数据 PageHelper.startPage(pageNum, pageSize); return personService.getAll(); } @RequestMapping("/save") public Person save(){ Person person = new Person(); person.setUsername("aaaaa"); person.setPassword("111111"); person.setAge(23); return personService.save(person); } }

