3个回答
展开全部
spring定义了一个以统一的方式操作各种数据库的Lob类型数据的LobCreator(保存的时候用),同时提供了一个LobHandler为操作二进制字段和大文本字段提供统一接口访问。
举例,例子里面的t_post表中post_text字段是CLOB类型,而post_attach是BLOG类型:
public class PostJdbcDao extends JdbcDaoSupport implements PostDao
{
private LobHandler lobHandler;
private DataFieldMaxValueIncrementer
incre;
public LobHandler getLobHandler() {
return
lobHandler;
}
public void setLobHandler(LobHandler lobHandler) {
this.lobHandler = lobHandler;
}
public void addPost(final Post post)
{
String sql = " INSERT INTO
t_post(post_id,user_id,post_text,post_attach)"
+ "
VALUES(?,?,?,?)";
getJdbcTemplate().execute(
sql,
new
AbstractLobCreatingPreparedStatementCallback(
this.lobHandler)
{
protected void setValues(PreparedStatement ps,
LobCreator
lobCreator) throws SQLException {
ps.setInt(1, incre.nextIntValue());
ps.setInt(2, post.getUserId());
lobCreator.setClobAsString(ps, 3, post.getPostText());
lobCreator.setBlobAsBytes(ps, 4, post.getPostAttach());
}
});
}
}
设置相对应的配置文件(Oracle 9i版本),Oracle的数据库最喜欢搞搞特别的东西啦:
<bean id="nativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
lazy-init="true" />
<bean id="oracleLobHandler"
class="org.springframework.jdbc.support.lob.OracleLobHandler"
lazy-init="true">
<property name="nativeJdbcExtractor"
ref="nativeJdbcExtractor" />
</bean>
<bean id="dao"
abstract="true">
<property name="jdbcTemplate" ref="jdbcTemplate"
/>
</bean>
<bean id="postDao" parent="dao"
class="com.baobaotao.dao.jdbc.PostJdbcDao">
<property
name="lobHandler" ref="oracleLobHandler" />
</bean>
Oracle 10g或其他数据库如下设置:
<bean id="defaultLobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<bean id="dao" abstract="true">
<property name="jdbcTemplate" ref="jdbcTemplate"
/>
</bean>
<bean id="postDao" parent="dao"
class="com.baobaotao.dao.jdbc.PostJdbcDao">
<property
name="lobHandler" ref="defaultLobHandler" />
</bean>
读取BLOB/CLOB块,举例:
public List getAttachs(final int userId){
String sql = "SELECT
post_id,post_attach FROM t_post where user_id =? and post_attach is not
null";
return getJdbcTemplate().query(
sql,new Object[]
{userId},
new RowMapper() {
public Object mapRow(ResultSet
rs, int rowNum) throws SQLException {
Post post = new
Post();
int postId = rs.getInt(1);
byte[] attach =
lobHandler.getBlobAsBytes(rs, 2);
post.setPostId(postId);
post.setPostAttach(attach);
return post;
}
});
}
举例,例子里面的t_post表中post_text字段是CLOB类型,而post_attach是BLOG类型:
public class PostJdbcDao extends JdbcDaoSupport implements PostDao
{
private LobHandler lobHandler;
private DataFieldMaxValueIncrementer
incre;
public LobHandler getLobHandler() {
return
lobHandler;
}
public void setLobHandler(LobHandler lobHandler) {
this.lobHandler = lobHandler;
}
public void addPost(final Post post)
{
String sql = " INSERT INTO
t_post(post_id,user_id,post_text,post_attach)"
+ "
VALUES(?,?,?,?)";
getJdbcTemplate().execute(
sql,
new
AbstractLobCreatingPreparedStatementCallback(
this.lobHandler)
{
protected void setValues(PreparedStatement ps,
LobCreator
lobCreator) throws SQLException {
ps.setInt(1, incre.nextIntValue());
ps.setInt(2, post.getUserId());
lobCreator.setClobAsString(ps, 3, post.getPostText());
lobCreator.setBlobAsBytes(ps, 4, post.getPostAttach());
}
});
}
}
设置相对应的配置文件(Oracle 9i版本),Oracle的数据库最喜欢搞搞特别的东西啦:
<bean id="nativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
lazy-init="true" />
<bean id="oracleLobHandler"
class="org.springframework.jdbc.support.lob.OracleLobHandler"
lazy-init="true">
<property name="nativeJdbcExtractor"
ref="nativeJdbcExtractor" />
</bean>
<bean id="dao"
abstract="true">
<property name="jdbcTemplate" ref="jdbcTemplate"
/>
</bean>
<bean id="postDao" parent="dao"
class="com.baobaotao.dao.jdbc.PostJdbcDao">
<property
name="lobHandler" ref="oracleLobHandler" />
</bean>
Oracle 10g或其他数据库如下设置:
<bean id="defaultLobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<bean id="dao" abstract="true">
<property name="jdbcTemplate" ref="jdbcTemplate"
/>
</bean>
<bean id="postDao" parent="dao"
class="com.baobaotao.dao.jdbc.PostJdbcDao">
<property
name="lobHandler" ref="defaultLobHandler" />
</bean>
读取BLOB/CLOB块,举例:
public List getAttachs(final int userId){
String sql = "SELECT
post_id,post_attach FROM t_post where user_id =? and post_attach is not
null";
return getJdbcTemplate().query(
sql,new Object[]
{userId},
new RowMapper() {
public Object mapRow(ResultSet
rs, int rowNum) throws SQLException {
Post post = new
Post();
int postId = rs.getInt(1);
byte[] attach =
lobHandler.getBlobAsBytes(rs, 2);
post.setPostId(postId);
post.setPostAttach(attach);
return post;
}
});
}
展开全部
将nativeJdbcExtractor 设置为 lazy-init="true",或使用判断 this.nativeJdbcExtractor != null && this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()
这是因为 nativeJdbcExtractor 需要通过运行期的反射机制获取底层的 JDBC 对象, 所以需要避免在 Spring 容器启动时就实例化这两个Bean。
这是因为 nativeJdbcExtractor 需要通过运行期的反射机制获取底层的 JDBC 对象, 所以需要避免在 Spring 容器启动时就实例化这两个Bean。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-11-27
展开全部
我问一句,你从哪里弄来的这一串字母?应该是乱打的吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询