spring的jdbctemplate的crud的基類dao。本站提示廣大學習愛好者:(spring的jdbctemplate的crud的基類dao)文章只能為提供參考,不一定能成為您想要的結果。以下是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);
}