s2sh项目搭建及使用详解

2024-06-19 11:48
文章标签 使用 项目 详解 搭建 s2sh

本文主要是介绍s2sh项目搭建及使用详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 把整合中遇到的问题放在最前面:再见
1、jar包冲突: 因为 Spring2.5 AOP Liberaries 里的 asm2.2.3.jar Hiberate 中的生成代理用的 asm.jar 冲突,我们需要删除 asm2.2.3.jar ,不然就会发生异常: java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor  。具体的删除方法(避免在 Tomcat 中的 lib 下删除了,然后又重新发布项目时此 jar 又重新发不到 lib 下):在 MyEclipse 中【 Window à Preferences à MyEclipse Enterprise Workbench à Project Capabilities à Spring 】在 spring2.5 AOP Libraries 中删除 asm2.2.3.jar

2、hibernate3.2生成映射文件报错:hibernate3.2生成oracle10.2映射文件时报如下错, An internal error occurred during: "Generating Artifacts". 问题原因,是因为Oracle的驱动程序不匹配,找最新版本的驱动程序,或者在本地安装的Oracle中找到对应的驱动包即可, 比如我的是:F:\oracle\product\10.2.0\db_1\jdbc\lib\classes12.jar,换这个试下,binggo! 另外,注意看一下eclipse\workspace\.metadata.metadata\.log文件,可以根据错误提示解决问题。
  • 整合步骤:/haha
1、先引入spring:
(1) 整合spring步骤
  a) 要引入的spring的library :
  b)测试spring的ioc是否配置成功:
① 配置applicationContext.xml测试数据
<span style="white-space:pre">	</span><span style="white-space:pre">	</span><!-- 测试spring的IOC是否配置成功 --><bean id="testIOC" class="com.spring.test.TestService"><property name="name" value="gwm"></property></bean>
② 编写Java测试代码
<span style="white-space:pre">	</span>获取ApplicationContext类
<span style="white-space:pre">	</span>public class LoadBeansXML {<span style="white-space:pre">	</span>private static ApplicationContext context;<span style="white-space:pre">	</span>public static ApplicationContext getContext(){<span style="white-space:pre">	</span>if(context == null){<span style="white-space:pre">	</span>context = new ClassPathXmlApplicationContext("config/applicationContext.xml");<span style="white-space:pre">	</span>return context;<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>return context;<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>测试类中的测试方法<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span><span style="white-space:pre">	</span>@Testpublic void test(){ApplicationContext cxt = LoadBeansXML.getContext();TestService ts = (TestService) cxt.getBean("testIOC");//测试spring的ioc是否配置成功System.out.println(ts.getName());//打印出gwm则成功}
2、spring与hibernate整合
(1)配置文件
a)  在添加 Hibernate 支持之前,我们先在【 MyEclipse DataBase Explorer 】中设置数据库连接,使用Oracle数据库,驱动程序为classes12.jar。
一路默认即可。
b)添加  Hibernate Capbilities
c)使用jdbc开发模式
d)不创建sessionFactory,使用applicationContext.xml方式配置sessionFactory和datasource,然后【finish】即可。
e)在applicationContext.xml为当前页面时,打开OutLine视图,右键new  Hibernate sessionFactory
可添加sessionFactory配置及相关属性
 (2)开发,根据数据表建立映射文件及相应的pojo
a)切换到MyEclipse DataBase Explorer】视图,找到前面新建的数据库连接,打开,在table中建立业务所需要的表,在该表上右键选择【Hibernate reverse Engineering

注:主键的生成方式可依据选择的数据库种类做出相应选择:Id Generator选择native,如果是Oracle可以选择increment
Java package要事先在项目中建立相应的包,使生成的entity实体和entity.hbm.xml存放在相应的包中。

--------------------------------------------------------------------------------

------------------------------------------------------------------------------------

