Clob/Blob:
- /**//**
- * @author YunHongTao
- */
- public class ExampleSpringLobDao extends JdbcDaoSupport {
- /**//**
- * Comment for <code>lobHandler</code>
- * 处理blob、clob字段的对象 提供了方便的处理方法 一定要先初始化
- * 在配置文件可以如下指定
- * <bean id="nativeJdbcExtractor"
- * class="org.springframework.jdbc.support.nativejdbc.WebLogicNativeJdbcExtractor" lazy-init="true"/>
- * <bean id="oracleLobHandler"
- * class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
- * <property name="nativeJdbcExtractor"><ref local="nativeJdbcExtractor"/></property>
- * </bean>
- * <bean id="ExampleSpringLobDao" class="test.ExampleSpringLobDao">
- * <property name="dataSource">
- * <ref local="dataSource"/>
- * </property>
- * <property name="lobHandler">
- * <ref bean="oracleLobHandler"/>
- * </property>
- * </bean>
- */
- private LobHandler lobHandler;
- private String text = "萨克雷发动机三卡领导及罚款司机阿东风口浪尖萨克路灯风纪扣拉萨定界符可滤色镜阿斗罚款滤色镜打伏击送达可分离/n"
- + "萨雷队交锋拉开三角枫框架萨克路风机司克拉风纪扣拉萨定界符可立即撒风口浪尖送达风/n"
- + "口浪尖斯大林咖啡碱四克拉定界符可怜三大件罚款来三大件分厘卡三大件罚款拉萨定界符/n";
- /**//**插入clob字段的方法示例
- * @param id
- * @param name
- * @param text
- * @throws DataAccessException
- */
- public void insertClob(final int id, final String name, final String text)
- throws DataAccessException {
- getJdbcTemplate().execute(
- "INSERT INTO yuntest (id,name,text) VALUES (?, ?, ?)",
- new AbstractLobCreatingPreparedStatementCallback(
- this.lobHandler) {
- protected void setValues(PreparedStatement ps,
- LobCreator lobCreator) throws SQLException {
- ps.setInt(1, id);
- ps.setString(2, name);
- lobCreator.setClobAsString(ps, 3, text);
- }
- });
- }
- /**//**更新clob字段的方法示例
- * @param name
- * @param text
- * @param id
- * @throws DataAccessException
- */
- public void updateClob(final String name, final String text, final int id)
- throws DataAccessException {
- getJdbcTemplate().execute(
- "UPDATE yuntest set name=?,text=? where id=?",
- new AbstractLobCreatingPreparedStatementCallback(
- this.lobHandler) {
- protected void setValues(PreparedStatement ps,
- LobCreator lobCreator) throws SQLException {
- ps.setString(1, name);
- lobCreator.setClobAsString(ps, 2, text);
- ps.setInt(3, id);
- }
- });
- }
- /**//**查询clob字段的方法示例
- * @return
- * @throws DataAccessException
- */
- public List getClob() throws DataAccessException {
- return getJdbcTemplate().query(
- "SELECT id,name,text FROM yuntest where id=? ",
- new Object[] { new Integer(999999) }, new RowMapper() {
- public Object mapRow(ResultSet rs, int rowNum)
- throws SQLException {
- int id = rs.getInt(1);
- String name = rs.getString(2);
- String text = lobHandler.getClobAsString(rs, 3);
- YunTestBean vo = new YunTestBean();
- vo.setId(id);
- vo.setName(name);
- vo.setText(text);
- return vo;
- }
- });
- }
- public void deleteClob() {
- String sql = "delete from yuntest where id = 999999";
- getJdbcTemplate().execute(sql);
- }
- /**//**
- * @return Returns the lobHandler.
- */
- public LobHandler getLobHandler() {
- return lobHandler;
- }
- /**//**
- * @param lobHandler
- * The lobHandler to set.
- */
- public void setLobHandler(LobHandler lobHandler) {
- this.lobHandler = lobHandler;
- }
- }
Insert/Update/Delete:
- String sql = "insert into " + tableName
- + "(id, ipproleid, cpermissionid, iisdelete)"
- + " values(" + getSeqID(tableName) + ",?,?,?)";
- Object[] values = new Object[] { domain.getIpproleid(),
- domain.getCpermissionid(),domain.getIisdelete() };
- this.getJdbcTemplate().update(sql, values);
Select:
- String sql = "select * from " + tableName + " where cpermissionid=? and ipproleid=?";
- Object[] values = new Object[] { permissionID,pproleType };
- List list = this.getJdbcTemplate().query(sql, values,
- new BaseRowMapper(M2PpuserPermission.class));
- return (list == null || list.size() == 0 ? null : (M2PpuserPermission) list
- .get(0));
- public final class BaseRowMapper implements RowMapper {
- private Class cls;
- /**//**
- * @param dao
- */
- public BaseRowMapper(Class cls) {
- this.cls = cls;
- }
- public Object mapRow(ResultSet rs, int row) throws SQLException {
- if (rs != null) {
- return RSUtil.toBean(rs, cls);
- } else {
- return null;
- }
- }
- }
批量更新:
- final Object[] propValue = getColumnPropValue(domain);
- final Object[] propName = getColumnPropName();
- String INSERTPROPSQL = "insert into M_COMMON_COLUMN_PROP "
- + " (icolumnid, cpropname, cpropvalue) values(?,?,?)";
- getJdbcTemplate().batchUpdate(INSERTPROPSQL,
- new BatchPreparedStatementSetter() {
- public int getBatchSize() {
- return propName.length;
- }
- public void setValues(PreparedStatement ps, int i)
- throws SQLException {
- ps.setInt(1, id);
- ps.setString(2, propName[i].toString());
- ps.setString(3, propValue[i].toString());
- }
- });