更新时间:2022-10-09 13:10:51
上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的DAO模块对JDBC的支持,以及Spring对事务的控制…
对于JDBC而言,我们肯定不会陌生,我们在初学的时候肯定写过非常非常多的JDBC模板代码!
我们来回忆一下我们怎么对模板代码进行优化的!
try { String sql = "insert into t_dept(deptName) values('test');"; Connection con = null; Statement stmt = null; Class.forName("com.mysql.jdbc.Driver"); // 连接对象 con = DriverManager.getConnection("jdbc:mysql:///hib_demo", "root", "root"); // 执行命令对象 stmt = con.createStatement(); // 执行 stmt.execute(sql); // 关闭 stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); }
/* * 连接数据库的driver,url,username,password通过配置文件来配置,可以增加灵活性 * 当我们需要切换数据库的时候,只需要在配置文件中改以上的信息即可 * * */ private static String driver = null; private static String url = null; private static String username = null; private static String password = null; static { try { //获取配置文件的读入流 InputStream inputStream = UtilsDemo.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); //获取配置文件的信息 driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); //加载驱动类 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,username,password); } public static void release(Connection connection, Statement statement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }
上面已经回顾了一下以前我们的JDBC开发了,那么看看Spring对JDBC又是怎么优化的
首先,想要使用Spring的JDBC模块,就必须引入两个jar文件:
public void save() { try { String sql = "insert into t_dept(deptName) values('test');"; Connection con = null; Statement stmt = null; Class.forName("com.mysql.jdbc.Driver"); // 连接对象 con = DriverManager.getConnection("jdbc:mysql:///hib_demo", "root", "root"); // 执行命令对象 stmt = con.createStatement(); // 执行 stmt.execute(sql); // 关闭 stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } }
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="10"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean>
// IOC容器注入 private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void save() { try { String sql = "insert into t_dept(deptName) values('test');"; Connection con = null; Statement stmt = null; // 连接对象 con = dataSource.getConnection(); // 执行命令对象 stmt = con.createStatement(); // 执行 stmt.execute(sql); // 关闭 stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///zhongfucheng"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="10"></property> <property name="maxStatements" value="100"></property> <property name="acquireIncrement" value="2"></property> </bean> <!--扫描注解--> <context:component-scan base-package="bb"/> <!-- 2. 创建JdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
package bb; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; /** * Created by ozc on 2017/5/10. */ @Component public class UserDao implements IUser { //使用Spring的自动装配 @Autowired private JdbcTemplate template; @Override public void save() { String sql = "insert into user(name,password) values('zhoggucheng','123')"; template.update(sql); } }
@Test public void test33() { ApplicationContext ac = new ClassPathXmlApplicationContext("bb/bean.xml"); UserDao userDao = (UserDao) ac.getBean("userDao"); userDao.save(); }