SpringBoot基础教程(六) | Mybatis补充篇
Mybatis插件介绍
- Mapper
通用Mapper就是为了解决单表增删改查,基于Mybatis的插件。开发人员不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法。
官方文档地址:https://gitee.com/free/Mapper/wikis/Home
- PageHelper
PageHelper是国内非常优秀的一款开源的 mybatis 分页插件,它支持基本主流与常用的数据库,例如 Oracle、Mysql、MariaDB、SQLite、Hsqldb 等。
官方文档地址:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
导入项目依赖
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<!--pageHelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>编写配置文件
##### 数据库相关配置 ##### spring.datasource.username=root spring.datasource.password=yzhroot spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=CTT ##### mapper相关配置 ##### # 指明继承mapper相关接口的类,若有多个用逗号隔开 mapper.mappers=training.springboot.mapper.dao.CustomMapper # 主键自增回写方法,默认值MYSQL mapper.identity=MYSQL ##### mybatis相关配置 ##### # 支持驼峰式与下划线转换 mybatis.configuration.map-underscore-to-camel-case=true #### pageHelper相关配置 ##### # 数据库方言 pagehelper.helper-dialect=mysql # 合理化页数,防止页数大于最大值或者小于0 pagehelper.reasonable=true # 支持通过mapper接口来传递分页参数 pagehelper.support-methods-arguments=true
编写实体类
/**
* 用户实体类
*/
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private Date createDate;
private Department department;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", createDate=" + createDate +
'}';
}
}这里使用的方式与JPA是一样的,其中@Id注解是为了告诉Mapper谁是主键,若没设置主键,在根据主键查询时,所有字段会被作为联合主键查询。
编写持久层代码
/**
* 自定义的基础mapper
* @param <T>
*/
public interface CustomMapper<T> extends MySqlMapper<T>, Mapper<T> {
}@Mapper
public interface UserDao extends CustomMapper<User> {
}这里的MySqlMapper接口和Mapper接口与JPA中JpaRepository接口类似,都是帮我们实现一些基础的CRUD功能。
测试
@SpringBootTest
class MapperApplicationTests {
@Autowired
DepartmentDao departmentDao;
@Autowired
UserDao userDao;
/**
* mapper相关测试
*/
//测试批量插入数据
@Test
public void testBatchInsertUser(){
List<User> users = new ArrayList<>();
for (int i = 0; i < 50; i++) {
User user = new User();
user.setUsername("小明"+(i+1));
user.setCreateDate(new Date());
users.add(user);
}
userDao.insertList(users);
}
//根据主键查询元素
@Test
public void testQueryObjByKey(){
System.out.println(userDao.selectByPrimaryKey(1328));
}
//根据id修改元素
@Test
public void testUpdateObjByKey(){
User user = userDao.selectByPrimaryKey(1328);
System.out.println("修改前:"+ user);
user.setUsername("小芳");
userDao.updateByPrimaryKey(user);
System.out.print("修改后:"+userDao.selectByPrimaryKey(1328));
}
//根据id删除元素
@Test
public void testDelObjByPriKey(){
System.out.println(userDao.deleteByPrimaryKey(1328));
}
//查询所有元素
@Test
public void testQueryAllUsers(){
System.out.println(userDao.selectAll());
}
//插入一条元素,并获取刚插入的主键
@Test
public void testInsertUseGeneratedKeysMapper(){
User user = new User();
user.setUsername("小刚");
userDao.insertUseGeneratedKeys(user);
System.out.println(user.getId());
}
//使用Example对象排序
@Test
public void testExampleOrderBy(){
Example example = new Example(User.class);
example.orderBy("id").asc();
System.out.println(userDao.selectByExample(example));
}
//使用Example对象的动态SQL
@Test
public void testExampleSql(){
Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("username","小明5");
System.out.println(userDao.selectByExample(example));
}
/**
* pageHelper使用
*/
@Test
public void testPageHelper(){
PageHelper.startPage(1,10);
System.out.println(userDao.selectAll());
}
}我只是简单的测试了一些基础的功能,若读者有兴趣,可以去参考官方文档。
广发银行公司氛围 23人发布