mybaits-plus 实现增删改查

本文展示了如何使用MyBatisGenerator插件生成Mapper、Service和实体类,并提供了测试用例,包括查询所有用户、插入、更新和删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

数据库表(student)如下:

使用 mybaits generator插件生成 mapper,service,和实体类

目录结构如下:

StudentMapper中代码

@Repository
public interface StudentMapper extends BaseMapper<Student> {
}

StudentService中代码

public interface StudentService extends IService<Student> {
}

StudentServiceImpl中代码

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}

编写测试类

@Autowired
    private StudentMapper studentMapper;
@Test
    void contextLoads() {
        //c查询全部用户
        //参数是一个wrapper
      List student =  studentMapper.selectList(null);
      student.forEach(System.out::println);
        }

        //插入信息
    @Test
    public void testInsert(){
        Student student = new Student();
        student.setName("jiang");
        student.setPassword(("1234j"));
        int result = studentMapper.insert(student);
        System.out.println(result);
        System.out.println(student);
    }
    //更新信息
     @Test
    public void testUpdate(){
        Student student = new Student();
        student.setId(3);
        student.setName("123");
        
        int i = studentMapper.updateById(student); //通过条件拼接sql语句
        System.out.println(i);

     }
     //删除信息
    @Test
    public void testdelete(){
    
        int i=4;
        studentMapper.deleteById(i);
        //System.out.println(student);
    }

 

好的,下面是使用Spring Boot + MyBatis Plus + Thymeleaf的增删改查示例代码: 1. 实体类定义 ``` @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private String email; } ``` 2. 数据访问层 ``` @Repository public interface UserMapper extends BaseMapper<User> { } ``` 3. 服务层 ``` @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> findAll() { return userMapper.selectList(null); } public User findById(Long id) { return userMapper.selectById(id); } public void save(User user) { if (user.getId() == null) { userMapper.insert(user); } else { userMapper.updateById(user); } } public void deleteById(Long id) { userMapper.deleteById(id); } } ``` 4. 控制器层 ``` @Controller public class UserController { @Autowired private UserService userService; @GetMapping("/") public String index(Model model) { List<User> userList = userService.findAll(); model.addAttribute("userList", userList); return "index"; } @GetMapping("/add") public String add(Model model) { model.addAttribute("user", new User()); return "form"; } @GetMapping("/edit/{id}") public String edit(@PathVariable Long id, Model model) { User user = userService.findById(id); model.addAttribute("user", user); return "form"; } @PostMapping("/save") public String save(@ModelAttribute User user) { userService.save(user); return "redirect:/"; } @GetMapping("/delete/{id}") public String delete(@PathVariable Long id) { userService.deleteById(id); return "redirect:/"; } } ``` 5. 页面模板 ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot MyBatis Plus Thymeleaf Demo</title> </head> <body> <h2>User List</h2> <table border="1"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Action</th> </tr> </thead> <tbody> <tr th:each="user : ${userList}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.email}"></td> <td> <a th:href="@{/edit/{id}(id=${user.id})}">Edit</a> <a th:href="@{/delete/{id}(id=${user.id})}">Delete</a> </td> </tr> </tbody> </table> <br> <a th:href="@{/add}">Add User</a> </body> </html> ``` 6. 表单模板 ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot MyBatis Plus Thymeleaf Demo</title> </head> <body> <h2>[[${user.id} == null ? 'Add' : 'Edit']] User</h2> <form th:action="@{/save}" method="post"> <input type="hidden" th:field="*{id}"> <label for="name">Name:</label> <input type="text" th:field="*{name}"> <br> <label for="email">Email:</label> <input type="email" th:field="*{email}"> <br> <button type="submit">Save</button> <a th:href="@{/}">Cancel</a> </form> </body> </html> ``` 以上是一个简单的Spring Boot + MyBatis Plus + Thymeleaf的增删改查示例,可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值