spring_day04 (JDBCTemplate implement CRUD, transaction control in the spring)

A .JDBCTemplat achieve CRUD

1.jar 包

 spring-jdbc-5.0.2.RELEASE.jar

 spring-tx-5.0.2.RELEASE.jar (and it is related to the transaction)

2.xml constraint file

 

 
 
<?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">

 Configuration data source

* C3po

*DBCP

* Spring built-in data source

<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_day02"></property>
    <property name="username" value="root"></property>
    <property name="password" value="1234"></property>
</bean> 

 jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///spring_day02

jdbc.username=root

jdbc.password=123

 

<!-- 引入外部属性文件: -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  
    <property name="location" value="classpath:jdbc.properties"/>
</bean> 

 

 4. Configuration JDBCTemplate

<! - operate a database of template configuration: the JdbcTemplate -> 
< bean the above mentioned id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > 
    < Property name = "dataSource" ref = "dataSource" > < / Property > 
</ the bean > 

 

 5. Write operations dao implementation class code, and of IOC

Two ways:

* First definition JdbcTemplate manner Dao class for all configurations (xml notes and can be). Jdbctemplate dao defined in the implementation class, and generates a set () method to achieve injection jdbctemplate

* The second let Dao inherited JdbcDaoSupport way, only for XML-based way, not take notes (no way to change jdbcDaoSupport source category). jabdDaoSupport spring is a custom class, there setJdbcTemplate () method, DAO implementation class inherit the class, so that the method has, not explicitly declared, may also be implemented DI

The transaction control two .spring

1. in two ways:

* Based on the configuration of the control things

* Based on encoded transaction control

* Are based on AOP

2.PlatformTransactionManager Interface:

Three methods

*void commit(TransactionStatus status);

*void rollback(TransactionStatus status);

*TransactionStatus getTransaction(TransactionDefinition definition);

3. The interface implementation class

org.springframework.jdbc.datasource.DataSourceTransactionManager

4.TransactionDefinition:

Five methods

5.TransactionStatus

Six operations

III. XML-based declarative transaction control (configuration)

1.jar 包

2.xml constraint: aop, tx namespace

3.

IV. Annotation-based declarative transaction control (configuration)

 

Guess you like

Origin www.cnblogs.com/counter-biao/p/11614096.html