参考博客:Spring Boot干货系列:(九)数据存储篇-SQL关系型数据库之MyBatis的使用
Mybatis注解的方式比较简单,只要定义一个dao接口,然后sql语句通过注解写在接口方法上。最后给这个接口添加@Mapper注解或者在启动类上添加@MapperScan(“com.springboot.dao”)注解都行。
如果使用分页插件pagehelper的话,需要在pom.xml中添加依赖。
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
com.alibaba
fastjson
1.2.31
org.apache.commons
commons-lang3
3.3
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.10
这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖。
MyBatis-Spring-Boot-Starter依赖将会提供如下:
就是说,使用了该Starter之后,只需要定义一个DataSource即可(application.properties中可配置),它会自动创建使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。会自动扫描你的Mappers,连接到SqlSessionTemplate,并注册到Spring上下文中。
在src/main/resources/application.properties中配置数据源信息:
spring.datasource.url = jdbc:mysql://localhost:3306/spring?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root0
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
在使用@Mapper注解方式代替XXmapper.xml配置文件、使用@Select等注解配置SQL语句的情况下,配置数据库字段名到JavaBean实体类属性命的自动驼峰命名转换:
# 设为true表示开启驼峰转换
mybatis.configuration.mapUnderscoreToCamelCase=true
实体对象:
public class LearnResource {
private Long id;
private String author;
private String title;
private String url;
//get & set 方法
}
Controller层:
@Controller
@RequestMapping("/learn")
public class LearnController {
@Autowired
private LearnService learnService;
@RequestMapping("t")
public String learn(){
return "learn-resource";
}
@RequestMapping(value = "queryLearnList",method = RequestMethod.POST,produces="application/json;charset=UTF-8")
@ResponseBody
public void queryLearnList(HttpServletRequest request , HttpServletResponse response){
int page = Integer.parseInt(request.getParameter("page")); // 取得当前页数,注意这是jqgrid自身的参数
int rows = Integer.parseInt(request.getParameter("rows")); // 取得每页显示行数,,注意这是jqgrid自身的参数
PageInfo pageObj =learnService.queryLearnResourceList(page, rows);
List
Service层接口及实现:
package com.springboot.service;
import com.github.pagehelper.PageInfo;
import com.springboot.entity.LearnResource;
public interface LearnService {
int add(LearnResource learnResource);
int delete(String id);
PageInfo queryLearnResourceList(int page, int size);
}
@Service
public class LearnServiceImpl implements LearnService {
@Autowired
LearnMapper learnMapper;
@Override
public int add(LearnResource learnResource) {
return this.learnMapper.add(learnResource);
}
@Override
public int delete(String id) {
return this.learnMapper.delete(id);
}
@Override
public PageInfo queryLearnResourceList(int page, int size) {
PageHelper.startPage(page, size);
List learnResourceList = learnMapper.learnResourceList();
PageInfo pageInfoDemo = new PageInfo(learnResourceList);
return pageInfoDemo;
}
}
使用PageHelper时,在查询list之前使用PageHelper.startPage(int pageNum, int pageSize)方法。其中pageNum是第几页,pageSize是每页多少条。
分页插件PageHelper项目地址: https://github.com/pagehelper/Mybatis-PageHelper
Dao接口:
@Mapper
public interface LearnMapper {
@Insert("insert into learn_resource(author, title,url) values(#{author},#{title},#{url})")
int add(LearnResource learnResource);
@Delete("delete from learn_resource where id = #{id}")
int delete(String id);
@Select("select * from learn_resource order by id")
List learnResourceList();
}
最终项目效果如下,实现了对数据库的部分基本操作。