SSM学习——Spring学习

一、Spring框架的概述以及spring中基于XML的IOC配置

1.spring的概述(了解)

  • 1.1、spring是什么
    spring是分层的JavaSE/EE 应用full-stack轻量级开源框架,以IoC(Inverse Of Control:控制反转)和AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层Spring MVC 和持久层的Spring JDBC以及业务事务管理等众多的企业及应用技术,还能整合众多的第三方框架和类库。
  • 1.2、spring的两大核心
    IoC(Inverse Of Control:控制反转)和AOP(Aspect Oriented Programming:面向切面编程)
  • 1.3、spring的发展历程
    EJB到spring的转化
  • 1.4、spring的体系结构
  • 在这里插入图片描述
    可以看出无论是使用JDBC,Servlet等Web组件,AOP或者测试的组件都必须要引入核心容器库!

2.程序的耦合和解耦

  • 2.1、案例中的问题分析
/**
 * 演示程序耦合问题
 *      程序耦合:
 *              耦合:程序间的依赖关系
 *                  包括:
 *                      类之间的依赖
 *                      方法之间的依赖
 *               解耦:
 *                  降低程序间的依赖关系
 *                实际开发中:
 *                    应该做到编译期不依赖,运行时可以依赖
 *               解耦的思路:
 *                      第一步:使用反射文件来创建对象,避免直接使用new关键字
 *                      第二步:通过读取配置文件的方式来获取要创建的对象的全限定类名
 **/
public class JdbcDemo1 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //1、注册驱动
//        DriverManager.registerDriver(new com.mysql.jdbc.Driver());//如果使用这个方法,在编译的过程才会出现错误,且依赖包,耦合性高
        Class.forName("com.mysql.jdbc.Driver");
        //2、获取连接
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/springTest","root","root");
        //3、获取操作数据库的预处理对象
        PreparedStatement preparedStatement = con.prepareStatement("select * from account");
        //4、执行SQL,得到结果集
        ResultSet rs = preparedStatement.executeQuery();
        //6、释放资源
        while(rs.next()){
            System.out.println(rs.getString("name"));
        }
        rs.close();
        preparedStatement.close();
        con.close();
    }
}
  • 2.2 、工厂模式解耦
    要获取bean对象都需要通过这个工厂对象来获取,不能够自己单独地创建。
public class BeanFactory {

    //创建一个properties对象
    private static Properties props;
    //创建一个Map用于存放key值和对应的实体对象
    private static Map<String,Object> beans;

    //在静态代码块中完成容器的初始化和赋值
    static{
        props = new Properties();
        //将配置文件读进输入流中
        InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
        //初始化容器对象
        beans = new HashMap<String,Object>();
        try {
            //获取配置文件中的内容
            props.load(in);
            //获取配置文件中的所有keys
            Enumeration<Object> keys = props.keys();
            while(keys.hasMoreElements()){
                //循环遍历keys,获取其中的实体类的全限定路径名
                String key = keys.nextElement().toString();
                //获取对应的全限定名
                String beanPath = props.getProperty(key);
                //根据全限定名,反射创建实体类
                Object bean = Class.forName(beanPath).newInstance();
                //将key和value值放到map容器中
                beans.put(key,bean);
            }
        }catch (Exception e){
            throw new ExceptionInInitializerError("初始化异常");
        }
    }

    public static Object getBean(String beanName){
        return beans.get(beanName);
    }
}

通过工厂对象获取实例,相当于把原来直接new出来一个bean对象的权力完全交给工厂去完成,所以通过工厂对象获取实例对象,也叫控制反转(IOC)。

3.IOC概念和Spring中的IOC

通过工厂对象获取实例,相当于把原来直接new出来一个bean对象的权力完全交给工厂去完成,所以通过工厂对象获取实例对象,也叫控制反转(IOC)。
官方解释:控制反转(IOC),把创建对象的权力交给框架,包括依赖注入(Dependency Injection,DI)和一来查找(Dependency Lookup)。
IOC的作用:削减程序间的耦合关系(接触我们代码中的依赖关系)

  • spring中的IOC:其实根本就不能实现数据库的增删改查,也不能实现前端数据的接收,修改等操作。但是他能够解决依赖关系,也只能解决程序间的依赖关系!
  • 创建一个maven工程说明spring的使用:
    1) 首先在pom.xml中引入架包依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>con.xxx</groupId>
    <artifactId>spring_day01_springXMLCreate</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

2) 在java包里边创建一个service包,一个dao包,service中存放一个接口和一个impl包:
AccountService.interface:

public interface AccountService {
    /**
     * 模拟创建一个保存用户的接口
     */
    void saveAccount();
}

AccountServiceImpl.java:

public class AccountServiceImp implements AccountService {
    public AccountServiceImp(){
        System.out.println("对象创建了");
    }
    private AccountDao accountDao = new AccountDaoImp();
    //模拟实现业务层的接口方法
    public void saveAccount(){
        accountDao.saveAccount();
    }

}

AccountDao.interface:

public interface AccountDao {
    void saveAccount();
}

AccountDaoImpl.java:

public class AccountDaoImp implements AccountDao {
  	//模拟实现AccountDao的方法
    public void saveAccount(){
        System.out.println("保存了一个用户");
    }
}

3) 在资源包的根路径下创建:bean.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--获取service对象-->
    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImp"></bean>
    <!-- 获取dao对象 -->
    <bean id="accountDao" class="com.xxx.dao.impl.AccountDaoImp"></bean>
</beans>

4) 创建一个测试类:

package com.xxx.ui;

import com.xxx.dao.AccountDao;
import com.xxx.service.AccountService;
import com.xxx.service.impl.AccountServiceImp;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 获取spring容器的IOC核心容器,并根据id获取对象
 *
 * ApplicationContext 的三个常用实现类:
 *      ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话,加载不了
 *      FileSystemXmlApplicationContext:他可以访问任意路径下的资源(但是必须取得权限)(不常用)
 *      AnnotationConfigApplicationContext:他是用于读取注解创建容器的
 *
 * 核心容器的两个接口引发出的问题:
 * ApplicationContext:  单例对象使用         这才是常用的
 *      他在构建核心容器是,创建对象才去的策略是立即加载的方式。
 *      也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象
 * BeanFactory:         多例对象
 *      他在创建核心容器的时候,创建对象采用的策略是采用延迟加载的方式,
 *      也就是说,什么时候根据id获取对象了,什么时候才真正地开始创建
 **/
public class ServletUi {

    //刚刚说了要尽量避免使用这种new的方法来创建一个对象的,这里这么做显然是不对的,毕竟耦合性高
    //如果这时候删除掉AccountServiceImp,那么程序在编译的时候就会报错
//    private static AccountService accountService = new AccountServiceImp();

    //获取spring配置文件信息,使用核心类,在根路径下的xml文件不需要另外配置
    private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");



    public static void main(String[] args) {
        //使用spring核心容器取对象
//        AccountService accountService = (AccountService)applicationContext.getBean("accountService");
//        //获取bean对象有两种方式,可以使用类的字节码对象来获取
//        AccountDao accountDao = applicationContext.getBean("accountDao",AccountDao.class);
//        System.out.println(accountService);
//        System.out.println(accountDao);
//        accountService.saveAccount();

        //-------------BeanFactory-----------------
        Resource resource = new ClassPathResource("bean.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        //测试使用BeanFactory来创建对象的时候,只有在调用了id的时候才创建此对象(延迟加载)
        AccountService accountService = (AccountService) factory.getBean("accountService");
        System.out.println(accountService);
    }
}

4.依赖注入(Dependency Injection)DI

依赖注入更多说的是一种spring为类对象或某成员进行赋值的意思。
1)spring对bean的管理细节(创建bean的三种方式,管理bean的生命周期,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">
    <!--
        把对象交给spring管理
        spring对bean的管理细节
            1.创建bean的三种方式
            2.bean对象的作用范围
            3.bean对象的生命周期
    -->
    <!-- 创建bean有三种方式
            第一种:使用默认构造函数创建:
                    在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签的时候,
                    采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建

    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImp"></bean>
    -->
    <!-- 第二种:获取工厂对象中的方法返回的对象(使用工厂中的方法创建对象,并把对象存入到spring容器中) -->
<!--    <bean id="beanFactory" class="com.xxx.factory.InstanceFactory"></bean>-->
<!--    <bean id="accountService" factory-bean="beanFactory" factory-method="getBean"></bean>-->
    <!-- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器中) -->
<!--    <bean id="accountService" class="com.xxx.factory.StaticFactory" factory-method="getBean"></bean>-->

    <!-- bean 的作用范围:
            bean标签的scope属性:
            作用:指定bean的作用范围
            取值:常用的就是单例和多例模式
                singleton:单例的(默认的)
                prototype:多例的
                request:作用于web应用的一次请求范围、
                session:作用于web应用的回话范围
                global-session:作用于集群环境的回话范围(全局回话范围),当不是集群环境的时候,他就是session
                -->
