这是最近整理的Spring整合Hibernate Struts1.x Struts2.x的相关配置文件,好的话大家拿去用,有什么不妥的地方欢迎大家批评指正!
Spring + hibernate +struts
一、spring对持久化的支持
1、Spring的数据访问设计思想(DAO、模板方法)
2、数据源配置:
方式一:Spring内置实现 DriverManagerDataSource
<!-- DataSource__DriverManagerDateSource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>jiaojun</value>
</property>
</bean>
方式二:DBCP提供的BasicDataSource
<!-- DataSource__BasicDateSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
方式三:JNDI数据源
JNDI数据源:(mysql 5,tomcat 5.5)
step1: 在tomcat/conf/server.xml中:
<Resource name="jdbc/mydatasource" auth="Container" description="DB Connection"
type="javax.sql.DataSource" username="root" password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test" maxActive="5" />
step2: 在tomcat/conf/context.xml中:
<ResourceLink name="mydatasource" global="jdbc/mydatasource" type="javax.sql.DataSourcer"/>
step3: 在spring applicationContext.xml中:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/mydatasource</value>
</property>
</bean>
3、JDBC支持:
step1: 配置数据源
step2: 配置JdbcTemplate
<!-- JdbcTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
step3:配置DAO
<!-- DAO -->
<bean id="orderDao" class="lab5.OrderDAOImpl">
<property name="jt"><ref bean="jdbcTemplate"/></property>
</bean>
注意:查询时,使用RowMapper(使用RowMapper定义如何将Result转换为对象)
implements RowMapper
4、hibernate支持:
step1: 配置数据源
step2: 配置sessionfactory
<!-- Session Factory -->
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>lab6/Order.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
</bean>
step3: 配置DAO
<!-- DAO -->
<bean id="orderDao" class="lab6.OrderDAOHibernateImpl">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
注意:以上配置是要求dao 继承HibernateDaoSupport
二、Spring对事务的支持:
1、Spring事务机制
声明式事务、事务管理器
2、hibernate事务p72
step1: 配置数据源
step2:配置sessionfactory (同上)
step3:配置事务管理器
<!-- Transaction Manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
step4:配置DAO
step5:配置目标对象
step6:创建事务服务代理
<!-- Service -->
<bean id="saleService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<value>lab7.SaleService</value>
</property>
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
<property name="target">
<ref bean="saleServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
注意:
事务属性描述格式:
传播行为,隔离级别,只读事务(readonly),回滚规则
在默认情况下,Spring的容器对于非受查异常(服务模块中抛出的非受查异常)
,会回滚事务。对于受查异常,会提交事务。
如果即使发生了某种受查异常,也要回滚事务,可以用 “- 异常类型“来声明。
同样,对于非受查异常,如果不要求回滚事务,可以用"+异常类型"来声明
3、简化事务配置
继承、自动代理
4、使用标注来进行事务管理(可选)
(1)引入相应命名空间
<?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-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
(2)在配置文件中添加
<!-- -->
<tx:annotation-driven transaction-manager="myTransactionManager"/>
(3)在service类中,使用@Transactional标记
增: @Transactional(propagation=Propagation.REQUIRED)
查: @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
改: @Transactional(propagation=Propagation.REQUIRED,rollbackFor=StockException.class)
5、分布式事务管理
步骤:
step1 导入jotm相关库
step2 编写carol.properties配置文件
step3 建两个数据库
step4 在spring配置文件中,配置jotm
<bean id="jotm"
class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
分布式事务要求数据源封装为XADataSource
<bean id="bankDS"
class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName"
value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/txdb1" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="1234" />
</bean>
有一个问题:程序执行完后,不会自行停止
三、Spring与struts 1整合:
前提:
必须在Web应用启动时,创建Spring的ApplicationContext实例
方式:
1、采用ContextLoaderListener来创建ApplicationContext:
<!-- ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2、采用ContextLoaderPlugIn来创建ApplicationContext
<!-- ContextLoaderPlugIn -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
</plug-in>
或者:
通过listener装载spring应用上下文
方式一:通过Spring的ActionSupport类
ActionSupport类:
知道ApplicationContext的获得方式。
步骤:
1、Action直接继承ActionSupport
2、使用ApplicationContext ctx = getWebApplicationContext();取得Spring上下文
3、取得相应Bean
注意:有可能需要替换commons-attributes-compiler.jar包。
优点:
简单
缺点:
耦合高
违反IOC
无法使用多方法的Action
方式二:通过Spring的DelegatingActionProxy类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
<!-- -->
<action path="/somepath" type="org.springframework.web.struts.DelegatingActionProxy"/>
3、在Spring配置文件中
<!-- -->
<bean name="/somepath" class="SomeAction">
<property name="service"><ref bean=""/>
</bean>
注意,要用bean name命名。
/somepath:Action的path
优点:
不使用Spring api编写 Action
利用了IOC装配。
可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,
避免struts中Action的线程安全问题。
缺点:
struts配置文件中,所有path都映射到同一个代理类
方式三:通过Spring的DelegatingRequestProcessor类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
<!-- -->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
3、在Spring配置文件中
<!-- -->
<bean name="/somepath" class="SomeAction">
<property name="service"><ref bean=""/>
</bean>
小结:
Spring与Struts整合方式只有两种:
(1)由Spring容器来管理Action(方式二,方式三)
(2)Action处于容器之外(方式一)
注意:
中文问题:
设置过滤器,设置页面编码,数据库编码
struts集成spring的方式一:
1、加载AppliationContext:
配置插件
<!-- -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
</plug-in>
或者采用listener方式:
<!-- -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-config/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2、 让Action继承ActionSupport
3、 调用ActionSupport的方法getWebApplicationContext获得WebApplicationContext
通过WebApplicatioContext获得被spring容器管理的Service
方式二:通过Spring的DelegatingActionProxy类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
<!-- -->
<action path="/somepath" type="org.springframework.web.struts.DelegatingActionProxy"/>
3、在Spring配置文件中
<!-- -->
<bean name="/somepath" class="SomeAction">
<property name="service"><ref bean=""/>
</bean>
注意,要用bean name命名。
/somepath:Action的path
优点:
不使用Spring api编写 Action
利用了IOC装配。
可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,
避免struts中Action的线程安全问题。
缺点:
struts配置文件中,所有path都映射到同一个代理类
方式三:通过Spring的DelegatingRequestProcessor类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
<!-- -->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
3、在Spring配置文件中
<!-- -->
<bean name="/somepath" class="SomeAction">
<property name="service"><ref bean=""/>
</bean>
小结:
Spring与Struts整合方式只有两种:
(1)由Spring容器来管理Action(方式二,方式三)
(2)Action处于容器之外(方式一)
注意:
中文问题:
设置过滤器,设置页面编码,数据库编码
struts2集成spring的方式:
1 加载AppliationContext:
配置插件,在struts.xml
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/config/sale.xml" />
</plug-in>
或者采用listener方式(在web.xml):
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-config/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
2 添加Action由spring创建
<constant name="struts.objectFactory" value="spring" />
注意: Action中的class是个 ”逻辑名称“ 要与application中bean 的id名一样
片段:
<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="user" extends="struts-default">
<action name="sendMsg" method="sendMsg" class="MsgAction">
<result>/message.jsp</result>
<result name="input">/message.jsp</result>
</action>
</package>
</struts>
3 实际处理的Action,scope="prototype" 一定要设定,该属性说明Action可以生成多个
application中的片段:
<bean id="UserAction" scope="prototype"
class="action.UserAction">
</bean>
4 hibernate整合与spring整合Hibernate相同
相关推荐
Spring使用DriverManagerDataSource和C3P0分别配置MySql6.0.6数据源
Spring使用DriverManagerDataSource和C3P0分别配置MySql6.0.6数据源
DriverManager DataSource
概述: DriverManager是驱动管理器,DataSource是数据源,一般DataSource是建立在DriverManager的基础上的,配合一个连接池的实现 。 1.DriverManager DriverManager传统的jdbc连接,通过Class.forName("xxx")的办法注册之后,就可以DriverManager.getConnection()获得连接了。...
AD20/Altium designer——元器件批量命名与编号的高效技巧
本文详细介绍了在AD20/Altium Designer中实现元器件批量命名与编号的高效技巧。通过解析核心的“原理图标注”功能,从基础全局操作到进阶的按模块分区、利用标识符及合并BOM等智能方法,帮助硬件工程师彻底告别手动编号,大幅提升设计效率与文档规范性,并融入了一套完整的实战工作流。
DataSource和DriverManager区别及联系
在JDBC2.0或JDBC3.0中,所有的数据库驱动程序提供商必须提供一个实现了DataSource接口的类,要使用数据源必须首先在JNDI中注册该数据源对象。 如果在JNDI中注册了数据源对象,将会比起使用DriverManager来具有两个方面的优势: 首先,程序不需要像使用DriverManager一样对加载的数据库驱动程序信息进行硬编码,程序员可以选择先
距离多普勒谱(RD谱)绘制方法及理解
RD谱是信号处理领域常用的图形。原始信号往往是一维时域序列,如何将其二维化,并进行变换生成RD谱呢?由于刚刚接触,查阅了网络上、老师手里的资料,发现RD谱的绘制方法并不统一,有些存在一定问题。在此,将心得体会记录下来。
Camera ITS测试
一、手机端设置: 将 CtsVerifier.apk 安装到设备上, 连接上linux主机。 二、主机端设置: 基础:CTS测试运行环境 然后,主机安装python2.7,及numpy/scipy/matplotlib/opencv 栈和 python 成像库。按照下面的命令依次安装: sudo apt-get install python-numpy sudo apt-g...
JDBC连接数据库的两种方式:DriverManager及DataSource(DBCP,C3P0,druid)
方法1:使用java.sql.DriverManager类 驱动管理器类,用于管理所有注册的驱动程序。 (注:DataSource 接口是 JDBC 2.0 API 中的新增内容,它提供了连接到数据源的另一种方法。使用 DataSource 对象是连接到数据源的首选方法。) 常用的两个API registerDriver(driver) : 注册驱动类对象 ConnectiongetC...
18|AI 辅助精准测试:让平台会解释、会总结、会推荐
本文介绍 AI 在精准测试平台中的落地方式,包括变更摘要、风险解释、补测建议和报告总结。测试开发工程师。Java 后端开发工程师。质量平台 / DevOps 平台建设者。想了解精准测试落地方法的技术负责人。从报告中提取变更文件、未覆盖行、推荐用例。拼成结构化 JSON。让 AI 生成 200 字质量摘要。把摘要展示在报告顶部。读完本文后,可以复制下面的 Prompt,让 AI 生成一个报告总结练习。你是一名 AI 应用开发专家和测试架构师。
Spring培训笔记
Spring 负责整个应用 是业务逻辑组件框架 目的:程序结构良好,简化开发 必须要会 很重要 Spring开发包 dist/ spring.jar spring核心jar包 spring-src.zip modules/目录下将 spring核心jar包 拆分若干个子包 resources/目录 包括了spring的配置文件(.dtd .xsd .ftl .tld)
YOLO11实战优化:长尾分布解决 | 引入Seesaw Loss配合YOLO11分类头,完美应对类别极其不平衡的现实业务场景
摘要: YOLO11在工业缺陷检测等长尾分布场景中面临尾部类别检测失效的问题,根源在于头部类别样本的负样本梯度压制了尾部类别的学习。传统方法如重采样、Focal Loss等效果有限。Seesaw Loss通过动态调整缓解因子和补偿因子,平衡类别间的梯度分布,结合YOLO11的解耦分类头、Anchor-Free设计和C2PSA/C3K2模块,显著提升尾部类别识别能力。文章详细解析了Seesaw Loss的数学原理,并提供了可直接集成到YOLO11的PyTorch实现代码,为长尾分布下的目标检测提供了实用解决方
R语言机器学习算法实战系列(五)GBM算法分类器+SHAP值 (Gradient Boosting Machines)
本文介绍了如何使用R语言实现梯度提升机(GBM)模型,并通过乳腺癌数据集进行预测。GBM是一种集成学习算法,通过逐步添加决策树模型来纠正前一个模型的错误,从而提高预测准确性。文章详细阐述了GBM的算法原理,包括初始化模型、计算残差、构建决策树、更新模型等步骤。接着,文章提供了从数据下载、加载R包、数据预处理、数据切割、特征选择、参数设置、模型构建、预测测试数据、模型评估、特征重要性分析、SHAP值解释到模型保存的完整教程。数据集包含569份恶性和良性肿瘤样本的32类指标,通过80%的训练集和20%的测试集进
Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00
方便学生使用的工具文档里面有比较全的经典配置文件示例下面是章节 Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer
【3天快速入门WPF】从小白到新手入门教程
嘿,小伙伴们!如果你已经有一定的C#开发基础,但想快速掌握WPF开发,达到企业级水准,那接下来的这个三天快速入门计划绝对适合你!虽然听起来有点挑战,但别担心,只要跟着这个高强度、结构化的学习计划,你一定能搞定它。首先,我们得搞清楚,WPF的核心知识点到底有哪些。要想达到企业级开发水准,XAML、数据绑定、MVVM、控件使用、命令系统、样式模板这些统统都得拿下。听起来是不是有点多?别急,我们把任务分解到三天里,每天安排合理的内容,保证理论和实践相结合,让你学得扎实又高效。
SSH配置:spring:applicationConfig.xml
近来做了一个小项目,用到了Struts1.x,Spring 2.5和Hibernate3,其中的一些配置文件较为典型,所以拿到这里来和大家分享。xml version="1.0" encoding="UTF-8"?>beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http
TI单芯片毫米波雷达1642代码走读(〇)——总纲
前言 近年来,自动驾驶行业发展如火如荼,雷达技术也逐渐从军工封闭圈走向了开放的市场。 毫米波雷达具有全天候探测能力,特别是在雨雪雾天气以及夜间都能可靠工作,并且探测距离相对其他车载传感器非常远,对运动目标非常敏感,价格还低廉,这些特性都使其成为自动驾驶感知层面不可或缺的一环。 现如今的雷达已经不仅仅局限于测距测速测角等传统的功能,雷达技术正在从感知走向认知、从宽带走向超宽带、从二维走向三维四维、从相控阵走向数字阵列、从单通道走向MIMO、从独立工作走向分布式协同、从分立组件走向射频数字一体化。MIMO
pscad IEEE标准系统搭建说明
pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。
freemarker的测试结果框架_java必背综合知识点总结(框架篇)
框架篇一、Struts1的运行原理在启动时通过前端总控制器ActionServlet加载struts-config.xml并进行解析,当用户在jsp页面发送请求被struts1的核心控制器ActionServlet接收,ActionServlet在用户请求时将请求参数放到对应的ActionForm对象中的成员变量中,然后ActionServlet则会根据struts-config.xml中的映射关...
深度学习工具包 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');```[](https://bitdeli.com/free "Bitdeli Badge")
框架整合
v/:* {behavior:url(#default#VML);}o/:* {behavior:url(#default#VML);}w/:* {behavior:url(#default#VML);}.shape {behavior:url(#default#VML);} Normal 0 7.8 磅 0 2
InTheHand.Net.Personal+BlueTooth4,蓝牙开发
项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。
WINCC7.3破解文件
WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!
ssh配置报错解决
报错内容: Servlet action is currently unavailable 一、在 myeclipse 5.5 中配置Struts1.x + Spring 2.0 + Hibernate3.1 1、首先要强调的是框架添加顺序: 第一步 添加Hibernate 第二步 添加Spring 第三步 添加Struts 2、修改配置文件 ...
Struts2+Hibernate+Spring 整合示例
Struts2+Hibernate+Spring 整合示例 Spring整合Struts2、Hibernate原理概述: 从用户角度来看,用户发出HTTP请求,当MVC框架的控制器组件拦截到用户请求时,将调用系统的业务逻辑组件,业务逻辑组件则调用系统的DAO组件,而DAO组件则依赖于SessionFactory和DataSource等底层组件实现数据库访问。
框架(Spring、Struts2和Hibernate三者)整合
总结一下就是: 1.Struts2与Spring相连的是:action不是Struts2框架new 出来的,而是从Spring的xml(applicationContext.xml)配置文件中拿出 2.Spring和Hibernate相通的是:Hibernate的SessionFactory采用Spring注入,同时dao的实现类继承Spring的类(HibernateDaoSupport)
Struts2/Spring/Hibernate框架的整合与综合实例
1、框架介绍 1.1 Struts2框架 Struts是最早的Java开源框架之一,也是现在Java Web框架的事实标准。Struts是MVC设计模式一个优秀的实现。Struts定义了通用的Controller(控制器),通过配置文件(一般是struts-config.xml)隔离了Model(模型)和View(视图),以Action的概念对用户请求做了封装,使代码更清晰易读。到目前为止,S...
Spring、Struts2、Hibernate整合
一、 Struts 2框架整合Spring步骤 工作之后这些框架很久都没接触了,作为备忘,记录一下,以后用到的时候方便查看 1、 复制文件。复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下。其中的x对应了Spring的版本号。还需要复制commons-logging.jar文件到WEB-INF/lib目录下。
IntelliJ IDEA 下Spring整合Struts2、Hibernate
1. 整合原理: Spring与Struts2整合实际上是将Action对象交给Spring容器来负责创建。 Spring与Hibernate整合就是将sessionFactory交给Spring来负责维护,以及Spring维护session和aop事务。 2. 需要导入的特殊jar包 Hibernate中 java persist api java的持久化规范(接口) MySQL数据...
JavaWeb开发之SSH框架整合——Struts2+Spring3+Hibernate3三大框架的整合
Struts2+Spring3+Hibernate3导包 Struts2导包与配置 Struts2/apps/struts2-blank.war/WEB-INF/lib/*.jar 如下图所示: 导入与Spring整合的jar包 struts2-spring-plugin-2.3.15.1.jar 用于Struts2整合Springstruts2-json-plugin
SSH三大框架搭建整合struts2+spring+hibernate
SSH框架整合理论: 在SSH框架的JAVAweb项目的开发过程中,WEB层通常使用的是Struts2+jsp,service层使用的是javaBean,DAO层使用的是hibernate,而spring的使用覆盖三层。 使用了spring框架之后,我们可以把对象交给spring来管理。在WEB层中,所有action对象的创建和管理都可以交给spring来完成,这样Struts2就不用自己来new...
2523




被折叠的 条评论
为什么被折叠?



