Spring框架往期文章
- Spring框架(一)–spring搭建和IOC
- Spring框架(二)–Spring Bean管理
- Spring框架(三)–Spring JDBC
- Spring框架(四)–AOP面向切面编程
- Spring框架(五)–Spring事务管理和Spring事务传播行为
Spring 是个一站式框架:Spring 自身也提供了控制层的 SpringMVC 和 持久层的 Spring JdbcTemplate。
以前我们的写法
package com.spring0.test;
import com.alibaba.druid.pool.DruidDataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcUtil {
public Connection getConnection() throws SQLException {
// DriverManager.getConnection()
DruidDataSource source = new DruidDataSource();
source.setUrl("");
source.setUsername("");
source.setPassword("");
source.setDriverClassName("");
return source.getConnection();
}
}
如今的高级写法
1.导入jar包的坐标
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2.创建一个config.properties的配置文件
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis_db?characterEncoding=utf8&serverTimezone=Asia/Shanghai
uname=root
password=root
3.创建一个db.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"
xmlns:context="http://www.springframework.org/schema/context"
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
">
<!-- 读取属性文件 -->
<context:property-placeholder location="config.properties"></context:property-placeholder>
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--spring生成数据库连接管理对象-->
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${uname}"></property>
<property name="password" value="${password}"></property>
<property name="initialSize" value="10"></property>
<property name="minIdle" value="5"></property>
<property name="maxActive" value="20"></property>
</bean>
<!--在配置文件中创建 JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dateSource"></property>
</bean>
</beans>
4.在spring.xml中导入db.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"
xmlns:context="http://www.springframework.org/schema/context"
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开启注解扫描 -->
<context:component-scan base-package="com.spring0"></context:component-scan>
<import resource="db.xml"></import>
</beans>
5.测试
JdbcTemplate 中常用的方法
execute:无返回值,可执行 ddl,增删改语句
update:执行新增、修改、删除语句;
queryForXXX:执行查询相关语句;
package com.spring0.dao;
import com.alibaba.druid.pool.DruidDataSource;
import com.spring0.bean.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@Repository
public class UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
public void save(){
//DruidDataSource自动获得connection 连接池
//jdbcTemplate默认使用事务提交,执行完立即提交
jdbcTemplate.update("insert into dept(name)values ('1111')");
int c= 10/0;
jdbcTemplate.update("insert into dept(name)values ('2222')");
}
}
现在我们大部分使用的是MyBatis,不在使用Spring自带的JdbcTemplate,这种方式也只做了解