步骤总览 :
① 在pom.xml中引入相关依赖:
(1) JDK版本号
(2) mysql驱动, mybatis依赖, mybatis分页PageHelper
② 在application.properties中添加配置信息;
③ 编写Mapper接口;
④ 编写Service;
⑤ 编写Controller;
⑥ 添加分页插件;
⑦ 获取自增长ID;
步骤1 :
在pom.xml中引入依赖:
<properties>
<!-- 指定编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 指定JDK版本 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springboot对web的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 对JSP页面的支持 start -->
<!-- servlet的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- jstl的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 对JSP页面的支持 end -->
<!-- 配置tomcat start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 配置tomcat end-->
<!-- mysql数据库驱动 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mysql数据库驱动 end -->
<!-- springboot对mybatis的依赖 start -->
<!-- 不要使用1.0.0版本, 因为不支持拦截器插件 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- springboot对mybatis的依赖 end -->
<!-- 分页插件PageHelper start -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
<!-- 分页插件PageHelper end -->
</dependencies>
步骤2 :
在application.properties中添加配置信息 :
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
步骤3 :
编写Mapper:
package online.bendou.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import online.bendou.entity.User;
public interface UserMapper {
@Insert("insert into user_info(name) values(#{name})")
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
public void add(User user);
@Select("select * from user_info")
public List<User> findPage();
}
步骤4 :
编写Service:
package online.bendou.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import online.bendou.entity.User;
import online.bendou.mapper.UserMapper;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Transactional
public void add(User user){
userMapper.add(user);
}
public List<User> findPage() {
/**
* 开启分页(需首先配置, 见页尾):
* 参数1: 当前页
* 参数2: 每页多少条
*/
PageHelper.startPage(1, 3);
return userMapper.findPage(); }
}
步骤5 :
package online.bendou.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import online.bendou.entity.User;
import online.bendou.service.UserService;
/**
* 测试整合的mybatis
* @author Lx
*
*/
@Controller
@RequestMapping("/mybatis")
public class MybatisController {
@Autowired
private UserService userService;
@RequestMapping("/add")
@ResponseBody
public User add(){
User user = new User();
user.setName("张三");
userService.add(user);
return user;
}
@GetMapping("/findPage")
@ResponseBody
public List<User> findPage(){
return userService.findPage();
}
}
步骤6 :
配置PageHelper;
创建MybatisConfiguration.java
package online.bendou.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 MybatisConfiguration {
@Bean
public PageHelper pageHelper(){
PageHelper ph = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
ph.setProperties(p);
return ph;
}
}
步骤7 :
返回自增长ID;
在Mapper接口的Insert方法加上@Options注解来获取自增长ID:
package online.bendou.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import online.bendou.entity.User;
public interface UserMapper {
@Insert("insert into user_info(name) values(#{name})")
@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
public void add(User user);
}
步骤8 :
启动测试!