您的位置:首页 > 编程语言 > Java开发

Spring中jdbcTemplate

2010-01-19 17:43 176 查看
首先在applicationContext.xml中配置数据源dataSource,jdbcTemplate,dao。
<!-- 配置数据源-->
<bean id="dataSource" scope="singleton" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.1.3:8088/test/>
<property name="username" value="root"/>
<property name="password" value="123"/>
............
</bean>
<!--配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" scope="singleton"/>
<!--配置DAO-->
<bean id="fillQuestionDAO" class="com.biao.dao.FillQuestionDAO" scope="singleton"/>
在具体DAO中:FillQuestionDAO
public class FillQuestionDAO implements IFillQuestionDAO{
/**
* 得到一个数据库连接
*/
private JdbcTemplate jdbcTemplate;
/**
* @return 获得 jdbcTemplate
*/
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
/**
* @param jdbcTemplate 设置 jdbcTemplate
*/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 功能:增加新的临时填空题
*/
public Long addFillQuestion(final FillQuestion fillQuestion) {
final StringBuffer sql=new StringBuffer();
sql.append(" insert into filltemp");
sql.append("(contentTempID,fillTemp,answerOne,parseOne) ");
sql.append(" values(?,?,?,?); ");
//临时填空题表中的id为auto_increment,所以可以以下步骤
GeneratedKeyHolder gkHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator(){
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
PreparedStatement ps=con.prepareStatement(sql.toString());
ps.setLong(1, fillQuestion.getContentTempID());
ps.setString(2, fillQuestion.getFillTemp());
ps.setString(3, fillQuestion.getAnswerOne());
ps.setString(4, fillQuestion.getParseOne());
return ps;
}
},gkHolder);
return gkHolder.getKey().longValue();
}
//如果id不为自增,无返回值
public void addFillQuestion(final FillQuestion fillQuestion) {
final StringBuffer sql=new StringBuffer();
sql.append(" insert into filltemp");
sql.append("(contentTempID,fillTemp,answerOne,parseOne) ");
sql.append(" values(?,?,?,?); ");
jdbcTemplate.update(sql.toString(),new PreparedStatementSetter(){
public void setValues(PreparedStatement ps) throws SQLException {
ps.setLong(1, fillQuestion.getContentTempID());
ps.setString(2, fillQuestion.getFillTemp());
ps.setString(3, fillQuestion.getAnswerOne());
ps.setString(4, fillQuestion.getParseOne());
}
});
}
/* 功能:删除临时填空题
* @see com.huazuo.bw.dao.question1_5.IFillQuestionDAO#deleteFillQuestion(com.huazuo.bw.po.FillQuestion)
*/
public void deleteFillQuestion(final long fillQuestionID) {
String sql="delete from filltemp where fillTempID=?";
jdbcTemplate.update(sql,new PreparedStatementSetter(){
public void setValues(PreparedStatement ps) throws SQLException {
ps.setLong(1,fillQuestionID);
}
});
}
/**
* 功能:更新临时填空题
* @param fillQuestion
*/
public void updateFillQuestion(final FillQuestion fillQuestion) {
StringBuffer sql=new StringBuffer();
sql.append("update filltemp set fillTempID=?,contentTempID=?,fillTemp=?,answerOne=?,parseOne=?");
sql.append(" where fillTempID=?");
jdbcTemplate.update(sql.toString(),new PreparedStatementSetter(){
public void setValues(PreparedStatement pstmt) throws SQLException {
int index=1;
pstmt.setLong(index++, fillQuestion.getFillQuestionID());
pstmt.setLong(index++,fillQuestion.getContentTempID());
pstmt.setString(index++, fillQuestion.getFillTemp());
pstmt.setString(index++, fillQuestion.getAnswerOne());
pstmt.setString(index++, fillQuestion.getParseOne());
pstmt.setLong(index++,fillQuestion.getFilQuestionID());
}
});
}
/**
* 功能:通过一个临时填空题ID来查询这个临时填空
* @param fillQuestionID
* @return
*/
public FillQuestion findFillQuestion(final Long fillQuestionID) {
String sql="select fillTempID,contentTempID,fillTemp,answerOne,parseOne from fillTemp where fillTempID=?";
return (FillQuestion) jdbcTemplate.query(sql, new PreparedStatementSetter(){
public void setValues(PreparedStatement ps) throws SQLException {
ps.setLong(1, fillQuestionID);
}
},new RowMapper(){
public Object mapRow(ResultSet rs, int arg1) throws SQLException {
FillQuestion fillQuestion=new FillQuestion();
fillQuestion.setFillQuestionID(rs.getLong(1));
fillQuestion.setContentTempID(rs.getLong(2));
fillQuestion.setFillTemp(rs.getString(3));
fillQuestion.setAnswerOne(rs.getString(4) );
fillQuestion.setParseOne(rs.getString(5));
return fillQuestion;
}
}).get(0);
}
/**功能:查询出所有的填空
* @return
*/
public List<FillQuestion> findAllFillQuestion() {
String sql="select fillTempID,contentTempID,fillTemp,answerOne,parseOne from fillTemp";
List<FillQuestion> list=null;
list=jdbcTemplate.query(sql, new PreparedStatementSetter(){
public void setValues(PreparedStatement pstmt) throws SQLException {
}},new RowMapper(){
public Object mapRow(ResultSet rs, int arg1)
throws SQLException {
FillQuestion fillQuestion=new FillQuestion();
fillQuestion.setFillQuestionID(rs.getLong(1));
fillQuestion.setContentTempID(rs.getLong(2));
fillQuestion.setFillTemp(rs.getString(3));
fillQuestion.setAnswerOne(rs.getString(4) );
fillQuestion.setParseOne(rs.getString(5));
return fillQuestion;
}
});
return list;
}
}
/**
* 功能:查询某个用户的购物点数余额
* @param userID:用户ID
* @return
*/
public double getShoppingPointsBalance(long userID) {
String sql="select shoppingPointsBalance from useraccount where userID=?";
String result=(String)jdbcTemplate.queryForObject(sql.toString(),new Object[]{userID},,java.lang.String.class);
double points=Double.parseDouble(result);
return points;
}
1)执行查询:
除了execute方法之外,还有其他大量的查询方法。在这些查询方法中,有很大一部分是用来查询单值的,比如返回一个汇总(count)结果或者从返回行结果中取得指定列的值。可以用queryForInt(..),queryForLong(..)或queryForObject(..)方法。 queryForObject方法用来将返回的JDBC类型对象转换成指定的java对象。
int count=jdbcTemplate.queryForInt("select count(*) from mytable");
String name=(String)jdbcTemplate.queryForObject("select name from mytable",String.class);
除了返回单值的查询方法,还有一组返回List结果的方法。如queryForList.
List rows=jdbcTemplate.queryForList("select * from mytable");
[{name=Bob,id=1},{name=Mary,id=2}]
2)更新数据库
jdbcTemplate.update("update mytable set name=? where id=?",new Object[]{name,new Integer(id)});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: