Spring JDBC 框架
1.Spring JDBC 介绍
数据库连接池连接 之 类图
Spring 框架提供的JDBC支持主要由四个包组成,分别是 core(核心包)、object(对象包)、dataSource(数据源包)和 support(支持包)。
Spring JDBC 核心 之 JdbcTemplate类:
JdbcTemplate模板类,通过该对象对数据库操作进行封装,可以提供一种对数据库进行简化操作的API。JdbcTemplate 类中包含了所有数据库操作的基本方法。
JdbcTemplate 类继承自抽象类 JdbcAccessor,同时实现了 JdbcOperations 接口。其直接父类 JdbcAccessor 为子类提供了一些访问数据库时使用的公共属性。
- execute()—用于执行任何SQL语句,DDL语句为主
- update(), batchUpdate()—执行新增、修改、删除等语句
- query(),queryForXXX()—执行查询相关语句
- call()—执行存储过程、函数相关语句
final String sql = "insert into tableName values(?)";
final String sql = "update user tableName <attribute>=?";
final String sql ="delete from tableName where attribute[i] =?";
return getJdbcTemplate()
.update(sql
, user.get<attribute>());
}
final String sql = "select * from tableName";
List<Class> tableNameList = getJdbcTemplate()
.query(sql
, new RowMapper<Class>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
Class className = new Class();
className.set<attribute>(
resultSet.getInt(1)
.
.
.
,resultSet.getInt(n));
return tableName;
}
});
进行Spring核心配置文件 配置 (模板)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring容器 加载数据源配置文件:jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- Dao 层组 -->
<bean id="Dao" class=" ....">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 将JdbcTemplate模版类,交由Spring IOC容器来管理 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"/>
</bean>
</beans>
基于注解的 IOC 配置
环境搭建–添加AOP包支持
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
注入实现对象注入
- @Component 创建对象的注解 等介于
<bean id="" class="">
- @Controller 一般用于表现层的注解。
- @Service 一般用于业务层的注解。
- @Repository 一般用于持久层的注解。
用于注入数据的注解标签
- @Autowired 用在类中,进行自动注入。按类型自动注入。
- @Qualifier 在按类型注入的基础上,按名称注入。
- @Resource 直接按照 Bean 的 id 注入。
- @Value 注入基本数据类型和 String 类型数据的属性
生命周期相关的注解标签
- @PostConstruct 用于指定初始化方法。
- @PreDestroy 用于指定销毁方法。
-@Scope 设置作用范围
Spring注解和XML配置选择问题
- 注解的优势:配置简单,维护方便
- XML 的优势:修改时,不用改源码。不涉及重新编译和部署。
类的注解配置
@Configuration注解
用于指定当前类是一个 spring 配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration 注解的类.class)。
@ComponentScan注解
用于指定 spring 在初始化容器时要扫描的包。
@Bean注解
该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器
@PropertySource
用于加载.properties 文件中的配置。例如我们配置数据源时, 可以把连接数据库的信息写到properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。
@Import
用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。