package com.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.model.User;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
// public JdbcTemplate getJdbcTemplate() {
// return jdbcTemplate;
// }
//
// // 注入
// public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
// this.jdbcTemplate = jdbcTemplate;
// }
@Override
public void register(User user) {
String sqlStr = "insert into user(userName,password) values(?,?)";
Object[] params = new Object[] { user.getUserName(), user.getPassword() };
jdbcTemplate.update(sqlStr, params);
}
@Override
public String findUserByUserName(String userName) {
String sql = String.format(
"select count(*) from User where userName='%s'", userName);
int count = jdbcTemplate.queryForInt(sql);
if (count == 1) {
return "ok";
} else {
return "error";
}
}
@Override
public List<User> findAllUsers() {
// TODO Auto-generated method stub
String sql="select * from User";
List<User> users=new ArrayList<User>();
List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
for(int i=0;i<list.size();i++){
User user=new User();
//不能强制转换long为Integer类型所以调用hashCode
user.setId((Integer)list.get(i).get("id").hashCode());
user.setUserName(list.get(i).get("userName").toString());
user.setPassword(list.get(i).get("password").toString());
users.add(user);
}
return users;
}
@Override
public String addusers(String userName,String password) {
// TODO Auto-generated method stub
int i = jdbcTemplate.update(
"insert into user(userName,password) values(?,?)",
new Object[] { userName, password });
if(i>0){
return "ok";
}else{return "error";}
}
@Override
public void deleteUsers(String id) {
// TODO Auto-generated method stub
//int d;
//d= Integer.parseInt(id);
jdbcTemplate.update("delete from user where id=? ",
new Object[] {id});
}
@Override
public boolean upDate(String id, String userName, String password) {
// TODO Auto-generated method stub
System.out.println(id);
System.out.println(userName);
System.out.println(password);
boolean flag = false;
/* 更新实现 */
int i = jdbcTemplate
.update(
"update user set userName=?,password=? where id=?",
new Object[] {userName,password,id });
if (i > 0) {
flag = true;
}
return flag;
}
/*@Override
public User FindByid(String id) {
// TODO Auto-generated method stub
User user=new User();
String sql = String.format(
"select count(*) from User where id='%s'", id);
user=(User)jdbcTemplate.queryForObject(sql, new Object[] { id }, new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum)
throws SQLException {
HttpServletRequest req = null;
req.setAttribute("id",rs.getString("id"));
req.setAttribute("userName", rs.getString("userName"));
req.setAttribute("password", rs.getString("password"));
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("userName"));
System.out.println(rs.getString("password"));
User users = new User();
users.setId(rs.getInt("id"));
users.setUserName(rs.getString("userName"));
users.setPassword(rs.getString("password"));
return users;
}
});
return user;
}*/
}
评论3
最新资源