zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_Transactional

    <?xml version="1.0" encoding="GBK"?>
    <project name="spring" basedir="." default="">
        <property name="src" value="src"/>
        <property name="dest" value="classes"/>
    
        <path id="classpath">
            <fileset dir="../../lib">
                <include name="**/*.jar"/>
            </fileset>
            <pathelement path="${dest}"/>
        </path>
    
        <target name="compile" description="Compile all source code">
            <delete dir="${dest}"/>
            <mkdir dir="${dest}"/>
            <copy todir="${dest}">
                <fileset dir="${src}">
                    <exclude name="**/*.java"/>
                </fileset>        
            </copy>
            <javac destdir="${dest}" debug="true" includeantruntime="yes"
                deprecation="false" optimize="false" failonerror="true">
                <src path="${src}"/>
                <classpath refid="classpath"/>
                <compilerarg value="-Xlint:deprecation"/>
            </javac>
        </target>
    
        <target name="run" description="Run the main class" depends="compile">
            <java classname="lee.SpringTest" fork="yes" failonerror="true">
                <classpath refid="classpath"/>
            </java>
        </target>
    
    </project>
    drop database spring;
    create database spring;
    use spring;
    
    create table news_inf
    (
     news_id int primary key auto_increment,
     news_title varchar(255) unique,
     news_content varchar(255)
    );
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        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-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
        <!-- 定义数据源Bean,使用C3P0数据源实现,并注入数据源的必要信息 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close"
            p:driverClass="com.mysql.jdbc.Driver"
            p:jdbcUrl="jdbc:mysql://localhost/spring"
            p:user="root"
            p:password="32147"
            p:maxPoolSize="40"
            p:minPoolSize="2"
            p:initialPoolSize="2"
            p:maxIdleTime="30"/>
    
        <!-- 配置一个业务逻辑Bean -->
        <bean id="newsDao" class="org.crazyit.app.dao.impl.NewsDaoImpl"
            p:ds-ref="dataSource"/>
    
        <!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类 -->
        <!-- 该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现-->
        <!-- 配置DataSourceTransactionManager时需要依注入DataSource的引用 -->
        <bean id="transactionManager" 
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
            p:dataSource-ref="dataSource"/>
        <!-- 根据Annotation来生成事务代理 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
    </beans>
    package lee;
    
    import org.springframework.context.support.*;
    import org.springframework.context.*;
    
    import org.crazyit.app.dao.*;
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class SpringTest
    {
        public static void main(String[] args)
        {
            // 创建Spring容器
            ApplicationContext ctx = new
                ClassPathXmlApplicationContext("beans.xml");
            // 获取事务代理Bean
            NewsDao dao = (NewsDao)ctx
                .getBean("newsDao" , NewsDao.class);
            // 执行插入操作
            dao.insert("疯狂Java" , "轻量级Java EE企业应用实战");
        }
    }
    package org.crazyit.app.dao;
    
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public interface NewsDao
    {
        public void insert(String title, String content);
    }
    package org.crazyit.app.dao.impl;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.transaction.annotation.*;
    
    import org.crazyit.app.dao.*;
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class NewsDaoImpl implements NewsDao
    {
        private DataSource ds;
        public void setDs(DataSource ds)
        {
            this.ds = ds;
        }
        @Transactional(propagation=Propagation.REQUIRED ,
            isolation=Isolation.DEFAULT , timeout=5)
        public void insert(String title, String content)
        {
            JdbcTemplate jt = new JdbcTemplate(ds);
            jt.update("insert into news_inf"
                + " values(null , ? , ?)"
                , title , content);
            // 两次插入的数据违反唯一键约束
            jt.update("insert into news_inf"
                + " values(null , ? , ?)"
                , title , content);
            // 如果没有事务控制,则第一条记录可以被插入
            // 如果增加事务控制,将发现第一条记录也插不进去。
        }
    }
  • 相关阅读:
    Python学习笔记(三)
    Python学习笔记(二)
    GDUFE ACM1159
    GDUEFE ACM1003 练手
    GDUFE ACM1033
    GDUFE ACM1128
    GDUFE ACM1002
    EDUFE ACM1050
    GDUFE ACM1007
    GDUFE ACM1003
  • 原文地址:https://www.cnblogs.com/tszr/p/12372672.html
Copyright © 2011-2022 走看看