spring boot实现@mapper对数据库表的增删改查

1.连接数据库,springboot为数据库名称

spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

2.创建Article实体类

    private Integer id;
    private String title;
    private String content;

3.创建ArticleMapper接口类

(1)增(根据实体类article属性进行操作)

public int insertComment(Article article);

(2)删(根据实体类id操作)

public int deleteComment(Integer id);

(3)改(根据实体类id和content两个属性执行操作)

public int updateComment(Integer id, String content);

(4)查(根据id查找)

public Article findById(Integer id);

4.创建 badGuy 类,并在实体方法外写上@RestController注解

(1)引入ArticleMapper接口

@Autowired
private ArticleMapper articleMapper;

(2)完成对接口类中的增操作

@GetMapping("/add")
 public String badGuy1() {
      Article article = new Article();
     article.setContent("简介:一个程序员的成长史!");
     article.setTitle("一本好书");
     articleMapper.insertComment(article);
     return "添加成功";

}

(3)完成对接口类中的删操作

@GetMapping("/delete")

  public String badGuy2() {
      articleMapper.deleteComment(12);
      return "删除成功";
   }

(4)完成对接口类中的改操作

@GetMapping("/update")
   public String badGuy3() {
       articleMapper.updateComment(13,"修改成功");
       return "更新成功";
     }

(5)完成对接口类中的查操作

@GetMapping("/select")
    public String badGuy4() {
       Article article = articleMapper.findById(11);
        System.out.println(article);
        return "查找成功";
        }

加油!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例,演示如何使用Spring整合MyBatis对数据库进行增删改查操作: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependencies> <!-- Spring整合MyBatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- MySQL数据库驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 2. 配置数据源 在 application.properties 文件中添加以下配置: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # MyBatis配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity ``` 3. 创建实体类 创建一个实体类,例如: ```java public class User { private Integer id; private String name; private Integer age; // 省略getter和setter方法 } ``` 4. 创建Mapper接口 创建一个Mapper接口,例如: ```java public interface UserMapper { // 新增用户 int insert(User user); // 删除用户 int deleteById(Integer id); // 更新用户 int update(User user); // 根据ID查询用户 User selectById(Integer id); // 查询所有用户 List<User> selectAll(); } ``` 5. 创建Mapper XML文件 在 resources/mapper 目录下创建 UserMapper.xml 文件,例如: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <!-- 新增用户 --> <insert id="insert" parameterType="User"> insert into user(name, age) values(#{name}, #{age}) </insert> <!-- 删除用户 --> <delete id="deleteById" parameterType="java.lang.Integer"> delete from user where id = #{id} </delete> <!-- 更新用户 --> <update id="update" parameterType="User"> update user set name = #{name}, age = #{age} where id = #{id} </update> <!-- 根据ID查询用户 --> <select id="selectById" resultType="User" parameterType="java.lang.Integer"> select id, name, age from user where id = #{id} </select> <!-- 查询所有用户 --> <select id="selectAll" resultType="User"> select id, name, age from user </select> </mapper> ``` 6. 创建Service接口及实现类 创建一个Service接口及实现类,例如: ```java public interface UserService { // 新增用户 int addUser(User user); // 删除用户 int deleteUserById(Integer id); // 更新用户 int updateUser(User user); // 根据ID查询用户 User getUserById(Integer id); // 查询所有用户 List<User> getAllUsers(); } @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int addUser(User user) { return userMapper.insert(user); } @Override public int deleteUserById(Integer id) { return userMapper.deleteById(id); } @Override public int updateUser(User user) { return userMapper.update(user); } @Override public User getUserById(Integer id) { return userMapper.selectById(id); } @Override public List<User> getAllUsers() { return userMapper.selectAll(); } } ``` 7. 编写Controller 在Controller中注入UserService,并实现对应的增删改查接口即可。例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/add") public int addUser(@RequestBody User user) { return userService.addUser(user); } @DeleteMapping("/{id}") public int deleteUserById(@PathVariable Integer id) { return userService.deleteUserById(id); } @PutMapping("/update") public int updateUser(@RequestBody User user) { return userService.updateUser(user); } @GetMapping("/{id}") public User getUserById(@PathVariable Integer id) { return userService.getUserById(id); } @GetMapping("/all") public List<User> getAllUsers() { return userService.getAllUsers(); } } ``` 这样,我们就完成了Spring整合MyBatis对数据库实现增删改查的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值