package org.spring.service;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.spring.entity.XsaJbxx;
import org.spring.util.Pagination;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
/**
* spring中的JDBC
*
* @author Administrator
*
*/
@SuppressWarnings("unchecked")
public class SpringJdbcServiceImpl {
@Resource(name = "jdbcTemplate")
JdbcTemplate jdbcTemplate;
public XsaJbxx findByEntity(String xm, String xh) {
String sql = "select * from xsa_jbxx x where x.xm=? and x.xh=?";
final XsaJbxx xs = new XsaJbxx();
this.jdbcTemplate.query(sql, new Object[] { xm, xh },
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
xs.setXsid(rs.getString("xsid"));
xs.setXm(rs.getString("xm"));
xs.setBh(rs.getString("bh"));
xs.setXxdm(rs.getString("xxdm"));
xs.setBmlx(rs.getString("bmlx"));
xs.setXh(rs.getString("xh"));
}
});
return xs;
}
String sql = "select * from xsa_jbxx x where x.xxdm=?";
public List<XsaJbxx> findByList(final String xxdm) {
return this.jdbcTemplate.query(sql, new Object[] { xxdm },
new RowMapper() {
public Object mapRow(ResultSet rs, int arg1)
throws SQLException {
XsaJbxx xs = new XsaJbxx();
xs.setXsid(rs.getString("xsid"));
xs.setXm(rs.getString("xm"));
xs.setBh(rs.getString("bh"));
xs.setXxdm(rs.getString("xxdm"));
xs.setBmlx(rs.getString("bmlx"));
xs.setXh(rs.getString("xh"));
return xs;
}
});
}
/**
* 返回分页数据
* @param xxdm
* @param first
* @param step
* @return
*/
public List<XsaJbxx> findByPage(String xxdm, int first, int step) {
Pagination p = new Pagination(sql.toString(), new Object[] { xxdm },
XsaJbxx.class, first, step, jdbcTemplate);
return p.getResultList();
}
public int findByPageTotalRows(String xxdm, int first, int step) {
Pagination p = new Pagination(sql.toString(), new Object[] { xxdm },
XsaJbxx.class, first, step, jdbcTemplate);
return p.getTotalRows();
}
public List<XsaJbxx> findByList1(final String xxdm) {
return this.jdbcTemplate.query(sql, new Object[] { xxdm },
ParameterizedBeanPropertyRowMapper.newInstance(XsaJbxx.class));
}
public String findByXm(String xh) {
String sql = "select x.xm from xsa_jbxx x where x.xh=?";
return (String) this.jdbcTemplate.queryForObject(sql,
new Object[] { xh }, String.class);
}
public int findByCont(String xxdm) {
String sql = "select count(*) from xsa_jbxx x where x.xxdm=?";
return this.jdbcTemplate.queryForInt(sql, new Object[] { xxdm });
}
public void update(final String xm, final String bh, final String xh) {
String sql = "update Xsa_jbxx x set x.xm=? ,x.bh=? where x.xh=? ";
this.jdbcTemplate.update(sql, new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, xm);
ps.setString(2, bh);
ps.setString(3, xh);
}
});
}
public void update1(String xm, String bh, String xh) {
String sql = "update Xsa_jbxx x set x.xm=? ,x.bh=? where x.xh=? ";
this.jdbcTemplate.update(sql, new Object[] { xm, bh, xh });
}
public void insert(XsaJbxx xsajbxx) {
String sql = "insert into xsa_jbxx(xsid,xm,bh,xxdm,xh,bmlx) values(?,?,?,?,?,?)";
Object[] obj = new Object[] { xsajbxx.getXsid(), xsajbxx.getXm(),
xsajbxx.getBh(), xsajbxx.getXxdm(), xsajbxx.getXh(),
xsajbxx.getBmlx() };
this.jdbcTemplate.update(sql, obj);
}
public void delete(String xh) {
String sql = "delete from xsa_jbxx x where x.xh=? ";
this.jdbcTemplate.update(sql, new Object[] { xh });
}
}
spring 中的jdbc
最新推荐文章于 2022-08-21 16:56:27 发布