首页 > > > SpringBoot整合mybatis
在页面中输出值
可以将一个值放入到input标签的value中
#dates
#numbers
#strings
#bools
#lists
#sets
#maps
#aggregates
#messages
#ids
#dates
${#dates.format(key)} 格式化日期,默认的以浏览器默认语言为格式化标准
${#dates.format(key,'yyy/MM/dd')} 按照自定义的格式做日期转换
${#dates.year(key)} year:取年
${#dates.month(key)} Month:取月
${#dates.day(key)} Day:取日
条件判断
条件判断
遍历数据
<span th:text="${#httpServletRequest.getAttribute('req')}"></span>
<span th:text="${session.sess}"></span>
<span th:text="${application.app}"></span>
@{}
<a th:href="@{/show}">相对路径</a>
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
<a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>
<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}">相对路径-传参-restful</a>
<!-- Mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>编写application.properties文件
spring.datasource.url=jdbc:mysql://localhost:3306/kaoshi1
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.cyj.pojo
@MapperScan 用户扫描MyBatis的Mapper接口
1 在 mapper 接口中以及映射配置文件中添加相关代码
/**
* 查询所有用户
* @return
*/
List<Users> selectUsersAll();
2 在业务层中添加查询方法
<select id="selectUsersAll" resultType="users">
select id,name,age from users
</select>
3 在 Controller 中添加方法
/**
* 查询所有用户
* @param model
* @return
*/
@RequestMapping("/findUserAll")
public String findUserAll(Model model){
List<Users> list = usersService.selectUserAll();
model.addAttribute("list",list);
return "showUsers";
}
4 添加页面 showUsers.html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width: 500px;">
<tr>
<td>用户ID</td>
<td>用户姓名</td>
<td>用户年龄</td>
<td>操作</td>
</tr>
<tr th:each="user:${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td>
<a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a>
<a th:href="@{/users/delUser(id=${user.id})}">删除用户</a>
</td>
</tr>
</table>
</body>
</html>数据回显主要是前端的th:field=”${数据}”实现回显
1 更新用户之前的查询,并将数据在页面中回显
1.1修改 mapper 接口以及映射配置文件
/**
* 根据id查询user
* @param id
* @return
*/
Users selectUserById(Integer id);
<select id="selectUserById" resultType="users">
select id,name,age from users where id=#{value}
</select>
1.2修改业务层代码
Users findUserById(Integer id);
@Override
public Users findUserById(Integer id) {
return usersMapper.selectUserById(id);
}
1.3修改 Controller
/**
* 根据用户id查询用户
* @param id
* @return
*/
@RequestMapping("/findUserById")
public String findUserById(Integer id,Model model){
Users user = usersService.findUserById(id);
model.addAttribute("user",user);
return "updateUser";//将查询的数据回显
}
1.4添加页面 updateUsers.html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/users/editUser}" method="post">
<input type="hidden" name="id" th:field="${user.id}"/>
用户姓名:<input type="text" name="name" th:field="${user.name}"/><br/>
用户年龄:<input type="text" name="age" th:field="${user.age}"/><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>
</html>
1.5修改 showUsers.html 页面添加操作功能
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width: 500px;">
<tr>
<td>用户ID</td>
<td>用户姓名</td>
<td>用户年龄</td>
<td>操作</td>
</tr>
<tr th:each="user:${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td>
<a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a>
<a th:href="@{/users/delUser(id=${user.id})}">删除用户</a>
</td>
</tr>
</table>
</body>
</html>2 用户更新
2.1修改 mapper 接口以及映射配置文件
/**
* 更新用户
* @param users
*/
void updateUser(Users users);
<update id="updateUser" parameterType="users">
update users set name=#{name},age=#{age} where id=#{id}
</update>
2.2修改业务层代码
void updateUser(Users users);
@Override
public void updateUser(Users users) {
usersMapper.updateUser(users);
}
2.3修改 Controller
/**
* 更新用户
* @param users
* @return
*/
@RequestMapping("/editUser")
public String editUser(Users users){
usersService.updateUser(users);
return "ok";
}1 修改 mapper 接口以及映射配置文件
/**
* 根据用户id删除用户
* @param id
*/
void deleteUserById(Integer id);
<delete id="deleteUserById" >
delete from users where id=#{value}
</delete>
2 修改业务层代码
void deleteUserById(Integer id);
@Override
public void deleteUserById(Integer id) {
usersMapper.deleteUserById(id);
}
3 修改 Controller
/**
* 删除用户
* @param id
* @return
*/
@RequestMapping("/delUser")
public String delUser(Integer id){
usersService.deleteUserById(id);
return "redirect:/users/findUserAll";
}
4 修改 showUsers.html
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width: 500px;">
<tr>
<td>用户ID</td>
<td>用户姓名</td>
<td>用户年龄</td>
<td>操作</td>
</tr>
<tr th:each="user:${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td>
<a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a>
<a th:href="@{/users/delUser(id=${user.id})}">删除用户</a>
</td>
</tr>
</table>
</body>
</html>来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69952973/viewspace-3128368/ ,如需转载,请注明出处,否则将追究法律责任。