Spring中的JdbcTemplate
JdbcTemplate:他是spring框架中提供的一个对象,是对原始Jdbc API对象的简单封装。
JdbcTemplate的作用:用于和数据库交互的,实现对表的CRUD操作
JdbcTemplate的入门
导入相关的坐标
<packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.3.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> </dependencies>
账户实体类的建立
package com.itheima.domain; import java.io.Serializable; //账户的实体类 public class Account implements Serializable { private Integer id; private String name; private Float money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Float getMoney() { return money; } public void setMoney(Float money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
操作数据库
package com.itheima.jdbctemplate; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; //JdbcTemplate的最基本用法 public class JdbcTemplateDemo1 { public static void main(String[] args) { //准备数据源:spring内置数据源 DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/eesy"); ds.setUsername("root"); ds.setPassword("12345"); //创建JdbcTemplate对象 JdbcTemplate jt = new JdbcTemplate(); //给jt设置数据源 jt.setDataSource(ds); //执行操作 jt.execute("insert into account(name,money)values('ccc',1000)"); } }

JdbcTemplate在spring的ioc中使用
配置bean
<?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.xsd"> <!--配置JdbcTemplate--> <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property> <property name="username" value="root"></property> <property name="password" value="12345"></property> </bean> </beans>
测试是否可以操作数据库
报错:
NoSuchBeanDefinitionException: No bean named 'jdbcTemplate' is defined
原因:xxxx没有定义,区分大小写:

package com.itheima.jdbctemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; //JdbcTemplate的最基本用法 public class JdbcTemplateDemo1 { public static void main(String[] args) { //获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //获取对象 JdbcTemplate jt = ac.getBean("JdbcTemplate",JdbcTemplate.class); //执行操作 jt.execute("insert into account(name,money)values('ddd',13240)"); } }
spring中基于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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置业务层--> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置账户的持久层--> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property> <property name="username" value="root"></property> <property name="password" value="12345"></property> </bean> <!--spring中基于xml的声明事务控制步骤步骤--> <!--1.配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--2.配置事务的通知: 需要导入事务的约束 属性id:给事务通知一个唯一标识 属性transaction-manager:给事务通知提供一个事务管理器引用 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--配置事务的属性 isolation:用于指定事务的隔离级别,默认值是DEFAULT,表示使用数据库的默认隔离级别 propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS read-only:用于指定事务是否只读。只有查询方法才能设置true.默认值是false.表示只读 timeout:用于指定事务的超时时间,默认值-1.表示永不超时。如果指定了数值,以秒为单位 rollback-for:用于指定一个异常,当产生该异常是,事务回滚,产生其他异常是,事务部回滚,没有默认值,表示任何异常都回滚 no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚,没有默认值。表示任何异常都回滚 --> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method> </tx:attributes> </tx:advice> <!--3.配置AOP中的通用切入点表达式 --> <aop:config> <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut> <!--4.建立事务通知和切入点表达式的对应关系--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor> </aop:config> </beans>
来源:https://www.cnblogs.com/train99999/p/11219654.html