用户类(User):包含用户基本信息(id,name,password),以及基本信息的get/set方法。
public class User { private int userID; //用户ID private String userName; //用户名 private String password; //用户密码 public int getUserID() { return userID; } public void setUserID(int userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString(){ return "user ID:" + this.getUserID() + " userName:" + this.getUserName() + " user password:" + this.getPassword(); } } View Code
创建用户的工厂(UserFactory):创建用户的工厂,创建具体用户对象。
public class UserFactory { public User createUser(String name, int id, String password){ User user = new User(); user.setUserName(name); user.setUserID(id); user.setPassword(password); return user; } }
用户数据访问接口(UserDao):定义对用户表(t_user)的基本操作(增、删、改、查)。
public interface UserDao { public int addUser(User user); public int updateUser(User user); public int deleteUser(User user); public User findUserByID(int id); public List<User> findAllUser(); }
用户数据访问实现类(UserDaoImpl):实现接口(UserDao)中定义的方法。
public class UserDaoImpl implements UserDao{ private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbc){ this.jdbcTemplate = jdbc; } @Override public int addUser(User user) { // TODO Auto-generated method stub String sql = "insert into t_user(userid,username,password)values(?,?,?)"; Object[] obj = new Object[]{ user.getUserID(), user.getUserName(), user.getPassword() }; return this.execute(sql, obj); } @Override public int updateUser(User user) { // TODO Auto-generated method stub String sql = "update t_user set username=?,password=? where userid=?"; Object[] obj = new Object[]{ user.getUserName(), user.getPassword(), user.getUserID() }; return this.execute(sql, obj); } @Override public int deleteUser(User user) { // TODO Auto-generated method stub String sql = "delete from t_user where userid=?"; Object[] obj = new Object[]{ user.getUserID() }; return this.execute(sql, obj); } private int execute(String sql, Object[] obj){ return this.jdbcTemplate.update(sql, obj); } @Override public User findUserByID(int id) { // TODO Auto-generated method stub String sql = "select * from t_user where userid=?"; RowMapper<User> rowMapper = new BeanPropertyRowMapper(User.class); return this.jdbcTemplate.queryForObject(sql, rowMapper, id); } @Override public List<User> findAllUser() { // TODO Auto-generated method stub String sql = "select * from t_user"; RowMapper<User> rowMapper = new BeanPropertyRowMapper(User.class); return this.jdbcTemplate.query(sql, rowMapper); } }
View Code存款访问接口(AccountDao):定义对存款表(account)的基本操作。