Spring框架:第四篇

第一章: Spring 中的 JdbcTemplate

1.1-JdbcTemplate 概述

它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装。spring 框架为我们提供了很多 的操作模板类。

操作关系型数据的:

  • JdbcTemplate
  • HibernateTemplate

操作 nosql 数据库的:

  • RedisTemplate

操作消息队列的:

  • JmsTemplate

1.2-JdbcTemplate快速入门

Maven工程

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <!--spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <!--【spring-jdbc】-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <!--spring-tx-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <!--mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.26</version>
    </dependency>

  </dependencies>

数据库脚本

CREATE DATABASE  IF NOT EXISTS db1
USE db1
CREATE TABLE account(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(40),
money FLOAT
)CHARACTER SET utf8 COLLATE utf8_general_ci;
INSERT INTO account(NAME,money) VALUES('aaa',1000);
INSERT INTO account(NAME,money) VALUES('bbb',1000);

实体类

public class Account {
  private int id;
  private String name;
  private float money;

  public int getId() {
    return id;
  }

  public void setId(int 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 +
            '}';
  }
}

JdbcTemplate测试类

public class Demo01 {
  public static void main(String[] args) {
    // 【创建Spring提供的数据源对象】
    DriverManagerDataSource ds = new DriverManagerDataSource();
    // 设置驱动
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    // 设置连接
    ds.setUrl("jdbc:mysql://localhost:3306/db1");
    // 设置数据库登录用户名
    ds.setUsername("root");
    // 设置数据库登录密码
    ds.setPassword("root");

    // 【创建JdbcTemplate对象】
    JdbcTemplate jt = new JdbcTemplate();
    // 设置数据源
    jt.setDataSource(ds);
    // 向数据库中添加一条数据
    int row = jt.update("insert into account(name,money) values(?,?)","CCC",2000);
    System.out.println(row);
  }
}

1.3-Spring配置数据源及CRUD

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.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://localhost:3306/db1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置AccountDao-->
    <bean id="accountDao" class="cn.lpl666.dao.impl.AccountImpl">
        <property name="jt" ref="jdbcTemplate"></property>
    </bean>
</beans>

测试类

public class Demo02 {
  public static void main(String[] args) {
    // 创建容器对象
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    // 创建JdbcTemplate
    JdbcTemplate jt = ac.getBean("jdbcTemplate",JdbcTemplate.class);
    //【插入】
    // int row = jt.update("insert into account(name,money) values(?,?)","DDD",2000);
    // System.out.println(row);

    //【更新】
    // int row = jt.update("update account set name=? where id=?","ddd",4);
    // System.out.println(row);

    //【删除】
    // int row = jt.update("delete from account where id=?",4);
    // System.out.println(row);

    //【查询结果集-集合】
    // List<Account> list = jt.query("select * from account where money>?",new BeanPropertyRowMapper<Account>(Account.class),10);
    // System.out.println(list);

    //【查询结果-单个】
    // List<Account> list = jt.query("select * from account where id=?",new BeanPropertyRowMapper<Account>(Account.class),1);
    // System.out.println(list.get(0));

   //【查询单行单列】
   // Long aLong = jt.queryForObject("select count(*) from account where money>?", Long.class, 10);
   // System.out.println(aLong);
  }
}

1.4-Dao 继承 JdbcDaoSupport

JdbcDaoSupport

// JdbcDaoSupport 是 spring 框架为我们提供的一个类,该类中定义了一个 JdbcTemplate 对象,我们可以
直接获取使用,但是要想创建该对象,需要为其提供一个数据源:具体源码如下:
public abstract class JdbcDaoSupport extends DaoSupport {
//定义对象
private JdbcTemplate jdbcTemplate;
//set 方法注入数据源,判断是否注入了,注入了就创建 JdbcTemplate
public final void setDataSource(DataSource dataSource) {
    if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource())
    { //如果提供了数据源就创建 JdbcTemplate
    this.jdbcTemplate = createJdbcTemplate(dataSource);
    initTemplateConfig();
    }
}
//使用数据源创建 JdcbTemplate
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}
//当然,我们也可以通过注入 JdbcTemplate 对象
public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
    initTemplateConfig();
}
//使用 getJdbcTmeplate 方法获取操作模板对象
public final JdbcTemplate getJdbcTemplate() {
    return this.jdbcTemplate;
}

Dao层

接口

public interface IAccountDao {
  /**
   * 查询所有
   * @return
   */
  List<Account> findAll();

  /**
   * 查询一个
   * @param id
   * @return
   */
  Account findOne(int id);

  /**
   * 更新一个
   * @param account
   * @return
   */
  boolean update(Account account);
}

实现类

package cn.lpl666.dao.impl;
import cn.lpl666.dao.IAccountDao;
import cn.lpl666.domain.Account;

