目录
一、JDBC模板类的使用
1.引入依赖
2.测试类
3.运行,查看数据库
二、使用Spring框架来管理模板类
1.配置文件
2.测试类
3.运行,查看数据库
三、Spring框架管理开源的连接池
1.配置开源的连接池
2.将数据库连接的信息配置到属性文件中
3.核心配置
四、Spring框架的JDBC模板的简单操作
1.测试类
2.实现类
3.之前的数据库
4.运行
(1)run1()----增
(2)运行run2()----删
编辑
(3)运行run3()---改
(4)运行run4()---通过id查
(5)运行run5()---查询所有
一、JDBC模板类的使用
1.引入依赖
<dependencies><!--spring的核心依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!--日志--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.0.2.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.2.RELEASE</version></dependency>
</dependencies>
2.测试类
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;public class JdbcTest {@Testpublic void run1(){//创建连接池对象,Spring框架内置了连接池对象DriverManagerDataSource dataSource=new DriverManagerDataSource();//设置四个参数dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql:///spring_db");dataSource.setUsername("root");dataSource.setPassword("2020");//提供模板,创建对象JdbcTemplate template=new JdbcTemplate(dataSource);//完成数据的增删改查template.update("insert into account values (null,?,?)","熊大","1000");}
}
3.运行,查看数据库
二、使用Spring框架来管理模板类
1.配置文件
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--配置连接池--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///spring_db"></property><property name="username" value="root"></property><property name="password" value="2020"></property></bean><!--配置jdbc的模板--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean></beans>
2.测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class JdbcTest2 {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void run1(){jdbcTemplate.update("insert into account values (null,?,?)","熊二","500");}
}
3.运行,查看数据库
三、Spring框架管理开源的连接池
1.配置开源的连接池
使用的是Durid开源的连接池,引入坐标如下:
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version>
</dependency>
2.将数据库连接的信息配置到属性文件中
db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_db
jdbc.username=root
jdbc.password=2020
3.核心配置
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--使用开源连接池<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///spring_db"></property><property name="username" value="root"></property><property name="password" value="2020"></property></bean>--><!--加载属性配置文件<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:db.properties"></property></bean>--><!--第二种写法:使用提供标签的方式--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--加载属性的文件--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--配置jdbc的模板--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean></beans>
四、Spring框架的JDBC模板的简单操作
1.测试类
增删改查代码
import com.qcby.pojo.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext.xml")
public class JdbcTest2 {@Autowiredprivate JdbcTemplate jdbcTemplate;/*增*/@Testpublic void run1(){int result=jdbcTemplate.update("insert into account values (null,?,?)","李四","700");System.out.println("受影响了"+result+"行");}/*删*/@Testpublic void run2(){int result=jdbcTemplate.update("delete from account where id=?",5);System.out.println("受影响了"+result+"行");}/*改*/@Testpublic void run3(){int result=jdbcTemplate.update("update account set name=?,money=? where id=?", "a", "500", 1);System.out.println("受影响了"+result+"行");}/*通过id查*/@Testpublic void run4(){Account account=jdbcTemplate.queryForObject("select * from account where id=?",new BeanMapper(),2);System.out.println(account);}/*查询所有数据*/@Testpublic void run5(){List<Account> accounts=jdbcTemplate.query("select * from account",new BeanMapper());for(Account account:accounts){System.out.println(account);}}}
2.实现类
/*实现类,用来进行数据封装*/
public class BeanMapper implements RowMapper<Account> {public Account mapRow(ResultSet rs, int rowNum) throws SQLException, SQLException {Account account=new Account();account.setId(rs.getInt("id"));account.setName(rs.getString("name"));account.setMoney(rs.getDouble("money"));return account;}
}
3.之前的数据库
4.运行
(1)run1()----增
数据库
(2)运行run2()----删
数据库
(3)运行run3()---改
数据库