spring的jdbctemplate的crud的基类dao
import java.util.List;
/***
* 基本接口
*
* @author xyq
* @param <T>
*
*/
public interface BaseDaoInf<T> {
/***
* 查询接口
*
* @return
*/
public List<T> find(String sql, Object[] parameters, Class<T> cl);
/***
* 添加,更新,删除接口
* @param sql
* @param id
* @param cl
* @return
*/
public int addOrUpdateOrDelete(String sql,Object[] parameters, Class<T> cl);
}
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import com.xyq.all.dao.inf.BaseDaoInf;
public class BaseDaoImpl<T> implements BaseDaoInf<T> {
/**
*
*/
private static final long serialVersionUID = 1L;
/***
* 数据库连接对象
*/
@Resource(name = "jdbcTemplate")
private JdbcTemplate jt;
/***
* 查询接口
*/
@Override
public List<T> find(String sql, Object[] parameters, Class<T> cl) {
List<T> resultList = null;
try {
if (parameters != null && parameters.length > 0)
resultList = jt.query(sql, parameters,
new BeanPropertyRowMapper<T>(cl));
else
// BeanPropertyRowMapper是自动映射实体类的
resultList = jt.query(sql, new BeanPropertyRowMapper<T>(cl));
} catch (Exception e) {
e.printStackTrace();
}
return resultList;
}
/***
* 添加,更新,删除的实现,返回1,0,-1
*/
@Override
public int addOrUpdateOrDelete(String sql, final Object[] parameters,
Class<T> cl) {
int num = 0;
try {
if (parameters == null || parameters.length == 0)
num = jt.update(sql);
else
num = jt.update(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps)
throws SQLException {
for (int i = 0; i < parameters.length; i++)
ps.setObject(i + 1, parameters[i]);
}
});
} catch (Exception e) {
e.printStackTrace();
num = -1;
}
return num;
}
}
/**
* 用户操作接口
* @author xyq
*
*/
public interface UserInfoDaoInf {
/***
* 查询是否有用户
* @param user
* @return
*/
public UserInfo findUser(UserInfo user);
/***
* 删除用户
* @param user
* @return
*/
public int deleteUserById(UserInfo user);
/***
* 添加用户
* @param user
* @return
*/
public int addUser(UserInfo user);
/***
* 更新用户
* @param user
* @return
*/
public int updateUser(UserInfo user);
}
import java.util.List;
import org.springframework.stereotype.Repository;
import com.xyq.all.dao.inf.UserInfoDaoInf;
import com.xyq.all.entity.UserInfo;
@Repository("userDao")
public class UserInfoDaoImpl extends BaseDaoImpl<UserInfo> implements
UserInfoDaoInf {
/***
* 查找用户信息
*/
@Override
public UserInfo findUser(UserInfo user) {
List<UserInfo> list = null;
String sql = "select * from userInfo where username=? and password =? ";
list = super.find(sql,
new String[] { user.getUserName(), user.getPassword() },
UserInfo.class);
if (list != null && list.size() > 0)
return list.get(0);
return null;
}
/***
* 删除用户
*/
@Override
public int deleteUserById(UserInfo user) {
String sql = "delete from userInfo where userid = ?";
return super.addOrUpdateOrDelete(sql, new Integer[] { user.getUserId() },
UserInfo.class);
}
/***
* 添加用户
*/
@Override
public int addUser(UserInfo user) {
String sql = "insert into userInfo values(null,?,?,?)";
return super.addOrUpdateOrDelete(
sql,
new Object[] { user.getUserName(), user.getPassword(),
user.getSecondPassword() }, UserInfo.class);
}
/***
* 更新用户
*/
@Override
public int updateUser(UserInfo user) {
String sql = "update userInfo set userName=?,password = ?,secondPassword = ? where userid=?";
return super.addOrUpdateOrDelete(
sql,
new Object[] { user.getUserName(), user.getPassword(),
user.getSecondPassword(), user.getUserId() },
UserInfo.class);
}
最新评论