目录
他是spring框架中提供的一个对象,是对原始繁琐的jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和HivernateTenplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等
数据库中account表
pom.xml中
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
测试java中实体类中
- package test;
-
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- import org.junit.Test;
- import org.springframework.jdbc.core.JdbcTemplate;
-
- import java.beans.PropertyVetoException;
-
- public class JdbcTemplateTest {
-
- @Test
- //测试JdbcTemplate开发步骤
- public void test1() throws PropertyVetoException {
- //创建数据源对象
- ComboPooledDataSource dataSource=new ComboPooledDataSource();
- dataSource.setDriverClass("com.mysql.jdbc.Driver");
- //位置为本地,3306端口,test数据库
- dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
- //用户名
- dataSource.setUser("root");
- //用户密码
- dataSource.setPassword("123456");
- JdbcTemplate jdbcTemplate=new JdbcTemplate();
- //设置数据源对象,知道数据库在哪里
- jdbcTemplate.setDataSource(dataSource);
- //执行操作
- int row = jdbcTemplate.update("insert into account value(?,?)", "tom", 5000);
- System.out.println(row);
- }
-
-
- }
domain包下的account类下
- package domain;
-
- public class Account {
- private String name;
- private double money;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public double getMoney() {
- return money;
- }
-
- public void setMoney(double money) {
- this.money = money;
- }
-
- @Override
- public String toString() {
- return "Account{" +
- "name='" + name + '\'' +
- ", money=" + money +
- '}';
- }
- }
数据库中的test数据库,account表
仔细看,这些都是参数注入(用了setXxx。。),我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DateSource注入到JdbcTemplate模版对象中,配置如下:
在test测试包下创建一系列
在applicationContext1.xml下
- <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.xsd">
-
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
- <property name="user" value="root"/>
- <property name="password" value="123456"/>
- bean>
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"/>
- bean>
- beans>
pom.xml中
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>javaMaven02artifactId>
- <version>1.0-SNAPSHOTversion>
- <dependencies>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.32version>
- dependency>
- <dependency>
- <groupId>c3p0groupId>
- <artifactId>c3p0artifactId>
- <version>0.9.1.2version>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.1.10version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.11version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
-
-
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.0.5.RELEASEversion>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-coreartifactId>
- <version>2.9.0version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.9.0version>
- dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-annotationsartifactId>
- <version>2.9.0version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
-
-
- dependencies>
-
-
- project>
JdbcTemplateTest类下
-
- public class JdbcTemplateTest {
- @Test
- //测试Spring产生jdbcTemplate对象
- public void test2() throws PropertyVetoException {
- ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext1.xml");
- JdbcTemplate jdbcTemplate=app.getBean(JdbcTemplate.class);
- int row = jdbcTemplate.update("insert into account value(?,?)", "KC", 50000);
- System.out.println(row);
-
- }
- }
运行结果:
数据库中的变化