// import cn.lpl666.util.JdbcSupport;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;
//public class AccountImpl extends JdbcSupport implements IAccountDao
public class AccountImpl extends JdbcDaoSupport implements IAccountDao {
  @Override
  public List<Account> findAll() {
    List<Account> list = getJdbcTemplate().query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));
    return list;
  }

  @Override
  public Account findOne(int id) {
    List<Account> list = getJdbcTemplate().query("select * from account where id=?",new BeanPropertyRowMapper<Account>(Account.class),id);
    return list.isEmpty()?null:list.get(0);
  }

  @Override
  public boolean update(Account account) {
    int row = getJdbcTemplate().update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());
    return row>0;
  }
}

第二章:Spring中的事务控制

2.1-Spring 事务控制我们要明确的

第一:JavaEE 体系进行分层开发,事务处理位于业务层,Spring 提供了分层设计业务层的事务处理解决方 案。

第二:spring 框架为我们提供了一组事务控制的接口。具体在后面的第二小节介绍。这组接口是在 spring-tx-5.0.2.RELEASE.jar 中。

第三:spring 的事务控制都是基于 AOP 的,它既可以使用编程的方式实现,也可以使用配置的方式实现。本章描述的是使用配置的方式实现。

2.2-Spring 中事务控制的 API 介绍

PlatformTransactionManager

此接口是 spring 的事务管理器,它里面提供了我们常用的操作事务的方法,如下图:

我们在开发中都是使用它的实现类,如下图:

TransactionDefinition

它是事务的定义信息对象,里面有如下方法:

事务的隔离级别

事务的传播行为

赶超时间

默认值是-1,没有超时限制。如果有,以秒为单位进行设置。

是否是只读事务

建议查询时设置为只读

TransactionStatus

此接口提供的是事务具体的运行状态,方法介绍如下图:

2.3-基于 XML 的声明式事务控制(配置方式)

搭建Maven工程

<dependencies>
    <!--junit-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--spring-context-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <!--spring-tx-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <!--spring-jdbc-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <!--spring-test-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <!--mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.26</version>
    </dependency>
    <!--aspectjweaver-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.5</version>
    </dependency>
  </dependencies>

第二步:创建 spring 的配置文件并导入约束

此处需要导入 aop 和 tx 两个名称空间


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

第三步:Dao层接口和实现类

接口

public interface IAccountDao {
  /**
   * 查询一个
   * @param id
   * @return
   */
  Account findOne(int id);

  /**
   * 更新一个
   * @param account
   * @return
   */
  boolean update(Account account);
}

实现类

public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
  @Override
  public Account findOne(int id) {
    String sql = "SELECT * FROM account WHERE id = ?";
    List<Account> list =  getJdbcTemplate().query(sql,new BeanPropertyRowMapper<Account>(Account.class),id);
    return list==null?null:list.get(0);
  }

  @Override
  public boolean update(Account account) {
    String sql = "UPDATE account SET NAME=?,money=? WHERE id = ?";
    int row = getJdbcTemplate().update(sql, account.getName(), account.getMoney(), account.getId());
    return row>0;
  }
}

第四步:Service层接口和实现类

接口

public interface IAccountService {
  /**
   * 转账
   * @param oneId  转账用户id
   * @param twoId  收账用户id
   * @param money  转账钱数
   */
  void transfer(int oneId,int twoId,float money);
}

实现类

public class AccountSerice implements IAccountService {
  private IAccountDao dao = null;
  public void setDao(IAccountDao dao) {
    this.dao = dao;
  }

  @Override
  public void transfer(int oneId, int twoId, float money) {
    Account one = dao.findOne(oneId);
    Account two = dao.findOne(twoId);
    one.setMoney(one.getMoney()-money);
    two.setMoney(two.getMoney()+money);
    dao.update(one);
    //int i = 10/0;
    dao.update(two);

  }
}

第五步:Spring配置

<?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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/db1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--【配置AccountDao】-->
    <bean id="accountDao" class="cn.lpl666.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--【配置AccountService】-->
    <bean id="accountService" class="cn.lpl666.service.impl.AccountSerice">
        <property name="dao" ref="accountDao"></property>
    </bean>
    <!--【配置事务管理器】-->
    <bean id="transferManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--【配置事务通知】-->
    <tx:advice id="txAdivce" transaction-manager="transferManger">
        <!--配置事务属性-->
        <tx:attributes>
            <!--propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。-->
            <tx:method name="*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!--【配置AOP】-->
    <aop:config>
        <!--配置管理包的表达式-->
        <aop:pointcut id="servicePath" expression="execution(* cn.lpl666.service.impl.*.*(..))"></aop:pointcut>
        <!--建立切入点表达式和事务通知的对应关系-->
        <aop:advisor advice-ref="txAdivce" pointcut-ref="servicePath"></aop:advisor>
    </aop:config>
</beans>

