整合MyBatisPlus操作数据库
1、什么是MyBatis-Plus
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做
改变,为简化开发、提高效率而生。
优点
只需要我们的Mapper继承 BaseMapper 就可以拥有crud能力
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
编写实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
//下面两个不是数据库字段,所以采用注解方式注释掉
@TableField(exist = false)
private String username;
@TableField(exist = false)
private String password;
//下面是数据库字段
private Long id;
private String name;
private Integer age;
private String email;
}
编写UserMapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
测试
@Autowired
UserMapper userMapper;
@Test
void testUserMapper(){
User user = userMapper.selectById(1L);
log.info("用户信息{}",user);
}
数据列表展示
使用MyBatis Plus提供的IService,ServiceImpl,减轻Service层开发工作。
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
Controller
@Autowired
UserService userService;
@GetMapping("/dynamic_table.html")
public String dynamic_table(Model model) {
//从数据库中查出user表中的用户进行展示
List<User> list = userService.list();
model.addAttribute("users",list);
return "table/dynamic_table";
}
<tbody>
<tr class="gradeX" th:each="user,stats:${users}">
<td th:text="${stats.count}">Trident</td>
<td th:text="${user.id}">id</td>
<td th:text="${user.name}">Internet
Explorer 4.0</td>
<td th:text="${user.age}">Win 95+</td>
<td class="center hidden-phone">[[${
user.email}]]</td>
<td class="center hidden-phone">X</td>
</tr>
</tbody>
实现分页功能
@GetMapping("/dynamic_table.html")
public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
Model model) {
//分页查询数据
Page<User> userpage = new Page<>(pn, 2);
//分页查询的结果
Page<User> page = userService.page(userpage, null);
long current = page.getCurrent();//代表当前第几页
long pages = page.getPages();//当前分页总页数
long total = page.getTotal();//列表
List<User> records = page.getRecords();
model.addAttribute("users",page);
return "table/dynamic_table";
}
<table class="display table table-bordered table-striped" id="dynamic-table">
<thead>
<tr>
<th>#</th>
<th>id</th>
<th>name</th>
<th>age</th>
<th>email</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr class="gradeX" th:each="user: ${users.records}">
<td th:text="${user.id}"></td>
<td>[[${user.name}]]</td>
<td th:text="${user.age}">Win 95+</td>
<td th:text="${user.email}">4</td>
<td>
<a th:href="@{/user/delete/{id}(id=${user.id},pn=${users.current})}"
class="btn btn-danger btn-sm" type="button">删除</a>
</td>
</tr>
</tfoot>
</table>
<div class="row-fluid">
<div class="span6">
<div class="dataTables_info" id="dynamic-table_info">
当前第[[${users.current}]]页 总计 [[${users.pages}]]页
共[[${users.total}]]条记录
</div>
</div>
<div class="span6">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class=""><a th:href="@{/dynamic_table.html(pn=${users.current-1})}">←
前一页</a></li>
<li th:class="${num == users.current?'active':''}"
th:each="num:${#numbers.sequence(1,users.pages)}" >
<a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
</li>
<li class=""><a th:href="@{/dynamic_table.html(pn=${users.current+1})}">
下一页→ </a></li>
</ul>
</div>
</div>
</div>
实现删除用户
@GetMapping("/user/delete/{id}")
public String deleteUser(@PathVariable("id") Long id,
@RequestParam(value="pn",defaultValue = "1")Integer pn,
RedirectAttributes ra){
userService.removeById(id);
ra.addAttribute("pn",pn);
return "redirect:/dynamic_table.html";
}
<td>
<a th:href="@{/user/delete/{id}(id=${user.id},pn=${users.current})}"
class="btn btn-danger btn-sm">删除</a>
</td>