实体Bean
package cn.csdn.bean;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
//用户的id
private int id;
//用户的名字
private String name;
//用户的年龄
private int age;
//用户的性别
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Dao层
public interface UserDao {
// 增加
void insert(User entity);
// 修改
void update(User entity);
// 删除
void delete(int id);
// 根据id查询
User selectById(int id);
// 查询所有
List<User> selectAll();
}
DaoImpl层 第一种实现:
package cn.csdn.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import cn.csdn.bean.User;
public class UserDaoImpl implements UserDao{
//定义一个Jdbc
private JdbcTemplate jt;
//必须有set 方法
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
public void delete(int id) {
// TODO Auto-generated method stub
String sql="delete from user where id="+id;
int temp=this.jt.update(sql);
if(temp>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
public void insert(User entity) {
// TODO Auto-generated method stub
String sql="insert into user(name,age,sex) values(?,?,?)";
Object obj[]={entity.getName(),entity.getAge(),entity.getSex()};
int temp=this.jt.update(sql,obj);
if(temp>0){
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
}
public List<User> selectAll() {
String sql="select * from user";
List list=this.jt.query(sql,new RowMapper(){
@Override
public Object mapRow(ResultSet rs, int row) throws SQLException {
// TODO Auto-generated method stub
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
return user;
}
});
System.out.println(list.size());
// TODO Auto-generated method stub
return list;
}
public User selectById(int id) {
// TODO Auto-generated method stub
String sql="select id,name,age,sex from user where id="+id;
User user=(User)jt.queryForObject(sql, new RowMapper(){
@Override
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
// TODO Auto-generated method stub
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
return user;
}
});
return user;
}
public void update(User entity) {
// TODO Auto-generated method stub
String sql="update user set name=?,age=?,sex=? where id=?";
Object obj[]={entity.getName(),entity.getAge(),entity.getSex(),entity.getId()};
int temp=jt.update(sql,obj);
if(temp>0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
}
}
第二种实现:
package cn.csdn.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import cn.csdn.bean.User;
public class UserDaoImpl3 implements UserDao{
//定义一个Jdbc
private SimpleJdbcTemplate sjt;
//必须有set 方法
public void setSjt(SimpleJdbcTemplate sjt){
this.sjt=sjt;
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
String sql="delete from user where id=?";
int temp=this.sjt.update(sql, id);
if(temp>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
@Override
public void insert(User entity) {
// TODO Auto-generated method stub
String sql="insert into user(name,age,sex) values(?,?,?)";
int temp=sjt.update(sql,entity.getName(),entity.getAge(),entity.getSex());
if(temp>0){
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
}
@Override
public List<User> selectAll() {
String sql="select * from user";
List list=this.sjt.queryForList(sql);
System.out.println(list.size());
return list;
}
@Override
public User selectById(int id) {
// TODO Auto-generated method stub
String sql="select id,name,age,sex from user where id=?";
User user=(User) sjt.queryForObject(sql, new ParameterizedRowMapper() {
@Override
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
return user;
}
}, id);
return user;
}
@Override
public void update(User entity) {
// TODO Auto-generated method stub
/**第一种方法*/
//String sql="update user set name=?,age=?,sex=? where id=?";
//int temp=sjt.update(sql, entity.getName(),entity.getAge(),entity.getSex(),entity.getId());
/**第二种方法*/
String sql="update user set name=:name,age=:age,sex=:sex where id=:id";
Map<String,Object> map=new HashMap<String,Object>();
map.put("name", entity.getName());
map.put("age", entity.getAge());
map.put("sex", entity.getSex());
map.put("id", entity.getId());
int temp=sjt.update(sql, map);
if(temp>0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
}
}