SpringTx事务控制之全注解配置——简单案例

Spring TX配置(声明式事务管理+annotation) Spring的事务管理提供了对多种事务管理器的支持,如JDBC事务管理器、Hibernate事务管理器、JTA事务管理器等,以适配不同的数据访问技术。总的来说,Spring的事务管理使得数据库操作更加简单、可靠和灵活,提高了应用程序的性能和稳定性。 阅读详情

说明

基于上次半注解的代码进行修改成全注解模式
https://blog.youkuaiyun.com/Android_Cob/article/details/105168439——半注解
https://blog.youkuaiyun.com/Android_Cob/article/details/105167595——xml注入

正文

1.pom.xml文件

 <dependencies>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.1_3</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>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</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>8.0.15</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.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

2.我的项目结构

在这里插入图片描述

3. 写注解类

3.1注解父类

package com.utils;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration           /*声明配置类*/
@ComponentScan("com")   /* 扫描包及其子包*/
@Import({JdbcUtil.class,TransactionConfig.class}) /*导入其他配置文件*/
@PropertySource("jdbcConfig.properties") /*读取jdbc配置文件*/
@EnableTransactionManagement  /*添加注解事务支持*/
public class SpringConfiguration {

}

3.2子类注解类——Jdbc注解

package com.utils;

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

import javax.sql.DataSource;


public class JdbcUtil {
    /*使用EL*/
    @Value("${driver}")
    private String  driverClassName;

    @Value("${jdbcUrl}")
    private String  url;

    @Value("${user}")
    private String  username;

    @Value("${password}")
    private String  password;

    /* 配置jdbcTemplate*/
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "datasource")
    public DataSource getDataSource() {
        /*连接数据库的必备信息*/
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setPassword(password);
        dataSource.setUsername(username);

        return dataSource;
    }
}

3.2子类注解——事务管理器

package com.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

public class TransactionConfig {

    /**
     * 用于创建事务管理器系统
     * @param dataSource
     * @return
     */
    @Bean(name = "transactionManager")
    public PlatformTransactionManager getTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);

    }
}

4.其他类

与上次半注解一样

4.1持久层

package com.dao.impl;

import com.dao.IAccountDao;
import com.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("accountDao")
public class AccountDaoImpl   implements IAccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Account> findAllAccount() {
        List<Account> query=null;
        try {
         query = jdbcTemplate.query("select * from account ",
            new BeanPropertyRowMapper<Account>(Account.class));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return query;
    }

