当前位置:   article > 正文

springboot+mybatis实现数据增删改查_spooting+mybati添加数据csdn

spooting+mybati添加数据csdn

springboot+mybatis实现数据增删改查

上一篇讲到使用idea创建springboot项目 那么下面我们来讲讲使用mybatis实现增删改查(curd)吧。

1、数据库创建

2、pom.xml文件 加入依赖mysql 等等

  1. <!--mysql依赖-->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>2.0.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. <scope>runtime</scope>
  11. </dependency>

3、在配置文件(application.yml)中加入数据库的配置  注意用户名、密码、库名、包名路径改成自己的

  1. server:
  2. port: 8111
  3. servlet:
  4. context-path: /
  5. spring:
  6. datasource:
  7. username: root
  8. password: '*******'
  9. driver-class-name: com.mysql.cj.jdbc.Driver
  10. url: jdbc:mysql://localhost:3306/lzw?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
  11. mybatis:
  12. mapper-locations: classpath:./mapper/*Mapper.xml
  13. type-aliases-package: com.example.bys.entity

 4、在src文件夹下新建UserEntity类  与数据库的字段对应

  1. package com.example.bys.entity;
  2. public class UserEntity {
  3. private int userid;
  4. private String username;
  5. private String password;
  6. private String email;
  7. public UserEntity(String username, String password, String email) {
  8. this.userid = userid;
  9. this.username = username;
  10. this.password = password;
  11. this.email = email;
  12. }
  13. public UserEntity() {
  14. }
  15. public int getUserid() {
  16. return userid;
  17. }
  18. public void setUserid(int userid) {
  19. this.userid = userid;
  20. }
  21. public String getUsername() {
  22. return username;
  23. }
  24. public void setUsername(String username) {
  25. this.username = username;
  26. }
  27. public String getPassword() {
  28. return password;
  29. }
  30. public void setPassword(String password) {
  31. this.password = password;
  32. }
  33. public String getEmail() {
  34. return email;
  35. }
  36. public void setEmail(String email) {
  37. this.email = email;
  38. }
  39. }

5、新建mapper文件夹并新建UserMapper接口

  1. package com.example.bys.mapper;
  2. import com.example.bys.entity.UserEntity;
  3. import org.apache.catalina.User;
  4. import org.springframework.stereotype.Repository;
  5. import java.util.List;
  6. @Repository
  7. public interface UserMapper {
  8. /**
  9. *
  10. * @return 查询所有用户
  11. */
  12. List<UserEntity> selectAllUser();
  13. /**
  14. *
  15. * @param id 用户id
  16. * @return 查询单个用户
  17. */
  18. UserEntity selectOneUser(int id);
  19. /**
  20. *
  21. * @param userEntity 插入用户
  22. * @return 插入的用户
  23. */
  24. void insertUser(UserEntity userEntity);
  25. /**
  26. *
  27. * @param id 删除用户的id
  28. */
  29. void deleteUser(int id);
  30. /**
  31. *
  32. * @param userEntity 更新用户的数据
  33. */
  34. void updateUser(UserEntity userEntity);
  35. }

6、新建service文件夹并新建UserServiceImpl类 去实现上面的mapper接口

  1. package com.example.bys.service;
  2. import com.example.bys.entity.UserEntity;
  3. import com.example.bys.mapper.UserMapper;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. @Service
  8. public class UserServiceImpl {
  9. @Autowired
  10. UserMapper userMapper;
  11. public List<UserEntity> selectAllUser() {
  12. return userMapper.selectAllUser();
  13. }
  14. public UserEntity selectOneUser(int id) {
  15. return userMapper.selectOneUser(id);
  16. }
  17. public void insertUser(UserEntity userEntity){
  18. userMapper.insertUser(userEntity);
  19. }
  20. public void deleteUser(int id){
  21. userMapper.deleteUser(id);
  22. }
  23. public void updateUser(UserEntity userEntity){
  24. userMapper.updateUser(userEntity);
  25. }
  26. }

7、在controller文件夹下新建UserController类 在里面写对应的逻辑

  1. package com.example.bys.controller;
  2. import com.example.bys.entity.UserEntity;
  3. import com.example.bys.service.UserServiceImpl;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import java.util.List;
  10. @RestController
  11. @RequestMapping("/user")
  12. public class UserController {
  13. @Autowired
  14. private UserServiceImpl userService;
  15. @RequestMapping("/selectAllUser")
  16. public List<UserEntity> selectAllUser(){
  17. return userService.selectAllUser();
  18. }
  19. @RequestMapping("/selectOneUser")
  20. public UserEntity selectOneUser(int id){
  21. return userService.selectOneUser(id);
  22. }
  23. @PostMapping("/insertUser")
  24. public void insertUser(@RequestBody UserEntity userEntity){
  25. userService.insertUser(userEntity);
  26. System.out.println("insert成功");
  27. }
  28. @RequestMapping("/deleteUser")
  29. public void deleteUser(int id){
  30. userService.deleteUser(id);
  31. System.out.println("delete成功");
  32. }
  33. @PostMapping("/updateUser")
  34. public void updateUser(@RequestBody UserEntity userEntity){
  35. userService.updateUser(userEntity);
  36. System.out.println("update成功");
  37. }
  38. }

8、在resource文件夹下新建mapper文件夹并新建映射Usermapper.xml文件  在里面写相应的数据库操作语句

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.bys.mapper.UserMapper">
  4. <select id="selectAllUser" resultType="com.example.bys.entity.UserEntity">
  5. select * from user
  6. </select>
  7. <select id="selectOneUser" resultType="com.example.bys.entity.UserEntity">
  8. select * from user where userid = #{id}
  9. </select>
  10. <insert id="insertUser" parameterType="com.example.bys.entity.UserEntity">
  11. insert into user (username,password,email) values (#{username},#{password},#{email})
  12. </insert>
  13. <delete id="deleteUser" parameterType="java.lang.Integer">
  14. delete from user where userid = #{id}
  15. </delete>
  16. <update id="updateUser" parameterType="com.example.bys.entity.UserEntity">
  17. update user set username=#{username},password=#{password},email=#{email} where userid=#{userid}
  18. </update>
  19. </mapper>

9、最后在启动类中加上一句扫描mapper包的位置 然后直接run起来就好了 

@MapperScan("com.example.bys.mapper")  //扫描mapper包

 

10、好啦到这里已经基本完成啦。。。下面用postman测试工具来测试(蛮好用的工具大家可以下载一个来玩一下)

测试查找全部用户 注意路径和Usercontroller里面的要一致

测试查找单个用户  

测试新增用户

测试删除用户

测试修改用户

工程代码已上传至Github 大家自行下载吧。。。。。。。

https://github.com/Goodnamelzw/springboot-mybatis

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/232876
推荐阅读
相关标签
  

闽ICP备14008679号