<!--    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImp" scope="singleton"></bean>-->
<!--    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImp" scope="prototype"></bean>-->

    <!-- bean对象的生命周期
            单例对象:
                    出生:当spring容器创建的时候就出生
                    活着:只要容器还在就还活着
                    死亡:容器对象那个销毁了就销毁
                    总结:单例对象的生命周期和容器周期相同
            多例对象:
                    出生:当我们创建对象时spring框架结尾我们创建
                    活着:对象只要在使用的过程中就一直活着
                    死亡:当对象长时间不用,且没有其他对象饮用的时候,由java垃圾回收期回收
     -->
    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImp"
        init-method="init" destroy-method="destroy"
    ></bean>
</beans>

2)依赖注入:

<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!--
        spring 中的依赖注入
         依赖注入:
            Dependency Injection
          IOC的作用:
            降低程序间耦合(依赖关系)
          依赖关系管理:
            以后都要交给spring来维护
            在当前的类需要用到其他的类对象,由spring为我们提供,我们只需要在配置文件中说明
           依赖关系维护:
                    就称之为依赖注入
            依赖注入:
                    能注入的数据有三种:
                        基本类型和String
                        其他的bean类型(在配置文件中或者注解配置过得bean)
                        复杂类型/集合类型
                     注入的方式:有三种
                        第一种:使用构造函数
                        第二种:使用set方法提供
                        第三种:使用注解的方式提供
     -->
    <!-- 构造函数注入:
            使用标签:constructor-arg
            标签出现的位置:bean标签的内部
            标签中的属性:
                    type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中的某个或某些参数的类型
                    index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值,索引的位置就是从0开始的
                    name:用于指定给构造函数中指定名称的参数赋值    这个是最常用的
                    =================  以上的参数都是指定给构造函数中的那个参数赋值的 ========================
                    value:用于提供基本类型和String类型的数据
                    ref:用于指定其他的bean类型数据,他指的是在spring的IOC核心容器中出现过的bean对象
            优势:
                    在获取bean对象的时候,注入数据是必须的,否则无法创建对象成功
            弊端:
                    改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据也得提供。 !!!!因此构造函数注入的方式并不常用!!!
     -->
    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImp">
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="name" value="liming"></constructor-arg>
<!--        <constructor-arg name="birthday" value="2019-01-02"></constructor-arg>-->
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>
    <!-- 配置一个日期对象 -->
    <bean id="now" class="java.util.Date"></bean>

    <!-- set方法注入                    !!!!!常用!!!!
                涉及的标签:property
                出现的位置:bean标签的内部
                标签的属性:
                    name:用于指定注入时所调用的set方法的名称
                    value:用于提供基本类型和String类型的数据
                    ref:用于指定其他的bean类型的数据。他指的是在spring的Ioc核心容器中出现过的bean对象
                优势:
                    创建对象时没有明确的限制,可以直接使用默认构造函数
                弊端:
                    如果有某个成员必须有值,则获取对象是有可能set方法没有执行
    -->
    <bean id="accountService2" class="com.xxx.service.impl.AccountServiceImp2">
        <property name="age" value="18"></property>
        <property name="name" value="黎明"></property>
        <property name="birthday" ref="now"></property>
    </bean>


    <!-- 复杂类型的注入/集合类型的注入
            用于给List结构的集合注入的标签:
                list,set,array(就是说这三个标签的可以互相交换使用,都是一样的效果)
            用于给Map结构的集合注入的标签:
                map,props
     -->
    <bean id="accountService3" class="com.xxx.service.impl.AccountServiceImp3">
        <property name="myStrs">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="myList">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </array>
        </property>
        <property name="mySet">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </array>
        </property>
        <property name="myMap">
            <map>
                <entry key="testA" value="aaa"></entry>
                <entry key="testB">
                    <value>bbb</value>
                </entry>
            </map>
        </property>
        <property name="myProps">
            <map>
                <entry key="testC">
                    <value>ccc</value>
                </entry>
                <entry key="testD" value="ddd"></entry>
            </map>
        </property>
    </bean>
</beans>

写在xml中的bean,在java代码中需要使用的话就直接通过全局的配置来获取就可以直接使用了。bean通过id获取:

public class ServletUi {
    public static void main(String[] args) {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = (AccountService) applicationContext.getBean("accountService3");
        accountService.saveAccount();
//        System.out.println(accountService == accountService2);//单例的时候为true,多例的时候为false
        applicationContext.close();//容器关闭的时候,bean对象跟着销毁
    }
}

二、spring中基于注解的IOC和ioc案例

1.spring 中的ioc常用注解

1)在工程中使用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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.xxx"></context:component-scan>
</beans>

2)在需要使用的实体上或者方法等,添加@xxx注解:

package com.xxx.service.impl;

