前言:Mybatis-Plus是Mybatis的增强插件,具有许多强大功能:如内置代码生成器,分页插件,CRUD操作等,专为偷懒而生官网链接
~demo效果:基于官网的快速入门,体验Mybatis-plus的便捷,结合SpringBoot渲染前端展示查询效果
1.项目所需依赖
<dependencies>
<!--数据库连接-->
<!--连接数据库 默认数据源-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.配置数据源+连接数据库
- application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
- sql代码
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'Jone', '18', 'test1@baomidou.com');
INSERT INTO `user` VALUES ('2', 'Jack', '20', 'test2@baomidou.com');
INSERT INTO `user` VALUES ('3', 'Tom', '28', 'test3@baomidou.com');
INSERT INTO `user` VALUES ('4', 'Sandy', '21', 'test4@baomidou.com');
INSERT INTO `user` VALUES ('5', 'Billie', '24', 'test5@baomidou.com');
3.编写Mapper继承自BaseMapper
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {
}
~ 测试
@SpringBootTest
class MybatisPlusSpringbootApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
// 参数是一个条件构造器 null 为查询表中全部数据
List<User> users = userMapper.selectList(null);
// 语法糖
users.forEach(System.out::println);
}
}
4.编写前端控制器
@Controller
public class UserController {
@Autowired
UserMapper userMapper;
/**
* 查询所有用户
* @return
*/
@RequestMapping("/user/list")
public String userList(Model model){
List<User> users = userMapper.selectList(null);
model.addAttribute("user_lists", users);
return "user/list";
}
}
@Controller
public class RouteController {
// 首页
@RequestMapping({"/","/index"})
public String index(){
return "dashboard";
}
}
5.部分前端页面接收参数
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h2>后台管理员列表</h2>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</thead>
<!--数据 遍历 集合-->
<tbody>
<tr th:each="user:${user_lists}">
<td th:text="${user.getId()}"></td>
<td th:text="${user.getName()}"></td>
<td th:text="${user.getAge()}"></td>
<td th:text="${user.getEmail()}"></td>
<td>
<a class="btn btn-sm btn-warning" th:href="@{/user/toupdate(id=${user.getId()})}">修改</a>
<span> | </span>
<a class="btn btn-sm btn-danger" th:href="@{/user/del(id=${user.getId()})}">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
</main>
~效果
查询用户集合
网友评论