注:此处有可能由于数据库驱动程序的问题,会出现问题,导致创建失败,请在前面找解决方案
b)上一步成功之后,就可以看到在com.spring.zd.pojo包中生成的实体类和相应的映射文件,以及在applicationContext.xml的sessionFactory中生成映射文件的映射属性路径
<span style="white-space:pre">		</span><span style="white-space:pre">	</span>
<span style="white-space:pre">		</span><property name="mappingResources"><list><value>com/spring/zd/pojo/TbUserinfo.hbm.xml</value></list></property>
c)编写hibernate的dao层代码,操作数据库
接口Dao
public interface Dao {//保存单个Serializable saveEntity(Object entity);//更新单个void updateEntity(Object entity);//删除单个void deleteEntity(Object entity);//获取单个Object getEntity(Class entityClass, Serializable id);//获取单个 延迟加载Object loadEntity(Class entityClass, Serializable id);//根据条件查询List findEntityByCondition(String hql, Object[] paramArr);//查询所有List findAllEntity(String hql);
实现Dao接口的实现类,<span style="line-height: 15px; font-size: 13px; text-indent: 28px; font-family: 宋体;">同时要继承</span><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; text-indent: 28px;">HibernateDaoSupport</span><span style="line-height: 15px; font-size: 13px; text-indent: 28px; font-family: 宋体;">类</span>
public class BaseDao extends HibernateDaoSupport implements Dao{//	private Session session;private DataSource dataSource;public DataSource getDataSource() {return dataSource;}public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}public void deleteEntity(Object entity) {getHibernateTemplate().delete(entity);}public List findAllEntity(String hql) {List<Object> listCondition = new ArrayList<Object>();listCondition = getHibernateTemplate().find(hql);return listCondition;}public List findEntityByCondition(String hql,Object[] paramArr) {List<Object> list = new ArrayList<Object>();list = getHibernateTemplate().find(hql, paramArr);return list;}public Object getEntity(Class entityClass, Serializable id) {Object entity = getHibernateTemplate().get(entityClass, id);return entity;}public Object loadEntity(Class entityClass, Serializable id) {Object entity = null;try {entity = getHibernateTemplate().load(entityClass, id);//load方法对象不存在时抛出异常} catch (Exception e) {}return entity;}public Serializable saveEntity(Object entity) {Serializable id = getHibernateTemplate().save(entity);return id;}public void updateEntity(Object entity) {getHibernateTemplate().update(entity);}
d)编写service层代码,以便spring对复杂的业务逻辑实现事务管理
service层接口
public interface  IUserService {//拥有多个操作,但这些操作必须 在同一个事物中(即复杂的业务逻辑时需要事务管理)void saveAndDelete(UserInfo userInfo);
}
实现service层接口的实现类
public class UserServiceImpl implements IUserService{private Dao dao;public void saveAndDelete(UserInfo userInfo){Serializable id = dao.saveEntity(userInfo);System.out.println(id.toString());//测试spring管理的事务是否配置成功
//		int i = 1/0;UserInfo entity = (UserInfo) dao.loadEntity(UserInfo.class, id);dao.deleteEntity(entity);}public void setDao(Dao dao) {this.dao = dao;}}
e)操作数据库代码编写完成后,我们就需要在spring中注册类了。打开applicationContext.xml,然后注册<bean>
在applicationContext.xml中注册bean
<!-- ioc加载bean 开始 --><bean id="dao" class="com.spring.zd.dao.impl.BaseDao"><property name="sessionFactory" ref="sessionFactory"></property><property name="dataSource" ref="dataSource"></property></bean><bean id="userService" class="com.spring.zd.service.impl.UserServiceImpl"><property name="dao" ref="dao"></property></bean><!-- ioc加载bean 结束 -->	
使用spring声明式事务配置hibernate的事务管理(主要管理service--具有复杂的业务逻辑)
<!-- 配置事务管理器 指定其作用的sessionFactory把事务交给Spring管理 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 在applicationContext.xml中默认是不能使用<tx:advice>和<aop:config>标签,需要在applicationContext.xml中,加入spring2.5的文档说明:xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 后才能使用。--><!-- 配置事务的传播属性 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="bulk*" propagation="REQUIRED"/><tx:method name="get*" read-only="true"/><tx:method name="load*" read-only="true"/><tx:method name="find*" read-only="true"/></tx:attributes></tx:advice><!-- 事务通知和切点的装配器,织入环境中,将事务交给Spring管理 --><aop:config><aop:pointcut expression="execution(* com.spring.zd.service.*.*(..))" id="pointCut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/></aop:config>
<span style="text-indent: 20px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">f)测试类</span>
//用session处理public void test1(){Session session = LoadBeansXML.getSession();Transaction transaction = session.beginTransaction();UserInfo userInfo = new UserInfo("gwm", "123456", nowDate, 123456.23);Serializable id = session.save(userInfo);System.out.println(id);transaction.commit();}//hibernateTemplete 单事务处理public void test2(){UserInfo userInfo = new UserInfo("gwm2", "123456", nowDate, 123456.23);dao.saveEntity(userInfo);}//hibernateTemplete 多事务处理//测试spring管理的事务是否配置成功@Testpublic void test3(){IUserService userService = (IUserService) cxt.getBean("userService");UserInfo userInfo = new UserInfo("gwm3", "123456", nowDate, 123456.23);userService.saveAndDelete(userInfo);}
g)至此,spring2.5与hibernate3.2整合结束,测试成功!

3、spring与struts2整合
(1)添加struts2的library
a) 添加  Struts Capbilities,勾选中其中两项即可,点击finish.

