Spring框架中JdbcTemplate類的查表功能演示(基於Druid連線池)
阿新 • • 發佈:2018-11-26
將資料庫行記錄轉為已知類的實現物件的思路,作為一個java後端程式設計師的基本修養,必須掌握,現舉其中一例:
步驟:
1.首先需要配置 Druid(阿里巴巴) 連線池環境,寫好工具類JDBCUtilsDruid,不再贅述;
2.配置Spring框架環境,不再贅述;
3.演示程式碼:
程式碼中用的資料庫為mysql,表資料見下圖:
為方便理解,Druid連線池工具類JDBCUtilsDruid也貼出來,如下:
package com.wen.utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JDBCUtilsDruid { private static DataSource ds=null; static{ InputStream is = JDBCUtilsDruid.class.getClassLoader().getResourceAsStream("druid.properties"); Properties properties=new Properties(); try { properties.load(is); ds= DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } public static DataSource getDateSource(){ return ds; } public static Connection getConnection() throws SQLException { return ds.getConnection(); } public static void close(ResultSet resultSet, Statement statement, Connection connection){ closeSource(resultSet); closeSource(statement); closeSource(connection); } public static void close(Statement statement,Connection connection){ close(null,statement,connection); } private static void closeSource(ResultSet resultSet){ try { if (resultSet!=null) resultSet.close(); } catch (SQLException e) { e.printStackTrace(); }finally { resultSet=null; } } private static void closeSource(Statement statement){ try { if (statement!=null) statement.close(); } catch (SQLException e) { e.printStackTrace(); }finally { statement=null; } } private static void closeSource(Connection connection){ try { if(connection!=null) connection.close(); } catch (SQLException e) { e.printStackTrace(); }finally { connection=null; } } }
與行記錄對應的Mylove類原始碼如下:
package com.wen.day05; import java.util.Objects; public class Mylove { private int id; private String name; public Mylove() { } public Mylove(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Mylove{" + "id=" + id + ", name='" + name + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Mylove mylove = (Mylove) o; return id == mylove.id && Objects.equals(name, mylove.name); } @Override public int hashCode() { return Objects.hash(id, name); } }
主要方法演示程式碼如下:
package com.wen.day05; import com.wen.utils.JDBCUtilsDruid; import org.junit.Test; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; public class Test08_JdbcTemplate_Retrive { @Test public void tests(){ //返回指定型別物件 test1(); //返回單條記錄的Map集合 test2(); //返回表的List<Map<K,V>>集合 test3(); //返回一個單條記錄對應的指定型別物件(使用RowMapper) test4(); //返回一個單條記錄對應的指定型別物件(使用BeanPropertyRowmapper) test5(); //返回一個多條記錄對應的指定物件的List集合 test6(); //使用query語句,且使用BeanRowmapper 並返回List<物件>集合 test7(); } //查詢返回單個值 private void test1() { String sql="SELECT name FROM mylove WHERE id=? "; JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtilsDruid.getDateSource()); String s = jdbcTemplate.queryForObject(sql, String.class, 1); System.out.println(s); System.out.println("========="); } //查詢一條記錄返回屬性的Map集合 private void test2() { String sql="SELECT * FROM mylove WHERE id=?"; JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtilsDruid.getDateSource()); Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql, 3); System.out.println(stringObjectMap); System.out.println("========="); } //查詢一張虛擬表返回一個List<Map<>>集合 private void test3() { String sql="SELECT * FROM mylove WHERE id IN (?,?,?)"; JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtilsDruid.getDateSource()); List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, 1, 2, 3); System.out.println(maps); System.out.println("========="); } //查詢一條記錄,並返回一個物件(RowMapper) private void test4() { String sql="SELECT * FROM mylove WHERE id=?"; JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtilsDruid.getDateSource()); Mylove mylove = jdbcTemplate.queryForObject(sql, new RowMapper<Mylove>() { @Override public Mylove mapRow(ResultSet resultSet, int i) throws SQLException { Mylove mylove = new Mylove(); mylove.setId(resultSet.getInt("id")); mylove.setName(resultSet.getString("name")); return mylove; } }, 1); System.out.println("使用Rowmapper"); System.out.println(mylove); System.out.println("=========="); } //查詢一條記錄,並返回一個物件(BeanRowmapper) private void test5() { String sql="SELECT * FROM mylove WHERE id=?"; JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtilsDruid.getDateSource()); Mylove mylove = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Mylove>(Mylove.class),2); System.out.println("使用BeanRowmapper"); System.out.println(mylove); System.out.println("=========="); } //使用query語句,且使用Rowmapper 並返回List<物件>集合 private void test6() { String sql="SELECT * FROM mylove WHERE id IN (?,?,?)"; JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtilsDruid.getDateSource()); List<Mylove> mylove = jdbcTemplate.query(sql, new RowMapper<Mylove>() { @Override public Mylove mapRow(ResultSet resultSet, int i) throws SQLException { Mylove mylove = new Mylove(); mylove.setId(resultSet.getInt("id")); mylove.setName(resultSet.getString("name")); return mylove; } }, 1, 2, 3); System.out.println("使用Rowmapper"); System.out.println(mylove); System.out.println("=========="); } //使用query語句,且使用BeanRowmapper 並返回List<物件>集合 private void test7() { String sql="SELECT * FROM mylove WHERE id IN (?,?,?)"; JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtilsDruid.getDateSource()); List<Mylove> mylove = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Mylove>(Mylove.class), 4, 2, 3); System.out.println("使用BeanRowmapper"); System.out.println(mylove); System.out.println("=========="); } }
執行結果如下:供各位比對:
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\jetBrains\apps\IDEA-C\ch-0\182.3684.101\lib\idea_rt.jar=54143:D:\jetBrains\apps\IDEA-C\ch-0\182.3684.101\bin -Dfile.encoding=UTF-8 -classpath "D:\jetBrains\apps\IDEA-C\ch-0\182.3684.101\lib\idea_rt.jar;D:\jetBrains\apps\IDEA-C\ch-0\182.3684.101\plugins\junit\lib\junit-rt.jar;D:\jetBrains\apps\IDEA-C\ch-0\182.3684.101\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;C:\Users\Administrator\Desktop\myprojects\out\production\mysql;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\mysql-connector-java-5.1.37-bin.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\c3p0-0.9.1.2.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\druid-1.0.9.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\JDBCTemplate\commons.logging-1.1.1.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\JDBCTemplate\spring-beans-5.0.2.RELEASE.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\JDBCTemplate\spring-core-5.0.2.RELEASE.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\JDBCTemplate\spring-jdbc-5.0.2.RELEASE.jar;C:\Users\Administrator\Desktop\myprojects\mysql\lib\JDBCTemplate\spring-tx-5.0.2.RELEASE.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.wen.day05.Test08_JdbcTemplate_Retrive,tests
九月 13, 2018 11:20:09 上午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
資訊: {dataSource-1} inited
黃晴
=========
{id=3, name=晴晴}
=========
[{id=1, name=黃晴}, {id=2, name=黃黃}, {id=3, name=晴晴}]
=========
使用Rowmapper
Mylove{id=1, name='黃晴'}
==========
使用BeanRowmapper
Mylove{id=2, name='黃黃'}
==========
使用Rowmapper
[Mylove{id=1, name='黃晴'}, Mylove{id=2, name='黃黃'}, Mylove{id=3, name='晴晴'}]
==========
使用BeanRowmapper
[Mylove{id=2, name='黃黃'}, Mylove{id=3, name='晴晴'}, Mylove{id=4, name='晴兒'}]
==========
Process finished with exit code 0
總結:
主要演示了JdbcTemplate中較為f繁雜的查詢方法:
test1();//返回指定型別物件
test2();//返回單條記錄的Map集合
test3();//返回表的List<Map<K,V>>集合
test4();//返回一個單條記錄對應的指定型別物件(使用RowMapper)
test5();//返回一個單條記錄對應的指定型別物件(使用BeanPropertyRowmapper)
test6();//返回一個多條記錄對應的指定物件的List集合
test7();//使用query語句,且使用BeanRowmapper 並返回List<物件>集合
-----------------------以上,供各位參考,也方便自己使用複製貼上da大法---------------------------