Spring Jdbc 의 강 함 을 증명 합 니 다.
Spring Jdbc
Spring IoC container。
Spring Jdbc jdbc , 。
,
1、Template , ,sql, , 。
2、new BeanPropertySqlParameterSource(t) bean 。
3、ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class) bean 。
4、 dao , sql 。 ibatis 。
5、 , sql jdbc 。 “ jsp sql , ..... ”
@Component
public class UserInfoDaoJdbcImpl extends SimpleJdbcDaoSupport implements UserInfoDao {
public int deleteEntity(UserInfo t) throws DataAccessException {
if (null != t.getId()) {
return super.getSimpleJdbcTemplate().update("delete from user_info where id=:id", t.getId());
} else if (null != t.getMap().get("ids")) {
return super.getSimpleJdbcTemplate().update(
"delete from user_info where id in (" + t.getMap().get("ids") + ")", t.getId());
} else {
return -1;
}
}
public Integer insertEntity(UserInfo t) throws DataAccessException {
String sql = "insert into user_info (user_name, password, birthday, age) values (:user_name, :password, :birthday, :age)";
return super.getSimpleJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(t));
}
public UserInfo selectEntity(UserInfo t) throws DataAccessException {
List<String> sqls = new ArrayList<String>();
sqls.add("select * from user_info where 1=1");
if (null != t.getId()) {
sqls.add("and id=:id");
}
if (null != t.getUser_name()) {
sqls.add("and login_name=:login_name");
}
if (null != t.getPassword()) {
sqls.add("and password=:password");
}
String sql = StringUtils.join(sqls, " ");
List<UserInfo> userInfoList = super.getSimpleJdbcTemplate().query(sql,
ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class), new BeanPropertySqlParameterSource(t));
int listSize = userInfoList.size();
if (1 == listSize) {
return userInfoList.get(0);
} else {
return null;
}
}
public Long selectEntityCount(UserInfo t) throws DataAccessException {
List<String> sqls = new ArrayList<String>();
sqls.add("select count(*) from user_info where 1=1");
if (null != t.getId()) {
sqls.add("and id=:id");
}
if (null != t.getUser_name()) {
sqls.add("and login_name=:login_name");
}
if (null != t.getPassword()) {
sqls.add("and password=:password");
}
String sql = StringUtils.join(sqls, " ");
return super.getSimpleJdbcTemplate().queryForLong(sql, new BeanPropertySqlParameterSource(t));
}
public List<UserInfo> selectEntityList(UserInfo t) throws DataAccessException {
List<String> sqls = new ArrayList<String>();
sqls.add("select * from user_info where 1=1");
if (null != t.getId()) {
sqls.add("and id=:id");
}
if (null != t.getUser_name()) {
sqls.add("and login_name=:login_name");
}
if (null != t.getPassword()) {
sqls.add("and password=:password");
}
String sql = StringUtils.join(sqls, " ");
return super.getSimpleJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(UserInfo.class),
new BeanPropertySqlParameterSource(t));
}
public List<UserInfo> selectEntityPaginatedList(UserInfo t) throws DataAccessException {
// TODO Auto-generated method stub
return null;
}
public int updateEntity(UserInfo t) throws DataAccessException {
List<String> sqls = new ArrayList<String>();
if (null != t.getAge()) {
sqls.add(" age=:age");
}
if (null != t.getBirthday()) {
sqls.add(" birthday=:birthday");
}
String sql = StringUtils.join(new String[] { "update user_info set ", StringUtils.join(sqls.toArray(), ","),
" where id=:id" });
return super.getSimpleJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(t));
}
}
Spring NamedParameterJdbcTemplate DAO
2009-06-30 12:42:24| : |
NamedParameterJdbcTemplate JdbcTemplate, JdbcTemplate NamedParameterJdbcTemplate ,NamedParameterJdbcTemplate JdbcTemplate 。
public Object queryForObject(String sql, Map paramMap, RowMapper rowMapper)
public Object queryForObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper)
SqlParameterSource MapSqlParameterSource
BeanPropertySqlParameterSource
public int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder) 。
:
/**
* 、NamedParameterJdbcTemplate , :x ?
* NamedParameterJdbcTemplate map SqlParameterSource
* map :x,x , map , key : ,value
* SqlParameterSource , :x x
* 、 KeyHolder keyholder=new GeneratedKeyHolder();
namedjdbctemp.update(sql, ps,keyholder);
*/
public class StuDaoImple implements StuDaointer{
private NamedParameterJdbcTemplate namedjdbctemp;
public StuDaoImple(){
namedjdbctemp=new NamedParameterJdbcTemplate(SQLConnUtil.getDataSource());
}// SQLConnUtil JDBC ,
/*
* NamedParameterJdbcTemplate :
* 1, 。
* 2,
*/
public void addStu(Stu stu) {
String sql="insert into stu values(:sname,:ssex,:sbrith)";
//: stu
SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
KeyHolder keyholder=new GeneratedKeyHolder();
namedjdbctemp.update(sql, ps,keyholder);
// KeyHolder
int m=keyholder.getKey().intValue();
System.out.println(m);
//Map map=keyholder.getKeys();//
//keyholder.getKeyList();// ,
}
public int count() {
String sql="select count(*) from stu";
// NamedParameterJdbcTemplate JdbcTemplate
int m=namedjdbctemp.getJdbcOperations().queryForInt(sql);
return m;
}
/*
* NamedParameterJdbcTemplate :
*
*/
public void delStu(int sid) {
String sql="delete stu where s_id=:id";
Map map=new HashMap();
map.put("id", sid);
namedjdbctemp.update(sql, map);
}
public List getAllStu() {
String sql="select s_id as sid,s_name as sname,s_sex as ssex,s_brith as sbrith from stu";
List list=namedjdbctemp.getJdbcOperations().query(sql,new BeanPropertyRowMapper(Stu.class));
return list;
}
public List getAllStu(Stu stu) {
String sql="select s_id as sid,s_name as sname,s_sex as ssex,s_brith as sbrith from stu where s_id=:sid";
SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
return namedjdbctemp.query(sql, ps, new BeanPropertyRowMapper(Stu.class));
}
public Stu getOneStu(Stu stu) {
String sql="select s_id as sid,s_name as sname,s_sex as ssex,s_brith as sbrith from stu where s_id=:sid";
SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
return (Stu)namedjdbctemp.queryForObject(sql, ps, new BeanPropertyRowMapper(Stu.class));
}
public String getStuName(Stu stu) {
String sql="select s_name as sname from stu where s_name=:sname";
SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
return (String)namedjdbctemp.queryForObject(sql, ps, String.class);
}
public void updStu(Stu stu) {
String sql="update stu set s_name=:sname,s_sex=:ssex,s_brith=:sbrith where s_id=:sid";
SqlParameterSource ps=new BeanPropertySqlParameterSource(stu);
namedjdbctemp.update(sql, ps);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.