    public Account findById(Integer id) {
        Account query =null;
        try {
             query = jdbcTemplate.queryForObject("select * from account where id = ?  ", new BeanPropertyRowMapper<Account>(Account.class), id);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return query;
    }

    public void saveAccount(Account account) {

        jdbcTemplate.update("insert into account (name,money)  values(?,?)  ", account.getName(),
           account.getMoney());

    }

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

    public void delAccount(Integer id) {

        jdbcTemplate.update("delete from    account  where id =? ",id);

    }

    public Account findByName(String name) {
        Account query = null;
        try {
            query = jdbcTemplate.queryForObject("select * from account where name =?",
                    new BeanPropertyRowMapper<Account>(Account.class), name);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return query;
    }
}

4.2 事务层

package com.service.impl;

import com.dao.IAccountDao;
import com.domain.Account;
import com.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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


    public List<Account> findAllAccount() {
        return dao.findAllAccount();
    }

    public Account findById(Integer id) {
        return dao.findById(id);
    }
    @Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
    public void saveAccount(Account account) {
            dao.saveAccount(account);
    }
    @Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
    public void updateAccount(Account account) {
            dao.updateAccount(account);
    }
    @Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
    public void delAccount(Integer id) {
        dao.delAccount(id);
    }

    @Transactional(propagation = Propagation.REQUIRED ,readOnly = false)
    public void transfer(String Name, String ToName, Integer money) {
        Account oldAccount = dao.findByName(Name);
        Account newAccount = dao.findByName(ToName);

        oldAccount.setMoney(oldAccount.getMoney()-money);

        newAccount.setMoney(newAccount.getMoney()+money);

        updateAccount(oldAccount);
        //int a =5/0;
        updateAccount(newAccount);
    }
}

5.测试

5.1用转账测试事务控制

import com.service.IAccountService;
import com.utils.SpringConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.annotation.Annotation;

public class test {
    /*转账测试*/
    @Test
    void test() {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        IAccountService service = (IAccountService) ac.getBean("accountService");
        service.transfer("aaa","bbb",1000);
        /*结论如果service中出错便会回滚事务*/
    }

}

经测试可以受Spring事务控制

Spring事务管理详解 事务是一个最小的不可再分的工作单元。 一个事务对应一套完整的业务操作。事务管理是指这些操作要么全部成功执行,要么全部回滚,从而保证数据的一致性和完整性。比如银行转账,需要保证转出和转入是一个原子操作。Spring提供了声明式和编程式两种事务管理方式。 阅读详情

相关推荐

spring-TX事务管理

文章目录事务与AOPspring-TX事务管理具体实现: XML配置方式1. 事务管理器配置2.事务集配置3.AOP手术刀配置具体实现: 注解配置方式 事务与AOP ​ 数据库事务是一个集合概念,可以由多个增删改查组成。当其中某个操作出现错误时,应该对前面已经执行的操作进行回滚。JAVA是自动提交事务的。如果要实现事务的功能,需要使用try/catch/final语句。首先关闭自动提交事务,在执行...

qq_32640013的博客 833

Spring学习-Spring Tx

一.概述 Spring Tx 全称为 Spring Transaction Management(Spring 事务管理),是 Spring 为 DB 事务管理提供过的一种便捷的接入方式。 二.导入依赖 Step1:在 pom.xml 中导入 spring-tx 依赖 Step:刷新 Maven 依赖,可以看到 spring-tx 联动依赖 spring-core 和 spring-beans 库 三.Spring 目前常用的事务两种机制 注解式:通过 @Transactional

qq_48772498的博客 5314

spring注解事务实现demo

声明式事务管理也有两种常用的方式,一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解。显然基于注解的方式更简单易用,更清爽。spring注解事务实现demo

Spring事务控制——SpringTx和SpringJdbc配置xml的简单使用

目录配置环境1.配置pom.xml,主要导入Spring框架2.配置Spring的xml配置文件代码实现3.我的test项目结构图3.1 持久层实现类继承了JdbcDaoSupport,这个类对JdbcTemplate进行了封装3.2 service实现类3.3 测试类完美实现对Jdbc的控制 配置环境 1.配置pom.xml,主要导入Spring框架 <dependencies> ...

Android_Cob的博客 585

AJ-Report开源数据大屏存在远程命令执行漏洞

该平台可以通过post方式在validationRules参数对应值中进行命令执行,可以获得服务器权限,登陆管理后台接管大屏。如果被攻击者利用,危害后果十分严重。

swordheart的博客 388

基于SOCKET的***网络软件的设计与实现

二、基于SOCKET的网络软件的需求。2、概要设计:画出软件系统结构图。3、详细设计:软件的详细设计,如类图、序列图、重要的算法流程图或伪代码,等等。4、实现:软件基本功能相关的截图和关键代码。

Recursions的博客 1371

maven项目spring的pom配置

UTF-8         4.3.5.RELEASE             org.springframework             spring-webmvc             ${spring.version}                               org.springframework             spring

ideatechinfo的专栏 1155

基于FSL的DTI数据预处理流程

最近在学习处理DTI数据,总结了一份应用FSL做DTI数据预处理的流程与大家交流交流。如果有错误的地方欢迎大家指正!        我用的数据是Philips的数据,如果是GE或者西门子的数据可能会有所不同。   原始数据:包括3DT1数据和高分辨率DTI数据,均是dicom格式。 工具:MRICRON和FSL,所有的命令均在Linux的终端中运行。   1.格式转换:将后缀名为dcm

zaza_zaza的博客 2万+

Spring的常见pom文件

<?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/mav

qq_30347133的博客 3129

QGIS实战:5分钟搞定谷歌地图、高德、天地图等多源地图加载(附最新图源URL)

本文详细介绍了如何在QGIS中快速加载谷歌地图、高德、天地图等多源地图,提供2023年验证可用的最新图源URL和配置步骤。从环境准备到高级技巧,涵盖国内外主流地图服务的完整解决方案,帮助用户提升GIS工作效率。

weixin_29210759的博客 1218

【HAL库-STM】智能温控按键小风扇【胎教级教学】

硬件设备:DHT11,0.96寸oled屏,二/三/四线风扇(不挑),面包板 、f10的精英板等等。当人在的风扇面前时可以自行控制风扇,不在面前时又想开风扇就让他温控自行开启,(在这里有一个想法就是加上蓝牙和wifi控制的功能能够远程遥控,这个如果做的再全一点就要自主研发是一个多功能app,这里没有做。针对这个dht11,我一开始是卡在了这个check函数里面,一直是我给完dht11低电平和高电平后,dht11不回复我低电平和高电平,这个不是我的问题,当时代码是没毛病的,这个点一直卡着我。吐血五天实现小成品

weixin_52451652的博客 1740

Mybatis与spring集成

1、导入pom依赖 1.1 添加spring相关依赖(5.0.2.RELEASE) spring-core spring-beans spring-context spring-orm spring-tx spring-aspects spring-web 1.2 添...

weixin_30273175的博客 363

STM32F103C8T6蓝莓派用HAL库GPIO模拟SPI驱动MAX31865读取PT100温度(附完整代码)

本文详细介绍了如何在STM32F103C8T6蓝莓派开发板上使用HAL库通过GPIO模拟SPI驱动MAX31865读取PT100温度传感器的完整方案。内容包括硬件连接、CubeMX配置、GPIO模拟SPI实现、温度计算算法及常见问题排查,并附有完整代码示例,帮助开发者快速实现高精度温度测量。

weixin_30684743的博客 937

Spring 项目常用的依赖(pom文件)

properties属性&lt;properties&gt; &lt;!--使用utf-8编码--&gt; &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; &lt;!--web--&gt; &lt;spring.version...

pxmxx的博客 4398

K8s(十二):监控与报警(163邮箱+钉钉)-Prometheus + Grafana + Alertmanager(超详细)

🔴 K8s(十二):监控与报警(163邮箱+钉钉)-Prometheus + Grafana + Alertmanager(超详细)

✨ 欢迎来到【Seal ^_^ 的优快云博客】!✨ 3万+

学Simulink——基于Simulink的Buck-Boost变换器双闭环PI+抗饱和设计建模示例

本文详细介绍了基于Simulink的非反相Buck-Boost变换器双闭环控制设计方法。针对Buck-Boost变换器特有的右半平面零点(RHPZ)和控制挑战,提出了电压-电流双闭环PI控制架构,并集成抗积分饱和机制。通过Simulink建模,实现了主电路、电流内环PI、电压外环PI、PWM及占空比限幅等模块的搭建,验证了在输入电压跌落(20V→10V)和负载突变(2A→4A)等场景下的稳定性能。仿真结果表明,输出电压波动小于0.35V,恢复时间快于1ms,电感电流有效限幅。文章还讨论了工程实践要点和扩展方

xiaoheshang_123的博客 318

Day68 Spring TX (事务)和配置细节以及单例设计模式

Spring其他配置细节 Spring其他配置细节: Spring配置文件流程: 加载Schema(配置全局自动注入) bean aop context 配置注解扫描 配置代理模式 配置属性配置文件扫描 配置数据库源 ...

新世界的大门 334

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

关于spring框架体系的pom依赖关系

很多人都在用spring开发java项目,但是配置maven依赖的时候并不能明确要配置哪些spring的jar,经常是胡乱添加一堆,编译或运行报错就继续配置jar依赖,导致spring依赖混乱,甚至下一次创建相同类型的工程时也不知道要配置哪些spring的依赖,只有拷贝,其实,当初我就是这么干的! spring的jar包只有20个左右,每个都有相应的功能,一个jar还可能依赖了若干其他jar,所以...

weixin_43222373的博客 1775

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

spring常用依赖-pom.xml

<properties> <org.springframework.version>4.3.19.RELEASE</org.springframework.version> <commons-logging.version>1.2</commons-logging.version> <ju...

diwei0861的博客 310

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

maven管理Spring MVC项目pom.xml配置

maven管理Spring MVC项目pom.xml配置

big_bigwolf的博客 4236

pom文件:Spring项目最常用,最全的依赖

<!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version&g...

lg_1996的博客 1099

spring框架maven依赖pom配置

使用Maven来管理Spring项目的依赖,可以有效地简化项目结构和依赖管理。上面的示例展示了如何为不同类型的Spring项目配置pom.xml,包括基本的Spring项目和Spring Boot项目。根据你的具体需求,可以调整或添加相应的依赖,以满足项目的功能要求。

jakeswang的博客 1万+

Java学习框架之spring37天(--AOP 注解使用-- 事物tx的注解使用;)

第一步: 在Application.xml的配置文件中 声明启动注解:   &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc...

博客 451

spring基于全注解的事务控制案例

spring基于全注解的事务控制案例 1.环境搭建 首先创建maven工程,在POM中导入坐标。 <?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.apa

lsx7758521的博客 332

TX事务注解说明

TX事务: @Transactional : 后面设置事务隔离级别。 需要 创建事务管理对象 之后再扫描注解即可。

csdndd521的博客 455
上一篇: SpringTx事务控制之半注解配置——简单案例
下一篇: MyBatis中xml文件配置以及demo案例(含多表查询)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这次最后一次熬夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值