CREATE TABLE IF NOT EXISTS `user`( `id` INT UNSIGNED AUTO_INCREMENT, `username` VARCHAR(100) NOT NULL, `password` VARCHAR(40) NOT NULL, `jifen` int(10) NOT NULL, PRIMARY KEY ( `id` ))ENGINE=InnoDB DEFAULT CHARSET=utf8;
public class User { private Integer id; private String username; private String password; private Integer jifen; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setJifen(Integer jifen){ this.jifen = jifen; } public Integer getjifen() { return jifen; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; } }
然后创建一个接口 UserDao
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package com.SpringDemo;
import java.util.List;
public interface UserDao { public int addUser(User user); public int updateUser(User user); public int deleteUser(int id); //通过id查询用户 public User findUserById(int id); //查询所有用户 public List<User> findAllUser(); public void transfer(String outUser,String inUser,Integer jifen); }
public class UserDaoImpl implements UserDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public int addUser(User user) { String sql="insert into user(username,password) value(?,?)"; Object[] obj=new Object[]{ user.getUsername(), user.getPassword() }; int num=this.jdbcTemplate.update(sql,obj); return num; }
@Override public int updateUser(User user) { String sql="update user set username=?,password=? where id=?"; Object[] params=new Object[]{ user.getUsername(), user.getPassword(), user.getId() }; int num=this.jdbcTemplate.update(sql,params); return num; }
@Override public int deleteUser(int id) { String sql="delete from user where id=?"; int num=this.jdbcTemplate.update(sql,id); return num; }
@Override public User findUserById(int id) { String sql="select * from user where id=?"; RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class); return this.jdbcTemplate.queryForObject(sql,rowMapper,id); }
@Override public List<User> findAllUser() { String sql="select * from user"; RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class); return this.jdbcTemplate.query(sql,rowMapper); }
@Override public void transfer(String outUser, String inUser, Integer jifen) { // 赠送积分 this.jdbcTemplate.update("update user set jifen=jifen+? where username=?",jifen,inUser); // 模拟系统运行时的突发性问题 int i =1/0; //赠送出积分 this.jdbcTemplate.update("update user set jifen=jifen-? where username=?",jifen,outUser);
@Override @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT, readOnly = false) public void transfer(String outUser, String inUser, Integer jifen) { // 赠送积分 this.jdbcTemplate.update("update user set jifen=jifen+? where username=?",jifen,inUser); // 模拟系统运行时的突发性问题 int i =1/0; //赠送出积分 this.jdbcTemplate.update("update user set jifen=jifen-? where username=?",jifen,outUser);