SpringBoot-09整合MyBatis
来源:互联网 发布:莱恩打碟软件安装教程 编辑:程序博客网 时间:2024/06/05 07:26
步骤总览 :
① 在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=.jspspring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.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;@Servicepublic 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;@Configurationpublic 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 :
启动测试!
阅读全文
0 0
- SpringBoot-09整合MyBatis
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot和Mybatis整合
- springboot 整合mybatis
- springboot+mybatis+dubbo整合
- SpringBoot整合MyBatis
- SpringBoot整合Mybatis
- springBoot整合mybatis
- springboot与mybatis整合
- springboot和mybatis整合
- Springboot整合Mysql+Mybatis
- springboot、mybatis、activemq整合
- SpringBoot整合MyBatis
- SpringBoot 整合mybatis
- SpringBoot + Mybatis 整合demo
- springboot整合mybatis
- SpringBoot-整合MyBatis
- Springboot 整合 Mybatis
- TypeScript 变量声明
- 上传项目到git仓库
- Docker镜像仓库Harbor之搭建及配置
- vuex(入门软文)
- Rescue
- SpringBoot-09整合MyBatis
- 赋值变量给短信模板
- iOS 关于两个Tab嵌套的使用
- Android Ubuntu下FFmpeg的编译教程
- dubbo学习——(一)dubbo编译启动
- 杨辉三角
- IntelliJ IDEA meven的配置文件管理
- HTML5新标签
- jco接口开发(java服务端代码)