spring基于注解的增删改查

首先新建一个实体类,这里不需要构造方法

public class Account {
    private String username;
    private int money;


    public void setUsername(String username){
        this.username=username;
    }
    public void setMoney(int money){
        this.money=money;
    }

    public String getUsername() {
        return username;
    }

    public int getMoney() {
        return money;
    }
}

然后是Service接口及其实现类

public interface Service {
    //修改数据
    public void update(String username,int money) throws SQLException;
    //新增数据
    public void save(Account account) throws SQLException;
    //删除数据
    public void delete(String username) throws SQLException;
    //查找数据
    public List<Account> find() throws SQLException;
}


@Service("service")
public class ServiceImpl implements Service {
    @Autowired//自动注入
    private UserDao dao;
    public void update(String username,int money) throws SQLException {
        this.dao.update(username,money);
    }

    public void delete(String username) throws SQLException {
        this.dao.delete(username);
    }

    public void save(Account account) throws SQLException {
    this.dao.save(account);
    }
    public List<Account> find() throws SQLException {

        return this.dao.find();
    }
}

然后是持久层接口及其实现类

public interface UserDao {
    //修改数据
    public void update(String username,int money) throws SQLException;
    //新增数据
    public void save(Account account) throws SQLException;
    //删除数据
    public void delete(String username) throws SQLException;
    //查找数据
    public List<Account> find() throws SQLException;
}

@Repository("Dao")
public class UserDaoImpl implements UserDao {
    @Autowired
    private QueryRunner query;


    public void update(String username,int money) throws SQLException {
        this.query.update("update  test set money=? where username=?",
                money,username);
    }

    public void delete(String username) throws SQLException {
      
        this.query.update("delete from test where username =?",username);
    }

    public List<Account> find() throws SQLException {

       return this.query.query("select * from test",new BeanListHandler<Account>(Account.class));
    }

    public void save(Account account) throws SQLException {
      this.query.insert("insert into test values(?,?)", new ResultSetHandler<Account>() {
          public Account handle(ResultSet resultSet) throws SQLException {
              return null;
          }
      },account.getUsername(),account.getMoney());
    }
}

工具类

@PropertySource("classpath:jdbc.properties")
@ComponentScan(basePackages = {"Dao","Service"})//要扫描的包
@Import(JDBCConfig.class)
public class SpringConfiguation {

}


public class JDBCConfig {
    @Bean(name="query")
    @Scope("prototype")
    public QueryRunner find(DataSource dataSource){
        return new QueryRunner(dataSource);
    }
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.DriverClass}")
    private String driverClass;

    @Bean(name="datasource")
    public DataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass(driverClass);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl(url);
        return dataSource;

    }
}

最后是properties文件

jdbc.url=jdbc:mysql://localhost:3306/db?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
jdbc.DriverClass=com.mysql.cj.jdbc.Driver

新建数据库表

create table test(username varchar20,money int);
insert into test values('xx',1000)
insert into test values('yy',900) 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值