如何搭建ssh项目(基于注解)
2015-09-17 22:21
489 查看
在JavaEE开发中会使用到struts+hibernate+spring框架组合起来搭建web应用程序,在这里讲一下如何搭建ssh项目
第一步,下载jar包,struts 、 hibernate 、spring
第二步,分别将以上的jar包及mysql驱动jar包及c3p0jar包放到lib目录下,并build path
第三步,配置web.xml
第四步,配置db.properties
第五步,新建applicationContext.xml
第六步,新建struts.xml
第七步,新建包com.dobeyue.ssh.entities,新建User 类文件
最后,运行该应用程序,在数据库中生成users表,即为成功
第一步,下载jar包,struts 、 hibernate 、spring
第二步,分别将以上的jar包及mysql驱动jar包及c3p0jar包放到lib目录下,并build path
第三步,配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
第四步,配置db.properties
user=root password= jdbcUrl=jdbc:mysql:///ssh driverClass=com.mysql.jdbc.Driver
第五步,新建applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:user="${user}" p:password="${password}" p:jdbcUrl="${jdbcUrl}" p:driverClass="${driverClass}"/> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.dobeyue.ssh.entities</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 使用注解配置事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置切面 --> <aop:aspectj-autoproxy/> <!-- 配置扫描的包 --> <context:component-scan base-package="com.dobeyue.ssh"/> </beans>
第六步,新建struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> </package> </struts>
第七步,新建包com.dobeyue.ssh.entities,新建User 类文件
package com.dobeyue.ssh.entities; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.springframework.stereotype.Component; @Component @Table(name="users") @Entity public class User { private Integer id; private String userName; @GeneratedValue(strategy=GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="userName",columnDefinition="varchar(255) default 'dobeyue'") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
最后,运行该应用程序,在数据库中生成users表,即为成功
相关文章推荐
- [Android Pro] sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别
- ViewPager欢迎界面
- 简易摄影棚
- 面向对象 第八课 接口
- Android第八讲——多媒体(三) 录制音频MediaRecorder
- Path,贝塞尔曲线,BitMap(位图), PorterDuffXfermode
- 关于C语言中数据类型的转化方式
- python--set
- Jmeter调试工具---Debug Sampler
- ES6详解四: WeakMap
- 面向对象 第六课 类的高级概念
- Android中Activity四种启动模式和taskAffinity属性详解
- zoj3829Known Notation 贪心
- 1.6-vsftp部署和优化-2
- Mac上用虚拟机安装windows系统
- 杭电5122 K.Bro Sorting
- 互联网将导致21世纪脑科学重大突破
- View中消息传递:onTouch() +onInterceptTouchEvent() + GestureDetector 学习
- Spring MVC 中的 Controller
- 第五讲:OpenGL坐标系和UIKit坐标系、锚点、addChild函数详解