import com.xxx.dao.AccountDao;
import com.xxx.dao.impl.AccountDaoImp;
import com.xxx.factory.BeanFactory;
import com.xxx.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @Author: zhou
 * @Date: 2019/11/22
 * 曾经的xml配置:
 * <bean id="accountService" class="com.xxx.service.impl.AccountServiceImp" scope="" init-method="" destroy-method="">
 *     <property name="" value=""|ref=""></property>
 *     </bean>
 *
 * 注解类型包括:
 * 用于创建对象的
 *          他们的作用就和在xml配置文件中编写一个<bean>标签实现的功能是一样的
 *          @Component:
 *                  作用:用于把当前的对象存入spring容器中
 *                  属性:
 *                      value:用于指定bean的id,当我们不写时,他的默认值是当前的类名,且首字母改小写
 *          @Controller:一般用在表现层
 *          @Service:一般用于业务层
 *          @Repository:一般用于持久层
 * 用于注入数据的
 *          他们的作用就和在xml配置文件中写property标签的作用是一样的
 *          AutoWired:
 *              作用:自动按照类型注入,只要容器中有唯一的一个bean对象和要注入的变量类型匹配,就可以注入成功
 *              出项位置:
 *                      可以是在变量上,也可以是在方法上
 *              细节:在使用注解注入的时候,set方法就不是必须的了
 *           Qualifier:
 *                作用:在按照类中注入的基础之上再按照名称注入。他在类成员注入时不能单独使用。但是在给方法参数注入的时候可以
 *                属性:
 *                      value:用于指定注入bean中的id
 *            Resource:
 *              作用:直接按照bean的id注入。他可以独立使用
 *              属性:
 *                  name:用于指定bean的id
 *            以上三个注解都只能注入其他bean类型的数据,而基本类型和String类型都无法使用上述注解实现
 *            另外,集合类型的注入都必须依赖于xml配置文件实现
 *
 *              @Value:
 *                  作用:用于注入基本类型和String类型的数据
 *                  属性:
 *                      value:用于指定数据的值。他可以使用spring中的SpEL(也就是spring中的el表达式)
 *                              SpEl的写法:${表达式}
 * 用于改变作用范围的
 *          他们的作用就和在xml配置文件中写scope的作用是一样的
 *          Scope:
 *              作用:用于指定bean的作用范围
 *              属性:
 *                  value:指定范围取值。常用的取值:singleton,prototype
 * 和生命周期相关的
 *          他们的作用就和在xml配置文件中的写init-method和destroy-method的标签方法是一样的
 *          PreDestroy:用于指定销毁的方法
 *          PreConstruct:用于指定初始化方法
 *
 **/
//如果注解中只有value,可以不写value这个关键字
//@Component(value = "accountService")
@Component
@Scope
public class AccountServiceImp implements AccountService {

    public AccountServiceImp(){
        System.out.println("service对象创建了");
    }

//    private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
    @Autowired
//    @Qualifier("accountDaoIpm1")
    private AccountDao accountDaoIpm1 = null;

    //模拟实现业务层的接口方法
    public void saveAccount(){
//        applicationContext.getBean("accountService");
        accountDaoIpm1.saveAccount();
    }
}
  1. 在测试类中使用spring中已经注解好的实体类:
public class ServletUi {

    public static void main(String[] args) {
           ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
           AccountService accountService = (AccountService)applicationContext.getBean("accountServiceImp");
           AccountDao accountDao = applicationContext.getBean("accountDaoIpm1",AccountDao.class);
           accountService.saveAccount();
           System.out.println(accountService);
           System.out.println(accountDao);
    }
}

2.案例使用xml方式和注解方式实现单表的CRUD操作(持久层技术使用dbUtils)

  1. 先对pom.xml文件进行配置,加入数据库和单元测试的各种支持:
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.xxx</groupId>
    <artifactId>spring_day02_XMLioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2)创建一个Account实体类:

public class Account {
    private Integer id;
    private String name;
    private Float money;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + 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;
    }
}

3)创建实体类的业务层接口及其实现类:
AccountService接口:

public interface AccountService {

    /**
     * 实现查询所有操作
     */
     List<Account> findAll();

    /**
     * 实现查询单个用户操作
     */
    Account findOneById(Integer id);

    /**
     * 实现增加一个用户操作
     */
    void saveAccount(Account account);

    /**
     * 实现更新一个用户操作
     */
    void updateAccount(Account account);

    /**
     * 实现删除一个用户操作
     */
    void deleteById(Integer id);
}

AccountServiceImpl实现类:

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public List<Account> findAll() {
        return accountDao.findAll();
    }

    public Account findOneById(Integer id) {
        return accountDao.findOneById(id);
    }

    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    public void deleteById(Integer id) {
        accountDao.deleteById(id);
    }
}