在 tx:advice 标签内部 配置事务的属性

  • read-only:是否是只读事务。默认 false,不只读。
  • isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。
  • propagation:指定事务的传播行为。
  • timeout:指定超时时间。默认值为:-1。永不超时。
  • rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。 没有默认值,任何异常都回滚。
  • no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回 滚。没有默认值,任何异常都回滚。

2.4-基于注解的事务控制方式1

Dao层实现类

@Repository("accountDao")
public class AccountDaoImpl  implements IAccountDao {
  @Autowired
  @Qualifier("jdbcTemplate")
  private JdbcTemplate jdbcTemplate = null;
  @Override
  public Account findOne(int id) {
    String sql = "SELECT * FROM account WHERE id = ?";
    List<Account> list =  jdbcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class),id);
    return list==null?null:list.get(0);
  }

  @Override
  public boolean update(Account account) {
    String sql = "UPDATE account SET NAME=?,money=? WHERE id = ?";
    int row = jdbcTemplate.update(sql, account.getName(), account.getMoney(), account.getId());
    return row>0;
  }
}

服务层实现类

Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class AccountService implements IAccountService {
  @Autowired
  @Qualifier("accountDao")
  private IAccountDao dao = null;

  @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
  @Override
  public void transfer(int oneId, int twoId, float money) {
    Account one = dao.findOne(oneId);
    Account two = dao.findOne(twoId);
    one.setMoney(one.getMoney()-money);
    two.setMoney(two.getMoney()+money);
    dao.update(one);
    // int i = 10/0;
    dao.update(two);

  }
}

配置文件

<?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"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 告知 spring 创建容器时要扫描的包 -->
    <context:component-scan base-package="cn.lpl666"/>
    <!--配置数据源-->
    <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/db1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--配置jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事务管理器-->
    <bean id="transferManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 开启spring对注解事务的支持-->
    <tx:annotation-driven transaction-manager="transferManger"></tx:annotation-driven>

</beans>

2.5-基于注解的事务控制方式2

纯注解

Dao层实现类

@Repository("accountDao")
public class AccountDaoImpl  implements IAccountDao {
  @Autowired
  @Qualifier("jdbcTemplate")
  private JdbcTemplate jdbcTemplate = null;
  @Override
  public Account findOne(int id) {
    String sql = "SELECT * FROM account WHERE id = ?";
    List<Account> list =  jdbcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class),id);
    return list==null?null:list.get(0);
  }

  @Override
  public boolean update(Account account) {
    String sql = "UPDATE account SET NAME=?,money=? WHERE id = ?";
    int row = jdbcTemplate.update(sql, account.getName(), account.getMoney(), account.getId());
    return row>0;
  }
}

服务层实现类

@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class AccountService implements IAccountService {
  @Autowired
  @Qualifier("accountDao")
  private IAccountDao dao = null;

  @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
  @Override
  public void transfer(int oneId, int twoId, float money) {
    Account one = dao.findOne(oneId);
    Account two = dao.findOne(twoId);
    one.setMoney(one.getMoney()-money);
    two.setMoney(two.getMoney()+money);
    dao.update(one);
    int i = 10/0;
    dao.update(two);

  }
}

JDBC配置类

package cn.lpl666.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
  @Value("${jdbc.driver}")
  private String driver;
  @Value("${jdbc.url}")
  private String url;
  @Value("${jdbc.user}")
  private String username;
  @Value("${jdbc.password}")
  private String password;

  /**
   * 创建QueryRunner对象
   * @param ds
   * @return
   */
  @Bean(name="jdbcTemplate")
  public JdbcTemplate createJdbcTemplate(@Qualifier("dataSource") DataSource ds){
    return new JdbcTemplate(ds);
  }

  /**
   * 创建数据源对象
   * @return
   */
  @Bean(name = "dataSource")
  public DataSource createDataSource() {
     DriverManagerDataSource dm = new DriverManagerDataSource();
     dm.setDriverClassName(driver);
     dm.setUrl(url);
     dm.setUsername(username);
     dm.setPassword(password);
     return dm;
  }
}

JDBC配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1
jdbc.user=root
jdbc.password=root

事务管理配置类

public class TransactionConfig {
  @Bean(name="transactionManager")
  public PlatformTransactionManager createTransactionManager(@Qualifier("dataSource") DataSource dataSource){
    return new DataSourceTransactionManager(dataSource);
  }

}

Spring配置类

@Configuration
@ComponentScan("cn.lpl666")  // spring容器要扫描的包
@Import({JdbcConfig.class,TransactionConfig.class})  // 导入其他配置类
@EnableTransactionManagement
public class SpringConfiguration {
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountTest2 {
  @Autowired
  @Qualifier("accountService")
  private IAccountService service = null;
  @Test
  public void transfer(){
    service.transfer(1,2,100);
  }
}

猜你喜欢

转载自www.cnblogs.com/lpl666/p/12352196.html