Struts21+Hibernate32+Spring25注解搭建.docx
- 文档编号:5523736
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:22
- 大小:290.90KB
Struts21+Hibernate32+Spring25注解搭建.docx
《Struts21+Hibernate32+Spring25注解搭建.docx》由会员分享,可在线阅读,更多相关《Struts21+Hibernate32+Spring25注解搭建.docx(22页珍藏版)》请在冰点文库上搜索。
Struts21+Hibernate32+Spring25注解搭建
1.新建web项目SSHPRJ
2.整合Hibernate
右键项目,MyEclipse—>AddHibernateCapabilities
Next
Next
Next
Finish
整合Spring2.5
右键项目,MyEclipse—>AddSpringCapabilities
Libraries选择前4个和第11个。
Next
Next
Finish
4.导包
导入数据库连接池包:
proxool-0.9.1.jarproxool-cglib.jar
和数据库驱动包:
mysql-connector-java-5.0.8-bin.jar
5.编写src/jdbc.properties文件
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:
mysql:
//192.168.1.115:
3306/psldb
db.user=chens
db.password=11111
db.alias=yan_mysql
db.houseKeepingTestSql=select1
db.characterEncoding=UTF-8
db.maximumConnectionCount=50
db.minimumConnectionCount=20
6.编写applicationContext.xml文件
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns: jee="http: //www.springframework.org/schema/jee"xmlns: tx="http: //www.springframework.org/schema/tx" xmlns: context="http: //www.springframework.org/schema/context" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.5.xsd http: //www.springframework.org/schema/txhttp: //www.springframework.org/schema/tx/spring-tx-2.5.xsd http: //www.springframework.org/schema/jeehttp: //www.springframework.org/schema/jee/spring-jee-2.5.xsd http: //www.springframework.org/schema/contexthttp: //www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"> --关于spring启动的优化default-lazy-init="true"--> jdbc.properties --使用proxool连接池配置数据源--> -- alias别名名字 houseKeepingTestSql=selectCURRENT_DATE当连接为空闲连接时, 用此条sql语句来测试是否空闲中。 如果sql语句不存在,那么测试将被忽略。 maximum-connection-lifetime最大连接生命周期默认值: 4小时 maximum-active-time: 最大活动时间默认值: 5分钟 maximum-connection-count最大连接数默认值: 15个 minimum-connection-count最小连接数默认值: 5个 --> --hibernate配置文件所在地--> /com/zt/game --配置hibernate属性如第一个方言等--> -- 1.hibernate.dialectHibernateSQL方言表示连接那种数据库 2.hibernate.query.substitutions 你可以使用hibernate.query.substitutions定义新的Hibernate查询短语。 比如说: hibernate.query.substitutionstrue1,false0 这个配置意思是当你在Hibernate里面输入true的时候,Hibernate会转化为1插入数据库, 当你在Hibernate里面输入false的时候,Hibernate会转化为0插入数据库,后面的Y,N同理。 对于某些数据库,例如Oracle来说,没有boolean数据类型,就是采用1代表true,0代表false, 因此使用这个配置在Hibernate里面直接用true/false会非常直观。 hibernate.query.substitutionstoLowercase=LOWER 这可以让你重新命名SQL的LOWER函数。 3.hibernate.jdbc.batch_size一个非零值,会开启Hibernate使用JDBC2的批量更新功能取值.建议值在5和30之间。 4.hibernate.cache.provider_class指定一个自定义的CacheProvider缓存提供者的类名.取值.classname.of.CacheProvider 5.hibernate.cache.provider_configuration_file_resource_pathhibernate缓存文件路径 6.update如果数据库中不存在表则生成,存在如果有增加自动增加,开发使用update,生产一般不设置 --> hibernate.hbm2ddl.auto hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.query.substitutions=true1,false0 hibernate.jdbc.batch_size=20 hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider --hibernate.cache.provider_configuration_file_resource_path 指定缓存文件路径和文件名 --> --事务配置配置事务管理器--> --使用annotation自动注册bean,并检查@Required,@Autowired的属性已被注入 base-package="com.wlb"把com.wlb下的包都纳入管理 --> component-scanbase-package="com.zt.game"/> --使用annotation定义事务采用注解的方式管理如: @Server@Controller@Repository@component--> annotation-driventransaction-manager="txManager"/> 7.在src下面配置struts.properties #struts接收的后缀名 struts.action.extension=action #把struts2交给spring管理,指定Struts2默认的ObjectFactoryBean,该属性默认值是spring。 struts.objectFactory=spring #允许动态方法调用(设为false说明不能使用XX! XX.action方式执行请求) struts.enable.DynamicMethodInvocation=true #是否为strtus开发模式,如果设置该属性为true,则可以在应用出错时显示更多、更友好的出错提示。 struts.devMode=false #默认的国际化地区信息 struts.locale=zh_CN #国际化信息内码 #struts.i18n.encoding=GBK #默认的UItemplate主题,关于struts2页面布局 struts.ui.theme=simple #该属性指定Struts2应用所需要的国际化资源文件,如果有多份国际化资源文件,则多个资源文件的文件名以英文逗号(,)隔开。 #struts.custom.i18n.resources=i18n #该属性指定视图主题所需要模板文件的位置,该属性的默认值是template,即默认加载template路径下的模板文件。 #struts.ui.templateDir=/WEB-INF/template #该属性指定上传文件的临时保存路径,该属性的默认值是javax.servlet.context.tempdir #struts.multipart.saveDir=temp/ #multipart请求信息的最大尺寸(文件上传用) struts.multipart.maxSize=8388608 #该属性设置Struts2是否允许在Action名中使用斜线 #struts.enable.SlashesInActionNames=true 8.在src下面编写struts.xml xmlversion="1.0"encoding="UTF-8"? > DOCTYPEstrutsPUBLIC "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN" "http: //struts.apache.org/dtds/struts-2.0.dtd"> --导入struts-default.xml文件--> 9.新建applicationContext-hibernate.xml xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd http: //www.springframework.org/schema/contexthttp: //www.springframework.org/schema/context/spring-context-2.5.xsd http: //www.springframework.org/schema/aophttp: //www.springframework.org/schema/aop/spring-aop-2.5.xsd http: //www.springframework.org/schema/txhttp: //www.springframework.org/schema/tx/spring-tx-2.5.xsd"> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> hibernate.cfg.xml 10.新建项目中的包结构如下: 10,配置web.xml xmlversion="1.0"encoding="UTF-8"? > xmlns=" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" --struts2过滤器--> --spring容器实例化,声明spring的监听器--> --指定spring配置文件--> classpath: applicationContext.xml 11用到的jar包 包多为生成的,很多可以删除。 12.新建BaseDAO publicclassBaseDAOextendsHibernateDaoSupportimplementsIBaseDAO{ /** *注解sessionFactory */ privateSessionFactorymySessionFacotry; @Resource publicvoidsetMySessionFacotry(SessionFactorysessionFacotry){ this.mySessionFacotry=sessionFacotry; } @PostConstruct publicvoidinjectSessionFactory(){ super.setSessionFactory(mySessionFacotry); } /** *根据实体,字段,值查询 */ publicObjectfindBy(Classclazz,Stringname,Objectvalue){ Criteriacr=getSession().createCriteria(clazz); cr.add(Restrictions.in(name,newObject[]{value})); returncr.list().get(0); } } 实体DAO /** *注解的别名,在service中使用它注解 */ @Repository("memberDao") publicclassMemberDAOextendsBaseDAOimplementsIBaseDAO{ publicvoidsave(PslMembersmember){ super.saveObject(member); } } Service /** *注解service *@authorAdministrator * */ @Service("memberService") @Transactional publicclassMemberServiceimplementsIMemberService{ /** *memberDao为dao里面的别名 */ private@Resource(name="memberDao") MemberDAOmemberDao; publicvoiddelete(PslMembersmember){ //TODOAuto-generatedmethodstub memberDao.delete(member); } } Action publicclassMemberActionextendsBaseAction{ /** *memberService为service里面的别名 */ private@Resource(name="memberService") IMemberServicememberService; /** *添加用户 */ privatestaticfinallongserialVersionUID=1L; publicStringaddMember(){ Stringusername=request.getParameter("username"); } } 注意: 在使用struts2.0的包时候,版本很重要,要保证xwork.jar,struts2.0-core.jar,和struts2-spring-plus.jar的版本同一
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Struts21 Hibernate32 Spring25 注解 搭建