3)创建实体类持久层的接口及其实现类:
AccountDao:

public interface AccountDao {

    /**
     * 实现查询所有操作
     */
    List<Account> findAll();

    /**
     * 实现查询单个用户操作
     */
    Account findOneById(Integer id);

    /**
     * 实现增加一个用户操作
     */
    void saveAccount(Account account);

    /**
     * 实现更新一个用户操作
     */
    void updateAccount(Account account);

    /**
     * 实现删除一个用户操作
     */
    void deleteById(Integer id);

}

AccountDaoImp实现类:

public class AccountDaoImpl implements AccountDao {

    private QueryRunner qr;

    public void setQr(QueryRunner qr) {
        this.qr = qr;
    }

    public List<Account> findAll() {
        try{
            return qr.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public Account findOneById(Integer id) {
        try{
            return qr.query("select * from account where id=?",new BeanHandler<Account>(Account.class),id);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public void saveAccount(Account account) {
        try{
            qr.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void updateAccount(Account account) {
        try{
            qr.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void deleteById(Integer id) {
        try{
            qr.update("delete from account where id=?",id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

4)添加xml文件,实现spring容器,把需要配置的内容在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 注入配置 -->
    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImpl">
        <!-- 配置注入的dao -->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!-- 配置dao对象 -->
    <bean id="accountDao" class="com.xxx.dao.impl.AccountDaoImpl">
       <property name="qr" ref="runner"></property>
    </bean>

    <!-- 配置QueryRunner -->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
        <!-- 注入数据源 -->
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接数据库的必备信息 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springtest"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

</beans>

5)编写测试的实现类:

public class SpringJdbcTest {

    @Test
    public void testFindAll(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        List<Account> all = accountService.findAll();
        for(Account account : all){
            System.out.println(account);
        }
    }

    @Test
    public void testFindOne(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        Account account = accountService.findOneById(1);
        System.out.println(account);
    }

    @Test
    public void testSave(){
        Account account = new Account();
//        account.setId(123);
        account.setName("test");
        account.setMoney(123.0f);
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        accountService.saveAccount(account);
    }

    @Test
    public void testUpdate(){
        Account account = new Account();
        account.setId(4);
        account.setName("test222");
        account.setMoney(123.0f);
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        accountService.updateAccount(account);
    }

    @Test
    public void testDelete(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService accountService = ac.getBean("accountService", AccountService.class);
        accountService.deleteById(5);
    }
}

3.改造基于注解的ioc案例,使用纯注解的方式实现(spring的一些新注解的使用)

主要是多了SpringConfig.java和JDBCConfig.java,然后不要bean.xml的配置文件,新增一个jdbcConfig.properties的配置文件,并且相关的需要注入的实体或者方法上加上注解,业务层的加@Component或者@Service,持久层的添加@Repository等,然后在测试类中使用新的读取配置方式new AnnotationConfigApplicationContext(SpringConfig.class);
SpringConfig:

/**
 * 该类是一个配置类,它的作用和bean.xml是一样的
 * spring中的新注解
 * Configuration
 *     作用:指定当前类是一个配置类
 *     细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
 * ComponentScan
 *      作用:用于通过注解指定spring在创建容器时要扫描的包
 *      属性:
 *          value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
 *                 我们使用此注解就等同于在xml中配置了:
 *                      <context:component-scan base-package="com.itheima"></context:component-scan>
 *  Bean
 *      作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
 *      属性:
 *          name:用于指定bean的id。当不写时,默认值是当前方法的名称
 *      细节:
 *          当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
 *          查找的方式和Autowired注解的作用是一样的
 *  Import
 *      作用:用于导入其他的配置类
 *      属性:
 *          value:用于指定其他配置类的字节码。
 *                  当我们使用Import的注解之后,有Import注解的类就父配置类,而导入的都是子配置类
 *  PropertySource
 *      作用:用于指定properties文件的位置
 *      属性:
 *          value:指定文件的名称和路径。
 *                  关键字:classpath,表示类路径下
 */
//@Configurable //有时候可以不写的。。。
@Configurable
@ComponentScan("com.xxx")
@Import(JDBCConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfig {
}

JDBCConfig:

public class JDBCConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }

    @Bean(name="dataSource")
    public DataSource createDataSource(){
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass(driver);
            dataSource.setJdbcUrl(url);
            dataSource.setUser(username);
            dataSource.setPassword(password);
            return dataSource;
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }
}

jdbcConfig.properties:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/springtest
jdbc.username = root
jdbc.password = root

4.spring与Junit整合

主要就是测试使用,spring集合一个强大的单元测试工具。

三、spring中的aop和基于XML以及注解的AOP配置

1.分析一个案例,回顾动态代理的相关技术:

传统的动态代理案例:

public class Client {
    public static void main(String[] args) {
        final Producer producer = new Producer();
        /**
         * 动态代理:
         * 特点:字节码随用随创建,随用随加载
         * 作用:不修改源码的基础上对方法进行加强
         * 分类:
         *      基于接口的动态代理
         *      基于子类的动态代理
         *  基于接口的动态代理:
         *      涉及的类:Proxy
         *      提供者:JDK官方
         *  如何创建代理对象:
         *      使用Proxy类中的newProxyInstance方法
         *  创建代理对象的要求:
         *      被代理对象至少实现一个接口,如果没有则不能使用
         *   newProxyInstance 方法参数:
         *      ClassLoader:类加载器
         *          他是用于加载代理对象字节码的。和被代理对象使用相同的类加载器。固定写法。
         *      Class[]:字节码数组
         *          他是用于让代理对象和被代理对象有相同的方法。固定写法
         *      InvocationHandler:用于提供增强的代码
         *          他是让我们写入和代理。我们一般都是写一个接口的实现类,通常情况下都是匿名内部类,但不是必须的
         *          此接口是谁用谁写
         */
        IProducer iProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),
                producer.getClass().getInterfaces(),
                new InvocationHandler() {
                    /**
                     * 作用:执行被代理对象的任何借口都会经过该方法
                     * 方法参数的含义:
                     * @param proxy     代理对象的引用
                     * @param method    当前执行的方法
                     * @param args      当前执行方法所需的参数
                     * @return 和被代理对象方法相同的返回值
                     * @throws Throwable
                     */
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //代码增强
                        Object returnValue = null;
                        //1.获取方法的参数
                        Float money = (Float) args[0];
                        //2.判断当前的方法是不是销售
                        if ("saleProduct".equals(method.getName())) {
                            returnValue = method.invoke(producer, money * 0.8f);
                        }
                        return returnValue;
                    }
                });
        iProducer.saleProduct(10000f);
    }
}

2.解决案例中的问题:

不使用动态代理的时候,要在业务层实现多个方法的相同功能时,需要多次调用,重复代码太多了,所以可以借助于动态代理的方式去解决,动态代理其实就是把当前的方法加强了一次,完全不影响当前方法的正常执行的。
本来没有使用动态代理的时候实现的方法是这样的:

public void transfer(String sourceName,String targetName,Float money) {
       try{
            //1.开启事务
            txUtil.beginTransaction();
            //2.执行操作
            //2.1 根据名称查询转入的账户
            List<Account> source = accountDao.findByName(sourceName);
            //2.2 根据名称查询转出的账户
            List<Account> target = accountDao.findByName(targetName);
            //2.3 转出的账户进行减钱的操作
            source.get(0).setMoney(source.get(0).getMoney() - money);
            //2.4 转入的账户进行加钱操作
            target.get(0).setMoney(target.get(0).getMoney() + money);
            //2.5 更新转出的账户
            accountDao.updateAccount(source.get(0));
            int i = 1/0;
            //6.更新转入的账户
            accountDao.updateAccount(target.get(0));
            //3.提交事务
            txUtil.commit();
        }catch (Exception e){
            //4.回滚事务
            txUtil.rollBack();
            throw new RuntimeException(e);
        }finally {
            //5.关闭事务
            txUtil.close();
        }
    }

加入了一个工厂方法,使用动态代理加强之后:

public class BeanFactory {

    //在此类中获取bean实例对象,使用代理方法

    private AccountService accountService;
    private TransactionUtil txUtil;

    public void setTxUtil(TransactionUtil txUtil) {
        this.txUtil = txUtil;
    }

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    public AccountService getAccountService(){
        AccountService accountServiceProxy = (AccountService) Proxy.newProxyInstance(accountService.getClass().getClassLoader(),
                accountService.getClass().getInterfaces(),
                new InvocationHandler() {
                    /**
                     * 添加事务的支持
                     * @param proxy
                     * @param method
                     * @param args
                     * @return
                     * @throws Throwable
                     */
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object rtValue = null;
                try{
                    //1.开启事务
                    txUtil.beginTransaction();
                    //2.执行操作
                    rtValue = method.invoke(accountService,args);
                    //3.提交事务
                    txUtil.commit();
                    //4.返回结果集
                    return rtValue;
                }catch (Exception e){
                    //5.回滚事务
                    txUtil.rollBack();
                    throw new RuntimeException(e);
                }finally {
                    //6.关闭事务
                    txUtil.close();
                }
            }
        });
        return accountServiceProxy;
    }
}

在xml文件中配置spring注入:

<!-- 配置代理的service -->
    <bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean>

    <!-- 配置代理对象 -->
    <bean id="beanFactory" class="com.xxx.factory.BeanFactory">
        <property name="accountService" ref="accountService"></property>
        <property name="txUtil" ref="txUtil"></property>
    </bean>

    <!-- 注入配置 -->
    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImpl">
        <!-- 配置注入的dao -->
        <property name="accountDao" ref="accountDao"></property>
    </bean>

此时上述的方法就可以简化了,以后调用就直接调用代理的对象就可以了:

public void transfer(String sourceName,String targetName,Float money) {
        //2.执行操作
        //2.1 根据名称查询转入的账户
        List<Account> source = accountDao.findByName(sourceName);
        //2.2 根据名称查询转出的账户
        List<Account> target = accountDao.findByName(targetName);
        //2.3 转出的账户进行减钱的操作
        source.get(0).setMoney(source.get(0).getMoney() - money);
        //2.4 转入的账户进行加钱操作
        target.get(0).setMoney(target.get(0).getMoney() + money);
        //2.5 更新转出的账户
        accountDao.updateAccount(source.get(0));
//        int i = 1 / 0;
        //6.更新转入的账户
        accountDao.updateAccount(target.get(0));
    }

调用使用:AccountService accountService = ac.getBean("proxyAccountService", AccountService.class);
使用代理对象之后,很重要的两点就是,实现了程序间的耦合度降低,和*简化了重复代码的编写*,另外维护也简单。

3:.AOP的概念:

AOP:Aspect Oriented Programming :面向切面的编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率,维护也简单。
4.spring中的AOP的相关概念:
1)连接点和切入点:连接点就是在接口或者子类中声明的所有方法,但是切入点专指代理对象已经实现了的方法。
2)通知,包括前置通知,环绕通知等
3)开发的时候,使用aop,主要指的是写好了业务代码(会产生切入点)之后,将公共的代码抽取出来,制作成通知,然后在配置文件中声明切入点与通知之间的关系,即切面。
5.spring中基于XML和注解的AOP配置:
1)先创建一个业务类接口:IAccountService:

public interface IAccountService {
    /**
     * 模拟保存一个用户
     */
    void saveAccount();
    /**
     * 模拟更新
     */
    void updateAccount(int id);
    /**
     * 模拟删除一个用户
     */
    int deleteAccount();
}

2)创建一个业务接口的实现类:

