基于【springboot篇】六. springboot中的事务控制
springboot中的crud
项目准备
- 创建项目day64-springboot-05-crud
- 项目配置参考上一章
- 新建数据库day64-springboot-crud
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(8) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `pwd` varchar(20) DEFAULT NULL, `age` int(3) DEFAULT NULL, `money` double(8,2) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'wpj', '123456', '23', '10000.00'); INSERT INTO `user` VALUES ('2', 'czh', '123456', '22', '10000.00');
- 创建实体类User
package com.wpj.bean; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.annotations.TableName; import com.baomidou.mybatisplus.enums.IdType; import lombok.Data; @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Integer id; private String name; private String pwd; private Integer age; private Double money; }
- 创建mapper接口和service及其impl
package com.wpj.mapper; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.wpj.bean.User; public interface IUserMapper extends BaseMapper<User> { }
package com.wpj.service; import com.wpj.baseservice.IBaseService; import com.wpj.bean.User; public interface IUserService extends IBaseService<User> { }
package com.wpj.service.impl; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.plugins.Page; import com.wpj.baseservice.impl.BaseServiceImpl; import com.wpj.bean.User; import com.wpj.mapper.IUserMapper; import com.wpj.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService { @Autowired private IUserMapper iUserMapper; @Override protected BaseMapper<User> getBaseMapper() { return iUserMapper; } }
- 创建mapper文件(也接口同名)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wpj.mapper.IUserMapper"> </mapper>
- application.properties修改为application.yml
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/day64-springboot-crud username: root password: 123456 mybatis-plus: type-aliases-package: com.wpj.bean mapper-locations: classpath:mapper/*.xml
- 开启包扫描
package com.wpj.day64springboot05crud; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableTransactionManagement // 开启事务 @SpringBootApplication(scanBasePackages = "com.wpj") @MapperScan(basePackages = "com.wpj.mapper") public class Day64Springboot05CrudApplication { public static void main(String[] args) { SpringApplication.run(Day64Springboot05CrudApplication.class, args); } }
- 测试
10.测试环境完成。。。。。。。开始做增删改查。
1 显示数据库所有信息
1.1 写controller
package com.wpj.web.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.wpj.bean.User;
import com.wpj.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private IUserService iUserService;
@RequestMapping("/getPage")
public String getPage(Page<User> page, ModelMap map){
iUserService.getPage(page);
return "userList";
}
}
1.2 写userList页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<base th:href="${#request.getContextPath()+'/'}">
</head>
<body>
<table>
<tr>
<td>ID</td>
<td>name</td>
<td>pwd</td>
<td>age</td>
<td>money</td>
<td>operator</td>
</tr>
<tr th:each="user:${page.records}">
<td th:text="${user.id}"/>
<td th:text="${user.name}"/>
<td th:text="${user.pwd}"/>
<td th:text="${user.age}"/>
<td th:text="${user.money}"/>
<td>
<a href="#">编辑</a>
<a href="#">删除</a>
</td>
</tr>
</table>
</body>
</html>
1.3 运行Day64Springboot05CrudApplication启动tomcat,访问
2. 编辑数据
2.1 写controller
2.2 修改html中的a标签
3. 删除数据
3.1 写controller
@RequestMapping(value = "/deleteUser/{id}")
public String deleteUser(@PathVariable Integer id){
iUserService.delete(id);
return "redirect:../getPage";
}
3.2 修改html中的a标签
<a th:href="|deleteUser/${user.id}|">删除</a>