七十二、Spring与DAO操作 execute()

Spring 与 Dao 部分,是 Spring 的两大核心技术 IoC 与 AOP 的典型应用体现

  • 对于 JDBC 模板的使用,是 IoC 的应用,是将 JDBC 模板对象注入给了 Dao 层的实现类。
  • 对于 Spring 的事务管理,是 AOP 的应用,将事务作为切面织入到了 Service 层的业务方法中。

目录

Spring 与 JDBC 模板

方法说明

jdbc实战操作


Spring 与 JDBC 模板

        为了避免直接使用 JDBC 而带来的复杂且冗长的代码,Spring 提供了一个强有力的模板 类---JdbcTemplate 来简化 JDBC 操作。并且,数据源 DataSource 对象与模板 JdbcTemplate 对象均可通过 Bean 的形式定义在配置文件中,充分发挥了依赖注入的威力。

概念:

什么是 JdbcTemplate?

        Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作

        Spring JDBC模块主要由4个包组成,分别是core(核心包)、dataSource(数据源包)、object(对象包)和support(支持包)。

包名 说明
core 包含了JDBC的核心功能,包括JdbcTemplate类、sinpleJdbcInsert类、SimpleJdbcCal1类以及NamedParameterJdbcTemplate类。
dataSource 访问数据源的实用工具类,它有多种数据源的实现,可以在JavaEE容器外部测试JDBC代码。
object 以面向对象的方式访问数据库,它允许执行查询并将返回结果作为业务对象,可以在数据表的列和业务对象的属性之间映射查询结果。
support 包含了core和object包的支持类,例如,提供异常转换功能的SQLException类。

        从上表可以看出,Spring对数据库的操作都封装在了这几个包中,而想要使用Spring JDBC,就雪要对其进行配置。

配置数据源
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>property name="username" value="root"/>
<property name="password" value="root"/>
<bean>
配置JDBC模板
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>注入数据源
</bean>
<bean id="xxx" class="Xxx">配置需要实例化的Bean
<properame="jdbcTemplate" ref="jdbcTemplate"/>注入JDBC模板
</bean>

        关于上述示例dataSource配置中的4个属性说明,如下表所示:

属性名 含义
driverClassName 所使用的驱动名称,对应驱动JAR包中的 Driver类。
url 数据源所在地址。
username 访问数据库的用户名。
password 访问数据库的密码

方法说明

在JdbcTemplate核心类中,提供了大量的更新和查询数据库的方法,我们就是使用的这些方法来操作数据库的。

方法 说明
execute() execute(String sql)方法可用于执行sql语句
update() update()用于执行插入、更新和删除操作
query() update()用于执行数据查询操作

                                                        ✨✨✨我是分割线✨✨✨

jdbc实战操作

        1、创建package

         2、src下创建 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.cj.jdbc.Driver" />
		<!--连接数据库的url -->
		<property name="url"
			value="jdbc:mysql://localhost:3306/spring?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=GMT&amp;useSSL=false" />
		<!--连接数据库的用户名 -->
		<property name="username" value="root" />
		<!--连接数据库的密码 -->
		<property name="password" value="123456" />
	</bean>
	<!-- 2配置JDBC模板 -->
	<bean id="jdbcTemplate"
		class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 默认必须使用数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>

        3、lib目录下引入 jar 包

SSM-jar包全家桶:https://tuomasi.lanzouy.com/b02uszjch

        密码:jars

        4、创建class

package com.Example.jdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {
	/**
	 * 使用execute()方法建表
	 */
	public static void main(String[] args) {
		// 加载配置文件
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 获取JdbcTemplate实例
		JdbcTemplate jdTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
		// 使用execute()方法执行SQL语句,创建用户账户管理表account
		jdTemplate.execute("create table account(" + "id int primary key auto_increment," + "username varchar(50),"
				+ "balance double)");
		System.out.println("账户表account创建成功!");
	}

}

        5、创建数据库

 create database spring;
mysql> create database spring;
Query OK, 1 row affected (0.02 sec)

mysql> use spring;
Database changed
mysql> show tables;
Empty set (0.02 sec)

         6、运行程序

        7、查看数据库

mysql> use spring;
Database changed
mysql> show tables;
+------------------+
| Tables_in_spring |
+------------------+
| account          |
+------------------+
1 row in set (0.00 sec)

mysql> desc account;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int         | NO   | PRI | NULL    | auto_increment |
| username | varchar(50) | YES  |     | NULL    |                |
| balance  | double      | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

        注:account 数据表已经创建成功。

猜你喜欢

转载自blog.csdn.net/m0_54925305/article/details/123149019