接着昨天没有写完的,我们接下来开始实现具体的类
1,建立一个类user,他的属性同我们数据库中的一样(id,name,password,sex)id是主键,他是自定义的自增长
2,建立一个接口类,在这个类中要是实现的方法分别是:保存用户,添加用户,查询用户,删除用户,根据id查找用户
3,实现接口类:实现我们在接口中所定义的方法
4,进行单元测试
还是同昨天一样,最重要的其实就是在配置文件中所配置的信息,那些信息是对后台数据库的连接,以及利用Spring中的Aop对他进行拦截的控制
(自我感觉,所谓的框架,就是将这些东西建成一个模型,方便你的使用,所以你只需要学会使用它就行了,当然最重要的便是你在谁用它时所需要配置的文件了)
现在,就将实现类源码奉上
public class User {
private int id;
private String username;
private String password;
private String sex;
@Override
public String toString() {
return "User [id=" + id + ", password=" + password + ", sex=" + sex
+ ", username=" + username + "]";
}
public User (int id,String username,String password,String sex){
this.id=id;
this.password=password;
this.sex=sex;
this.username=username;
}
public User(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package jdbc;
import java.util.List;
public interface UserDao {
/**
* 创建表格
* @param tableName
*/
public void create(String tableName);
/**
* 保存用户
* @param user
*/
public void saveUser(User user);
/**
* 更新用户
* @param user
*/
public void updateUser(User user);
/**
* 删除用户
*/
public void deleteUser(User user);
/**
* 根据id查询用户信息
* @param id
* @return
*/
public User queryById(int id);
/**
* 查询所有用户信息
* @return
*/
public List queryAllUser();
/**
* 批量更新用户
* @param userlist
* @return
*/
public int[] batchUpdate(List userlist);
/**
* 查询所有用户
* @return
*/
public List queryAllUser1();
}
package jdbc;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class UserDaoImpl implements UserDao{
// private JdbcTemplate jdbcTemplate;
//
// public void setJdbctemplate(JdbcTemplate jdbcTemplate) {
// this.jdbcTemplate = jdbcTemplate;
// }
private JdbcTemplate jdbcTemplate;
// public JdbcTemplate getJdbcTemplate() {
// return jdbcTemplate;
// }
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int[] batchUpdate(final List userlist) {
int[] updateCount= jdbcTemplate.batchUpdate("update user set name=?,password=?,sex=?where id=?", new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
// TODO Auto-generated method stub
ps.setString(1, ((User) userlist.get(i)).getUsername());
ps.setString(2, ((User) userlist.get(i)).getPassword());
ps.setString(3, ((User) userlist.get(i)).getSex());
ps.setInt(4, ((User) userlist.get(i)).getId());
}
public int getBatchSize() {
// TODO Auto-generated method stub
return userlist.size();
}
});
return updateCount;
}
public void create(String tableName){
jdbcTemplate.execute("CREATE TABLE `"+tableName+"`(`id` integer NOT NULL ,`name` varchar(24) NOT NULL ,`password` varchar(40) NOT NULL ,`sex` varchar(10) NOT NULL ,PRIMARY KEY (`id`));");
}
public void deleteUser(User user) {
// TODO Auto-generated method stub
jdbcTemplate.update("delete from user where id=?", new Object[]{user.getId()}
);
}
public List queryAllUser() {
// TODO Auto-generated method stub
List list=(List) jdbcTemplate.query("select * from user", new Object[]{},new UserRowMapper());
return list;
//return null;
}
public User queryById(int id) {
// TODO Auto-generated method stub
User user=jdbcTemplate.queryForObject("select* from user where id=?", new Object[]{id},User.class);
return user;
}
public void saveUser(User user) {
// TODO Auto-generated method stub
jdbcTemplate.update("insert into user(name,password,sex) values(?,?,?)",
new Object[]{user.getUsername(),user.getPassword(),user.getSex()}
//new int[]{java.sql.Types.VARCHAR}
);
}
public void updateUser(User user) {
// TODO Auto-generated method stub
jdbcTemplate.update("update user where id=?", new Object[]{user.getUsername(),user.getPassword(),user.getSex()}
// new int[]{java.sql.Types.VARCHAR}
);
}
public List queryAllUser1() {
// TODO Auto-generated method stub
List list=jdbcTemplate.query("select * from user", new UserRowMapper());
return list;
}
}
package jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
//把查询结果设置到某个对象中
public class UserRowMapper implements RowMapper<User> {
public User mapRow(ResultSet rs, int rownum) throws SQLException {
// TODO Auto-generated method stub
User user=new User(rs.getInt("id"), rs.getString("name"),
rs.getString("password"), rs.getString("sex"));
return user;
}
}
package jdbc;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)//注释指定调用此类进行测试
@ContextConfiguration(locations="classpath:bean3.xml")
public class TestService {
private UserDao userdao;
@Resource
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
@Test
public void addUser(){
userdao.create("user56");
User user=new User();
user.setId(38);
//userdao.deleteUser(user);
}
}