备忘录

普通JDBC和Spring JDBC区别

2021-01-06 · 4 min read
Java

以MySQL为例,参考12
首先得有—个叫连接(Connection) 的东西,用来代表和数据库的连接;想执行SQL 怎么办?用一个Statement 来表示;SQL 返回的结果也得有—个抽象的概念: ResultSet 。

//只需要mysql-connector-java-5.1.49.jar
public static void main(String[] args) throws ClassNotFoundException, SQLException {
	Class.forName("com.mysql.jdbc.Driver");
	Connection connection = DriverManager.getConnection(
			"jdbc:mysql://localhost:3306/mycart?useSSL=false&useUnicode=true&characterEncoding=utf8", "root",
			"1234");
	Statement statement = connection.createStatement();
	ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM userinfo");
	resultSet.next();
	int c0 = resultSet.getInt(1);
	System.out.println(c0);
	resultSet.close();
	statement.close();
	connection.close();

	MysqlDataSource ds = new MysqlDataSource();
	ds.setURL("jdbc:mysql://localhost:3306/mycart?useSSL=false&useUnicode=true&characterEncoding=utf8");
	ds.setUser("root");
	ds.setPassword("1234");
	Connection conn = ds.getConnection();
	Statement state = conn.createStatement();
	ResultSet res = state.executeQuery("SELECT COUNT(*) FROM userinfo");
	res.next();
	int c1 = res.getInt(1);
	System.out.println(c1);
	res.close();
	state.close();
	conn.close();		
}

Spring JdbcTemplate(除了MySQL驱动还需要5个Spring基础包及Jdbc和tx事务包)

public static void main(String[] args) {
	MysqlDataSource ds = new MysqlDataSource();
	ds.setURL("jdbc:mysql://localhost:3306/mycart?useSSL=false&useUnicode=true&characterEncoding=utf8");
	ds.setUser("root");
	ds.setPassword("1234");
	int c0 = new JdbcTemplate(ds).queryForObject("SELECT COUNT(*) FROM userinfo", Integer.class);
	System.out.println(c0);

	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
	int c1 = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM userinfo", Integer.class);
	System.out.println(c1);
	((ClassPathXmlApplicationContext)applicationContext).close();
}

当然applicationContext.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-4.3.xsd">
	<!--1配置数据源 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<!--连接数据库的ur1 -->
		<property name="url" value="jdbc:mysql://localhost:3306/mycart" />
		<!--连接数据库的用户名 -->
		<property name="username" value="root" />
		<!--连接数据库的密码 -->
		<property name="password" value="1234" />
	</bean>
	<!--2配置JDBC模板 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!--默认必须使用数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 定义id为userDao的Bean -->
	<bean id="userDao" class="com.ssm.jdbc.UserDaoImpl">
		<!--将 jdbcTemplate注入到 userDao实例中 -->
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>	
</beans>