1.pom.xml
<!-- 数据库连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 引入静态模板引擎,才能访问templates下面的静态页面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- web开发 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 配置文件提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 日志、getset方法等 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatisplus整合连接数据库,包含了jdbc包(和mybatis整合一样都包含)-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2.application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/graduationproject
username: root
password: 113846
driver-class-name: com.mysql.jdbc.Driver #5.x版本mysql
3.bean
@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("user") //指定数据库表名字
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
@AllArgsConstructor:lombok包实现的有参构造器
@NoArgsConstructor:springboot启动后对bean生成无参构造器
@Data:生成get()、set()、toString()方法
@TableName:指定数据库中的表名
4.mapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
@Mapper:标识该类为bean,当springboot启动后自动注入组件到ioc容器,后续在服务实现层能通过@Autowired注解注入该bean,实现数据库表的增删查改等操作。
BaseMapper<User>:mybatisplus的jar包中的接口类,源码中Mapper继承该接口后,无需编写mapper.xml文件,即可获得CURD功能。并<T>为该Mapper支持泛型,在本次BaseMapper测试中泛型为User。
5.service和serviceImpl
@Service
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
ServiceImpl:源码中ServiceImpl<M extends BaseMapper<T>, T> implements IService<T>,其中IService 实现类( 泛型:M 是 mapper 对象,T 是泛型实体 )。
6.controller
(1).查询分页功能
@Slf4j
@Controller
public class PagingAndCurdTestController {
@Autowired
UserService userService;
//RequestParam:请求参数
@RequestMapping("/dynamic_table")
public String getDynamicTable(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
Model model){
//查询该表所有对象
List<User> list = userService.list();
// model.addAttribute("lists",list);
//分页查询数据,pn第几页,2 每页显示记录数
Page<User> userPage = new Page<>(pn,2);
//分页查询结果
Page<User> page = userService.page(userPage,null);
model.addAttribute("pages",page);
return "table/dynamic_table";
}
}
(2).删除功能
//PathVariable:路径参数
@GetMapping("/user/delete/{id}")
public String deleteUser(@PathVariable("id") Long id,
@RequestParam(value = "pn",defaultValue = "1") Integer pn,
RedirectAttributes redirectAttributes){
log.info("删除的用户id={}",id);
userService.removeById(id);
//重定向 ,携带参数
redirectAttributes.addAttribute("pn",pn);
return "redirect:/dynamic_table";
}
7.resoutces\templates\table\dynamic_table.html
<section class="panel">
<header class="panel-heading">
Dynamic Table
<span class="tools pull-right">
<a href="javascript:;" class="fa fa-chevron-down"></a>
<a href="javascript:;" class="fa fa-times"></a>
</span>
</header>
<div class="panel-body">
<div class="adv-table">
<table class="display table table-bordered table-striped" id="dynamic-table">
<thead>
<tr>
<th>Rendering engine</th>
<th>id</th>
<th>Browser</th>
<th>Platform(s)</th>
<th class="hidden-phone">Engine version</th>
<th class="hidden-phone">CSS grade</th>
</tr>
</thead>
<tbody>
<tr class="gradeX" th:each="users,stat:${pages.records}">
<td th:text="${stat.count}">Trident</td>
<td th:text="${users.id}">id</td>
<td th:text="${users.name}">Internet
Explorer 4.0
</td>
<td th:text="${users.age}">Win 95+</td>
<td class="center hidden-phone">[[${users.email}]]</td>
<td>
<a th:href="@{/user/delete/{id}(id=${users.id},pn=${pages.current})}" class="btn btn-danger btn-sm" type="button">删除</a>
</td>
</tr>
</tbody>
</table>
<div class="row-fluid"><div class="span6">
<div class="dataTables_info" id="dynamic-table_info">
当前第[[${pages.current}]]页,总计[[${pages.pages}]]页,共[[${pages.total}]]条记录
</div>
</div>
<div class="span6"><div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev disabled">
<a href="#">← Previous</a>
</li>
<li th:class="${num==pages.current?'active':''}" th:each="num:${#numbers.sequence(1,pages.pages)}">
<a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
</li>
<li class="next disabled">
<a href="#">Next → </a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</section>
(1).
th:each:循环对象集合
stat:thymeleaf的内置变量stat,可以获取对象集合的index、count、first、last等属性值
pages.records:在分页类Page<T>的源码中records为对象集合
th:text:获取Model中存储的文本值
th:href:路径跳转
(2).
[[${pages.current}]]:标签外写法,获取分页类Page<T>的源码中的current为当前页数的值
[[${pages.pages}]]:标签外写法,获取分页类Page<T>的源码中的pages为数据集合全部页数的值
[[${pages.total}]]:标签外写法,获取分页类Page<T>的源码中的total为全部对象条数的值
th:class="${num==pages.current?'active':''} :在li标签封装中遍历显示页数值,如果num值等于当前页数current,高亮显示
8.web页面展示
http://localhost:8080/dynamic_table