public class AccountServiceImpl implements IAccountService {

    public void saveAccount() {
        System.out.println("保存了一个用户。。。");
    }

    public void updateAccount(int id) {
        System.out.println("更新了一个用户。。。"+id);
    }

    public int deleteAccount() {
        System.out.println("删除了一个用户。。。");
        return 0;
    }
}

3)配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置spring的Ioc,把service对象配置进来 -->
    <bean id="accountService" class="com.xxx.service.impl.AccountServiceImpl"></bean>

    <!--spring中基于XML的AOP配置步骤
       1、把通知Bean也交给spring来管理
       2、使用aop:config标签表明开始AOP的配置
       3、使用aop:aspect标签表明配置切面
               id属性:是给切面提供一个唯一标识
               ref属性:是指定通知类bean的Id。
       4、在aop:aspect标签的内部使用对应标签来配置通知的类型
              我们现在示例是让printLog方法在切入点方法执行之前之前:所以是前置通知
              aop:before:表示配置前置通知
                   method属性:用于指定Logger类中哪个方法是前置通知
                   pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强

           切入点表达式的写法:
               关键字:execution(表达式)
               表达式:
                   访问修饰符  返回值  包名.包名.包名...类名.方法名(参数列表)
               标准的表达式写法:
                   public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
               访问修饰符可以省略
                   void com.itheima.service.impl.AccountServiceImpl.saveAccount()
               返回值可以使用通配符,表示任意返回值
                   * com.itheima.service.impl.AccountServiceImpl.saveAccount()
               包名可以使用通配符,表示任意包。但是有几级包,就需要写几个*.
                   * *.*.*.*.AccountServiceImpl.saveAccount())
               包名可以使用..表示当前包及其子包
                   * *..AccountServiceImpl.saveAccount()
               类名和方法名都可以使用*来实现通配
                   * *..*.*()
               参数列表:
                   可以直接写数据类型:
                       基本类型直接写名称           int
                       引用类型写包名.类名的方式   java.lang.String
                   可以使用通配符表示任意类型,但是必须有参数
                   可以使用..表示有无参数均可,有参数可以是任意类型
               全通配写法:
                   * *..*.*(..)

               实际开发中切入点表达式的通常写法:
                   切到业务层实现类下的所有方法
                       * com.xxx.service.impl.*.*(..)
   -->
    <!-- 配置logger类 -->
    <bean id="logger" class="com.xxx.util.Logger"></bean>
    <!-- 配置AOP -->
    <aop:config>
        <!-- 配置切面 -->
        <aop:aspect id="loggerAdvice" ref="logger">
            <!-- 配置切入点 -->
            <aop:before method="printLog" pointcut="execution(* com.xxx.service.impl.*.*(..))"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

