Spring 使java開發變得容易,為開發者節省了不少時間,其中包括數據庫的連接與使用。下面將使用三種方式連接數據庫:傳統的jdbc方式、springjdbc方式。
1.傳統jdbc方式
首先項目需要導 mysql-connector-java jar包
或者采用maven導入jar包
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
代碼如下:
// 1通過直接賦值方式配置 // String url = "jdbc:mysql:///dbname"; // String user = "root"; // String password = ""; // String driverClass = "com.mysql.jdbc.Driver"; // 2通過.properties配置文件讀取配置 InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("dbconfig.properties"); Properties properties = new Properties(); properties.load(inputStream); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.user"); String password = properties.getProperty("jdbc.password"); String driverClass = properties.getProperty("jdbc.driverClass"); // 連接操作 Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, user, password); // crud(增刪改查)操作 String sql = "sql_crud_statement"; preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); // 解析結果 Student student = null; while (resultSet.next()) { int id = resultSet.getInt("id"); int age = resultSet.getInt("age"); //do sth }
分析:這種傳統連接方式實現起來很簡單,但是代碼量相對較多,而且用完之后還要有手動釋放資源的操作,相對的需要有很多重讀代碼。
2.srping jdbc方式
當然,首要任務是引入包,涉及的包有一點小多,所以首選maven來導入
<!--MySQL Driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.5.RELEASE</version> </dependency>
配置好之后就可以進行令人激動地鏈接數據庫操作了
大致流程是首先通過框架里面的上下文機制獲取jdbcTemplate(數據模板)對象,然后再通過jdbcTemplate對象進行curd操作:
1.上下文的xml配置beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="url" value="jdbc:mysql:///spring_data"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
2.加載xml獲取jdbcTemplate對象,實現curd
ctx = new ClassPathXmlApplicationContext("beans.xml"); jdbcTemplate = ctx.getBean(“jdbcTemplate”); jdbcTemplate.query(sql, new RowCallbackHandler(){ // 重寫processRow 來實現自己的循環處理結果集 @Override public void processRow(ResultSet rs) throws SQLException { int id = rs.getInt("id"); String name = rs.getString("name"); //do sth } });
分析:這種方式雖然需要配置上下文,但是實現起來很容易,實現的代碼就那么幾行,看着效果不錯,而且jdbcTemplate對象是已經實現的對象,我們只需要重寫里面的processRow方法就可以實現。相比傳統方式,這種方法精進了不少,也不用考慮釋放資源。
然而這不是框架想要的全部,如果想要其他操作,比如分頁操作,實現起來也不是很容易,所有的sql還需要自己寫,也不是很方便啊,那么問題來了,有沒有真正的“好”的方式來實現呢?下一篇將分析下spring實現curd以及其優點。