Springboot集成mybatis和pagehelper Springboot集成mybatis1、在pom.xml中添加tomcat,mybaits,mysql依赖 org.springframework.boot spring-boot-starter-tomcat mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-sta
Springboot集成mybatis 1、在pom.xml中添加tomcat,mybaits,mysql依赖pom.xmlorg.springframework.boot spring-boot-starter-tomcatmysql mysql-connector-java2、在启动类上添加@MapperScan,用于配置扫描mybatis的接口类扫描包 3、代码编写 4、在application.yml中填写数据库配置(必填)和mybatis配置(可选) 问题: 1、Unregistering JMX-exposed beans on shutdown 原因:可能是org\apache\tomcat\embed\tomcat-embed-core下的包下载不完整 解决方案:先删除后,在update maven ---------------------------------------------------- mybatis中加入分页 1、在pom.xml中添加pagehelper依赖 org.mybatis.spring.boot mybatis-spring-boot-starter${mybatis.version} 2、配置pagehelper属性 方法(1)、在application.yml中配置pagehelper mybatis: #... 其他配置信息 configuration-properties: offsetAsPageNum: true rowBoundsWithCount: true reasonable: true 方法(2)、新建一个类,使用@Configuration注解表示该类为一个配置类 @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; } } 3、在需要分页的方法中使用PageHelper.startPage(int pageNum,int pageSize); 例如:PageHelper.startPage(2, 2); -------------------------------------------------- mybaits获取主键 在@Insert的方法上添加@Options @Options(keyColumn="id", keyProperty="id", useGeneratedKeys=true) com.github.pagehelper pagehelper-spring-boot-starter${pagehelper.version}
application.yml4.0.0 org.com.cay.spring-boot spring-boot-mybatis0.0.1-SNAPSHOT jar spring-boot-mybatis Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent1.5.6.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-tomcatorg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-webmysql mysql-connector-javaorg.mybatis.spring.boot mybatis-spring-boot-starter1.3.0 com.github.pagehelper pagehelper-spring-boot-starter1.1.2 org.springframework.boot spring-boot-maven-plugin
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
MybatisConfig.java
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); } }
