JDBC批处理和Hibernate的query方法

<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1,JDBC</span></span><span style="">提供了对批处理的支持,但是不同的驱动对批处理的实现不一样,未必会提高性能。批处理代码:</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style=""> <textarea cols="63" rows="15" name="code" class="java">static void createBatch() throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "insert into user(name,birthday, money) values (?, ?, ?) ";
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for (int i = 0; i &lt; 100; i++) {
ps.setString(1, "batch name" + i);
ps.setDate(2, new Date(System.currentTimeMillis()));
ps.setFloat(3, 100f + i);

ps.addBatch();
}
int[] is = ps.executeBatch();
} finally {
JdbcUtils.free(rs, ps, conn);
}
}</textarea></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">2.query</span><span style="">中的常用方法:</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">List</span><span style="">,所有数据都返回</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">UniqueResult </span><span style="">查询唯一</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">setFirstResult </span><span style="">设置首个结果</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">setMaxResult<span style=""> </span></span><span style="">设置最大结果</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="">使用</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">heibernate</span><span style="">的心得:在对象设计比较复杂的时候,比较适合采用</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">Hibernate</span><span style="">,如果比较简单的时候,适合用</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">JDBC</span><span style="">直接实现。</span><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"></span></p>

你可能感兴趣的:(Hibernate)