第八节:SpringBoot集成MyBatis
2017-09-05 10:17
417 查看
pom.xml
Controller
启动类
Mybatis使用分页插件PageHelper
pom.xml配置
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
Controller
@RequestMapping("/user")
public User selectAge(int age){
//第一个参数是第几页;第二个参数是每页显示条数。
PageHelper.startPage(1, 2);
return userMaper.Select(age);
}
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency>User实体类
package com.xiaowen.model; public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }UserMapper
package com.xiaowen.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.xiaowen.model.User; @Mapper public interface UserMaper { @Select("select * from t_user where age = #{age}") User Select(int age); }
Controller
package com.xiaowen.controller; import com.xiaowen.mapper.UserMaper; import com.xiaowen.model.User; @RestController public class WebController { @Autowired private UserMaper userMaper; @RequestMapping("/user") public User selectAge(int age){ return userMaper.Select(age); } }
启动类
package com.xiaowen; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); } }浏览器访问:http://localhost:8088/user?age=10
Mybatis使用分页插件PageHelper
pom.xml配置
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
package com.xiaowen.util; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.PageHelper; /** * 注册MyBatis分页插件PageHelper * @author xiaowen * */ @Configuration public class MybatisConf { @Bean public PageHelper pageHelper(){ PageHelper pageHelper=new PageHelper(); Properties p=new Properties(); p.setProperty("offsetAsPageNum", "true"); p.setProperty("rowBoundsWithCount", "true"); p.setProperty("reasonable", "true"); pageHelper.setProperties(p); return pageHelper; } }
Controller
@RequestMapping("/user")
public User selectAge(int age){
//第一个参数是第几页;第二个参数是每页显示条数。
PageHelper.startPage(1, 2);
return userMaper.Select(age);
}
相关文章推荐
- SpringBoot实践:集成Mybatis-Plus
- Spring Boot实践--集成Mybatis和多数据源配置
- Spring Boot集成Mybatis
- Spring Boot入门——集成Mybatis
- 从0开始学习SpringCould(5)--SpringBoot 集成Mybatis使用Druid数据源
- springboot如何集成mybatis的pagehelper分页插件
- spring boot和mybatis集成分页插件
- SpringBoot集成MyBatis开启事务
- springboot+mybatis、JPA+swagger2完美集成
- Spring Boot 集成MyBatis
- SpringBoot入门-17(springboot集成mybatis注解形式增删查改properties配置)
- 在springboot项目中使用mybatis 集成 Sharding-JDBC
- 在Spring Boot中集成Mybatis
- 10.玩转Spring Boot 集成Mybatis
- Spring Boot集成MyBatis访问数据库
- SpringBoot学习(3.2)——集成Mybatis, xml方式
- Spring Boot 集成Mybatis实现主从(多数据源)分离方案示例
- Spring Boot 集成Mybatis实现主从(多数据源)分离方案
- Spring Boot (二)集成Mybatis、Druid
- SpringBoot学习笔记(4) Spring Boot 集成 Mybatis