b)上一步完成后,就可以看到项目的src目录下多了struts的配置文件struts.xml
c)在struts.xml配置文件中,可以通过<constant>节点设置:项目的开发模式,项目的字符编码集,项目是否使用远程方法调用等(这些属性可以在struts2-core-2.1.1.jar包下的default.properties文件中找到相关属性的和属性对应的值)。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts><!-- 设置项目的开发模式 --><constant name="struts.devMode" value="true"></constant><!-- 设置项目的编码格式 --><constant name="struts.i18n.encoding"  value="GBK"></constant><!-- 是否允许远程方法调用 --><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><package name="default" namespace="/" extends="struts-default"><action name="login" class="com.spring.zd.action.Login" method="regester"><result>/user/list.jsp</result></action></package>
</struts>  
d)编写action层代码
action层代码的实现方式分为两种方式:
一种是属性驱动:属性驱动需要继承子ActionSupport类,在类中写上需要获取的属性名称(必须是jsp页面form表单中存在的属性名称才能成功获取),对应的getter和setter方法。或者是在jsp页面有与form表单中属性名称相对应的javabean,可在jsp页面中设置 对象.属性 ,在action中new 出javabean对应的对象。
属性驱动  .java
public class Login <span style="background-color: rgb(51, 204, 0);">extends ActionSupport</span>{private UserInfo <span style="background-color: rgb(51, 204, 0);">userInfo</span>;private IUserService userService;//	private Log log = LogFactory.getLog(this.getClass());<span style="color:#33cc00;">public void setUserInfo(UserInfo userInfo) {this.userInfo = userInfo;}public UserInfo getUserInfo() {return userInfo;}</span>public void setUserService(IUserService userService) {this.userService = userService;}public String regester(){
//		System.out.println(userInfo.getUsername());System.out.println(userInfo.getPassword());System.out.println(userInfo.getHiredate());userService.saveUserInfo(userInfo);
//		userService.saveAndDelete(userInfo);return this.SUCCESS;}}
<span style="white-space:pre">	</span>.jsp 
<body>This is my JSP page. <br><form method="post" action="<span style="background-color: rgb(255, 153, 0);">login!regester.action</span>" name="fm"><!-- 远程方法调用:<span style="background-color: rgb(255, 153, 0); font-family: Arial, Helvetica, sans-serif;">login!regester.action <span style="color:#ffffcc;">      </span></span>action的name!方法名.action -->username:<input name="<span style="color:#33cc00;">userInfo.username</span>" /><br>password:<input type="password" name="userInfo.password"/><br> hiredate:<input name="userInfo.hiredate" /><br><input type="submit" value="Login"/></form></body>
一种是模型驱动:模型驱动需要实现ModelDrivern<T>接口,同时实现接口的getModel方法(作用:获取对象)
模型驱动  .java
public class Login <span style="color:#33cc00;">implements ModelDriven<UserInfo></span>{private UserInfo userInfo<span style="color:#33cc00;"> = new UserInfo()</span>;private IUserService userService;//	private Log log = LogFactory.getLog(this.getClass());<span style="color:#33cc00;">	public void setUserInfo(UserInfo userInfo) {this.userInfo = userInfo;}public UserInfo getUserInfo() {return userInfo;}</span>public void setUserService(IUserService userService) {this.userService = userService;}public String regester(){
//		System.out.println(userInfo.getUsername());System.out.println(userInfo.getPassword());System.out.println(userInfo.getHiredate());userService.saveUserInfo(userInfo);
//		userService.saveAndDelete(userInfo);return null;}@Override
<span style="color:#33cc00;">	public UserInfo getModel() {// TODO Auto-generated method stubreturn userInfo;}</span>}
<span>	</span>.jsp 
<body>This is my JSP page. <br><form method="post" action="<span style="background-color: rgb(255, 153, 0);">login!regester.action</span>" name="fm"><!-- 远程方法调用:<span style="background-color: rgb(255, 153, 0); font-family: Arial, Helvetica, sans-serif;">login!regester.action <span style="color:#ffffcc;">      </span></span>action的name!方法名.action -->username:<input name="<span style="color:#33cc00;">username</span>" /><br>password:<input type="password" name="password"/><br> hiredate:<input name="hiredate" /><br><input type="submit" value="Login"/></form></body>


  • 整合过程中的关键点:

1、在struts.xml中配置struts.ObjectFactory属性,可通过spring管理struts

<constant name="struts.ObjectFactory" value="spring"></constant>

2、在applicationContext.xml的文件声明中,配置  default-autowire="byName"属性 ,这样可以自动装配,不必显示配置依赖的bean

3、struts2使用属性驱动或者模型驱动时,使用对象封装来获取对象属性时,该对象的getter和setter方法,必须全部写上,否则拦截器不能正确的获取到对象的属性。(如果只写对象的setter方法,只能获取对象的第一个属性值,其它值为null)











这篇关于s2sh项目搭建及使用详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1074943

相关文章

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.