利用 Spring 的 JdbcTemplate 处理 BLOB、CLOB

利用spring的jdbcTemplate处理blob、clob 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的数据库最喜欢搞搞特别的东西啦: ...

Created 16166-01-16 · Updated 9096-10-09 · 2 min · 219 words · -

JDBC URL

JDBC URL disable tls JDK8版本过高引起MySQL连接失败:javax.net.ssl.SSLHandshakeException: No appropriate protocol >[https://juejin.cn/post/6969142310718144520](https://juejin.cn/post/6969142310718144520) jdbc:mysql://127.0.0.1/database0?useunicode=true&characterencoding=utf8&tinyInt1isBit=false&useSSL=false MySQL MySQL Connector/J Driver 驱动程序包: http://mvnrepository.com/artifact/MySQL/MySQL-connector-java 驱动程序类名: com.MySQL.jdbc.Driver JDBC URL Format The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional: jdbc:MySQL://[host1][:port1][,[host2][:port2]]…[/[database]] [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...] Here is a simple example for a connection URL: jdbc:MySQL://localhost:3306/sakila?profileSQL=true JDBC URL: jdbc:MySQL://<host>:<port>/<database_name> 默认端口3306,如果服务器使用默认端口则port可以省略 MySQL Connector/J Driver 允许在URL中添加额外的连接属性jdbc:MySQL://<host>:<port>/<database_name>?property1=value1&property2=value2 常用的有两个,一个是gjt (Giant Java Tree) 组织提供的MySQL驱动,其JDBC Driver名称 (JAVA类名) 为: org.gjt.mm.MySQL.Driver ...

Created 5056-03-05 · Updated 9096-10-09 · 2 min · 350 words · -

PreparedStatementCache

PreparedStatementCache http://www.cnblogs.com/alipayhutu/archive/2013/04/18/3029171.html 如何理解PreparedStatementCache,以及如何使用 为节约键盘敲击次数,记PreparedStatement为PS, PreparedStatementCache为PSCache,并且所有的SQL使用的绑定变量。 原理: http://www.dbafree.net/?p=287 http://agapple.iteye.com/blog/838286 http://singleant.iteye.com/blog/1298837 使用: 使用ps. sql里用?占位,等待被替换。例如,select * from table1 where user_name = ? and age > ? 启用pscache。 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> .... <property name="poolPreparedStatements" value="true" /> <property name="maxOpenPreparedStatements" value="10" /> .... </bean> 解析后的PS缓存,与重复利用。 sql语句,被发送到DB server端,经一系列处理 (语法解析、语义解析、结构优化) ,转化为一个树型结构 (sql, string -> tree) 。 3.1 sql解析后 (string -> tree) ,如何将ps放到pscache里? 首先,澄清个概念。执行树这个不是在应用客户端的,是在oracle服务器那边,server中也有pscache. 再者是说,只要客户端开启了PSCache,那么CS端都会缓存住这些个树 (尽管C端维护的结构可能简单一点) 。 底层会缓存 (LRU替换) ,用户不用关心。 3.2 假如pscache中,有一棵树是select * from table1 where user_name = ? and age > ? ,现在并发来了50个线程,都要用这棵树,如何复用? 执行树是一个具体的执行步骤了,是oracle服务器底层的具体执行步骤,所以再多的并发,直接拿过来用就是了,跟并发没有很多的关系。 可以理解为一个非状态的公共方法,就可以了,多个线程直接调用,复用这个方法,不存在瓶颈。 本质上来说,是oracle底层"包揽了"。底层逻辑: 根据sql (String及参数类型等各种信息) ,计算出一个key,看它在pscache里是否存在。 ...

Created 22226-09-22 · Updated 9096-10-09 · 1 min · 103 words · -

Spring Boot 关系型数据库持久层概览

概述 在 Spring Boot 里访问 MySQL、PostgreSQL、SQLite 等关系型数据库,常见有几条路线。它们最终都经过 JDBC 连到数据库,差别在于中间抽象层有多高、SQL 由谁写。 应用代码 │ ├── Spring Data JDBC ← Repository 风格,底层仍是 JDBC ├── Spring Data JPA ← 对象为主,框架生成 SQL │ └── JPA(标准)→ Hibernate(实现) ├── MyBatis (+ Plus) ← SQL 手写 + 映射 └── JdbcTemplate / JDBI / jOOQ ← SQL 路线(字符串或类型安全 DSL) 下面分别介绍 Spring Data JDBC、Spring Data JPA、MyBatis、JdbcTemplate、JDBI、jOOQ,并简要说明 JPA 本身是什么。 Spring Data JDBC Spring Data JDBC 是 Spring 官方提供的 轻量持久化方案,依赖 spring-boot-starter-data-jdbc。它提供与 Spring Data JPA 类似的 CrudRepository 接口风格,但 不是 ORM,底层仍通过 JdbcTemplate 访问数据库。 ...

Created 22226-05-22 · Updated 20206-05-20 · 5 min · 974 words · -