4)编写一个切面类(公共方法,在业务代码执行前或者后或指定位置执行):

public class Logger {
    /**
     * 用于打印日志的公共方法
     */
    public void printLog(){
        System.out.println("打印日志了。。。");
    }
}

5)编写一个测试类:

public class AOPTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService accountService = (IAccountService)ac.getBean("accountService");
        accountService.saveAccount();
        accountService.updateAccount(1);
        accountService.deleteAccount();
    }
}

基于注解的配置其实也差不多,主要就是标签的运用了。

四、spring中的JdbcTemplate以及Spring的事务控制

1.JdbcTemplate:就是使用spring配置jdbc相关属性内容

1)先导入配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.xxx</groupId>
    <artifactId>day04_spring_01_JDBCTemplate</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</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>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

    </dependencies>

</project>

2)准备好数据库,编写实体类:

public class Account implements Serializable {

    private Integer id;
    private String name;
    private Float money;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + 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;
    }
}

3)编写dao接口及其实现类:
IAccountDao:

public interface IAccountDao {
    /**
     * 根据id查询账户
     */
    public Account findById(Integer id);

    /**
     * 根据名字查询账户
     */
    public void findByName(String name);
}

AccountDaoImpl:

public class AccountDaoImpl implements IAccountDao {
    JdbcTemplate jt;
    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
    public Account findById(Integer id) {
        List<Account> accounts = jt.query("select * from account where id=?",new BeanPropertyRowMapper<Account>(Account.class),id);
        return accounts.isEmpty()?null:accounts.get(0);
    }
    public void findByName(String name) {
    }
}

