spring-boot利用注解实现增删改查

spring-boot利用注解实现增删改查,第1张

项目结构

yml配置

这是我的数据库的字段,后期好看实现增删改查的结果

pom.xml

这是我的项目,实际实现数据库的增删改查用不了这么多



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.7
         
    
    com.pay-demo1
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        11
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            com.baomidou
            mybatis-plus
            3.1.0
        
        
            com.alipay.sdk
            alipay-sdk-java
            3.3.49.ALL
        
        
            org.mybatis
            mybatis
            3.5.9
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            mysql
            mysql-connector-java
            8.0.15
        
        
            log4j
            log4j
            1.2.17
            compile
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis
            mybatis
            3.5.9
        
        
            junit
            junit
            4.13
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 实体层
package com.paydemo1.demo.pojo;



public class Pocket {

    private String room_ID;
    private String balance;
    private String recharge;

    public String getRoom_ID() {
        return room_ID;
    }

    public void setRoom_ID(String room_ID) {
        this.room_ID = room_ID;
    }

    public String getBalance() {
        return balance;
    }

    public void setBalance(String balance) {
        this.balance = balance;
    }

    public String getRecharge() {
        return recharge;
    }

    public Pocket(String room_ID, String balance, String recharge) {
        this.room_ID = room_ID;
        this.balance = balance;
        this.recharge = recharge;
    }

    @Override
    public String toString() {
        return "Pocket{" +
                "room_ID='" + room_ID + '\'' +
                ", balance='" + balance + '\'' +
                ", recharge='" + recharge + '\'' +
                '}';
    }

    public void setRecharge(String recharge) {
        this.recharge = recharge;
    }
}
数据访问层 :mapper
package com.paydemo1.demo.mapper;

import com.paydemo1.demo.pojo.Pocket;

import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface PocketMapper {
    //查询全部
    @Select("select * from pocket")
    List findAll();
    //新增数据
    @Insert(" insert into  pocket( room_ID,balance,recharge ) values (#{room_ID},#{balance},#{recharge}) ")
    void save(Pocket pocket);

    //删除数据
    @Delete(" delete from pocket where room_ID= #{room_ID} ")
    void delete(String room_ID);

    //根据id查找
    @Select("select room_ID,balance,recharge from pocket where room_ID= #{room_ID} ")
    Pocket get(String room_ID);

    //更新数据
    @Update("update pocket set balance=#{balance},recharge=#{recharge} where room_ID=#{room_ID} ")
    void update(Pocket pocket);

}
业务逻辑层:service 
package com.paydemo1.demo.service;


import com.paydemo1.demo.pojo.Pocket;

import java.util.List;

public interface PocketService  {


    List findAll();


    void save(Pocket pocket) ;


    void delete(String room_ID);

    Pocket get(String room_ID) ;

    void update(Pocket pocket);
}
业务实现:serviceImpl
package com.paydemo1.demo.service.serviceImpl;

import com.paydemo1.demo.mapper.PocketMapper;
import com.paydemo1.demo.pojo.Pocket;
import com.paydemo1.demo.service.PocketService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class PocketServiceImpl implements PocketService{
    @Resource
    private PocketMapper pocketMapper;

    @Override
    public List findAll() {
        return pocketMapper.findAll();
    }

    @Override
    public void save(Pocket pocket) {
        pocketMapper.save(pocket);
    }

    @Override
    public void delete(String room_ID) {
        pocketMapper.delete(room_ID);
    }

    @Override
    public Pocket get(String room_ID) {
        return pocketMapper.get(room_ID);
    }

    @Override
    public void update(Pocket pocket) {
        pocketMapper.update(pocket);
    }
}
 然后我们在test里面测试,也可以写在controller控制层里面,在下图地方实现

测试代码 
package com.paydemo1.demo;

import com.paydemo1.demo.pojo.Pocket;
import com.paydemo1.demo.service.PocketService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
class DemoApplicationTests {
    @Resource
    private PocketService pocketService;

    //删除钱包用户
    @Test
    public void delete() {
        pocketService.delete("1");
    }
    //根据id查询钱包用户
    @Test
    public void get(){
        System.out.println(pocketService.get("2").toString());
    }
    //查询整个表
    @Test
    public void userList(){
        List pocket = pocketService.findAll();
        System.out.println(pocket.toString());
    }
    //增加钱包用户
    @Test
    public void save(){
        Pocket pocket = new Pocket("8","8","8") ;
        pocketService.save(pocket);
        List pocket1 = pocketService.findAll();
        System.out.println(pocket1.toString());
    }

//    根据id修改
    @Test
    public void update(){
        String room_ID =pocketService.get("2").getRoom_ID();
        String balance =pocketService.get("2").getBalance();
        String recharge =pocketService.get("2").getRecharge();
        double balance1 = Double.parseDouble(balance)+Double.parseDouble((recharge));
        String balance2 = ""+balance1;
        Pocket pocket = new Pocket(room_ID,balance2,recharge);
        pocketService.update(pocket);
        System.out.println(pocketService.get(room_ID).toString());

    }
}

现在钱包里面的表数据为 

 1.删除钱包用户,点击运行就可以了

运行结果

2.根据id查询钱包信息

测试结果

3.查询整个钱包

 4.增加钱包用户

 

5.用充值金额,对钱包余额进行修改,大家可以直接在Pocket里面将balance和recharge设置为double属性,就不用这么多的强转了。

 

 6.如果想业务实现,从前端传入后端的数据,直接在controll里面写方法实现数据库的 *** 作就可以了

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/916042.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存