java SSM第三章学习内容(Spring与Mybatis整合的方式四:注解,五:事务,六:事务注解)

四.注解

 

基本步骤:

1.导jar包到lib下并添加依赖
2.把applicationContext.xml及mybatis-config.xml整合到recources包下




1.根据数据库创建实体类
2.mybatis-config.xml的配置固定以下参数

3.写个接口给于方法(如增删改查)

4.在LianxiMapper.xml配置SQL语句

5.新建LianxiServicet并使用注解(@Service代表在配置文件加入Bean,而@Autowired代表引用相应的类)

6.在applicationContext.xml文件中添加配置,需要加入扫描注解

7.测试类


部分参考代码:

LianxiServicet:

@Service("LianxiServices")
public class LianxiServicet implements LianxiMapper {
        @Autowired
        private LianxiMapper lianxiMapper;
        private Lianxi lianxi;
        public List<Lianxi> selectAll() {
            // TODO Auto-generated method stub
            return lianxiMapper.selectAll();
        }        
        public int updateAll(Lianxi lianxi) {
            return lianxiMapper.updateAll(lianxi);
        }
        public int insertAll(Lianxi lianxi) {
            return lianxiMapper.insertAll(lianxi);
        }        
        public int delectAll(Lianxi lianxi) {
            return lianxiMapper.delectAll(lianxi);
        }
        public LianxiMapper getLianxiMapper() {
            return lianxiMapper;
        }
        public void setLianxiMapper(LianxiMapper lianxiMapper) {
            this.lianxiMapper = lianxiMapper;
        }
}


LianxiMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.LianxiMapper">

    <select id="selectAll" resultType="Lianxi">
        select * from Lianxi
    </select>
    
    <update id="updateAll" parameterType="Lianxi">
        update Lianxi set model=#{model} where id=#{id}
    </update>
    
    <insert id="insertAll" parameterType="Lianxi">
        insert into Lianxi values(#{id},#{model},#{ofprice},#{ofDate})
    </insert>
    
    <delete id="delectAll" parameterType="Lianxi">
        delete from Lianxi where id=#{id}
    </delete>
</mapper>


applicationContext.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:p="http://www.springframework.org/schema/p"
    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-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.0.1:3306/supermarket?
                        useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

    <!-- 配置DAO -->
    <bean  class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.dao.LianxiMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <context:component-scan base-package="com.servicet"/>
    </beans>


Lianxi1Test测试类:

public class Lianxi1Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        LianxiServicet lianxiServicet=(LianxiServicet)context.getBean("LianxiServices");
        //查询
        List<Lianxi> list = lianxiServicet.selectAll();
        for (Lianxi li:list) {
            System.out.println(li.getId()+li.getModel());
        }
        
        /*//更改
        Lianxi lx = new Lianxi(4,"嘻嘻");
        int num = lianxiServicet.updateAll(lx);
        System.out.println(num);*/
        
        /*//增加
        Date date =null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse("2005-12-12");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Lianxi lx2 = new Lianxi(6,"哈哈",200.87,date);
        int num2 = lianxiServicet.insertAll(lx2);
        System.out.println(num2);*/
        
        /*//删除
        Lianxi lx = new Lianxi(6);
        int num =  lianxiServicet.delectAll(lx);
        System.out.println(num);*/
    }
}


五.事务
 

基本步骤:

1.导jar包到lib下并添加依赖(这里多了一个jar包,tx见下图)
2.把applicationContext.xml及mybatis-config.xml整合到recources包下



3.在applicationContext.xml配置一下头部链接以及配置事务,切面,要修改名称



参考代码:

applicationContext.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:p="http://www.springframework.org/schema/p"
    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-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/supermarket?
                        useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引用数据源组件 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 引用MyBatis配置文件中的配置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!-- 配置DAO -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
        <property name="basePackage" value="com.dao" />
    </bean>

    <context:component-scan base-package="com.servicet" />
    <!-- 定义事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="select*" propagation="SUPPORTS" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delect*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- 定义切面 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod"
            expression="execution(* com.servicet..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
    </aop:config>
</beans>




六:事务注解


 

基本步骤:

1.导jar包到lib下并添加依赖(这里多了一个jar包,tx)
2.把applicationContext.xml及mybatis-config.xml整合到recources包下
3.在applicationContext.xml配置一下头部链接以及配置事务,切面,要修改名称
(要使用扫描<context:component-scan base-package="com.servicet" />)
4.在需要使用事务的类中进行添加注解
@Transactional
@Override
@Transactional(propagation = Propagation.SUPPORTS)


参考代码:

applicationContext.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:p="http://www.springframework.org/schema/p"
    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-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/supermarket?
                        useUnicode=true&amp;characterEncoding=utf-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 引用数据源组件 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 引用MyBatis配置文件中的配置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <!-- 配置DAO -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
        <property name="basePackage" value="com.dao" />
    </bean>

    <context:component-scan base-package="com.servicet" />
    <!-- 定义事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="select*" propagation="SUPPORTS" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delect*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- 定义切面 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="serviceMethod"
            expression="execution(* com.servicet..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
    </aop:config>
</beans>


LianxiServicet:

@Transactional
@Service("LianxiServices")
public class LianxiServicet implements LianxiMapper {
        @Autowired
        
        private LianxiMapper lianxiMapper;
        private Lianxi lianxi;
        @Override
        @Transactional(propagation = Propagation.SUPPORTS)
        public List<Lianxi> selectAll() {
            // TODO Auto-generated method stub
            return lianxiMapper.selectAll();
        }        
        public int updateAll(Lianxi lianxi) {
            return lianxiMapper.updateAll(lianxi);
        }
        public int insertAll(Lianxi lianxi) {
            return lianxiMapper.insertAll(lianxi);
        }        
        public int delectAll(Lianxi lianxi) {
            return lianxiMapper.delectAll(lianxi);
        }
        public LianxiMapper getLianxiMapper() {
            return lianxiMapper;
        }
        public void setLianxiMapper(LianxiMapper lianxiMapper) {
            this.lianxiMapper = lianxiMapper;
        }
}

猜你喜欢

转载自blog.csdn.net/JayVergil/article/details/81267206