4)编写spring的容器配置文件bean.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 配置jdbc模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置公共的dao -->
    <bean id="jdbcTemplateSupportDao" class="com.xxx.dao.impl.JdbcSupportDao">
        <property name="jt" ref="jdbcTemplate"></property>
    </bean>

    <!-- 配置持久层 -->
    <bean id="accountDao" class="com.xxx.dao.impl.AccountDaoImpl3">
        <property name="jt" ref="jdbcTemplate"></property>
    </bean>
    <!-- 配置jdbc数据源 -->
    <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/springtest"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
</beans>

5)编写一个测试类:

public class JdbcTemplateDemo1 {
    public static void main(String[] args) {
        //使用spring容器注入的方式
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
        jdbcTemplate.execute("insert into account(name,money) value('yyy',100)");
    }
}

事务的隔级别,回滚机制等设置都可以直接使用spring来设置,先不详细写下去了。
主要的bean.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="1234"></property>
    </bean>
    <!-- spring中基于XML的声明式事务控制配置步骤
        1、配置事务管理器
        2、配置事务的通知
                此时我们需要导入事务的约束 tx名称空间和约束,同时也需要aop的
                使用tx:advice标签配置事务通知
                    属性:
                        id:给事务通知起一个唯一标识
                        transaction-manager:给事务通知提供一个事务管理器引用
        3、配置AOP中的通用切入点表达式
        4、建立事务通知和切入点表达式的对应关系
        5、配置事务的属性
               是在事务的通知tx:advice标签的内部
     -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务的通知-->
    <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>

    <!-- 配置aop-->
    <aop:config>
        <!-- 配置切入点表达式-->
        <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
        <!--建立切入点表达式和事务通知的对应关系 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
</beans>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤整合SSM框架和Spring: 1. 首先,需要在项目引入SpringSpringMVC和MyBatis的依赖包,可以使用Maven或手动添加。 2. 在Spring的配置文件引入MyBatis和SpringMVC的配置文件,可以使用以下代码: ``` <import resource="classpath:mybatis-config.xml"/> <import resource="classpath:spring-mvc.xml"/> ``` 3. 配置MyBatis的数据源和事务管理器,可以使用以下代码: ``` <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> ``` 4. 配置SpringMVC的DispatcherServlet和HandlerMapping,可以使用以下代码: ``` <context:component-scan base-package="com.example.controller" /> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> ``` 5. 最后,在Spring的配置文件引入自己的业务逻辑和DAO层的Bean。 以上是整合SSM框架和Spring的大致步骤,具体实现还需要根据项目情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值