ssh mysql环境搭建_SSH2环境搭建

一、创建SSH2项目,导入Struts2并测试。

6c87668add0f47b6e4f599b1cbca5c76.png

f40cbe2fe19007d841e3ce8616592b75.png

7afb802d6680f678addd0f64315ced6d.png

32ba2b4cf10bca1d2486e3c4ae5b2582.png

cb62ef6ea566da8e9f33847e59e6b032.png

web.xml

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

index.jsp

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

*.action

default.jsp

default page

UserAction.java

packagecom.cwq.action;importcom.opensymphony.xwork2.ActionSupport;public class UserAction extendsActionSupport {

@Overridepublic String execute() throwsException {//TODO Auto-generated method stub//return super.execute();

returnSUCCESS;

}/***@paramargs*/

public static voidmain(String[] args) {//TODO Auto-generated method stub

}

}

struts.xml

default.jsp

发布项目,启动Tomcat,运行结果:

597b60213f86da57065ed945993d8d94.png

二、导入Spring包,配置数据源

c0c6520c664801b090eb887b7d3ecd70.png

1888227eb564ee39df85dbac8022fccb.png

4199537e0db0748eb949593080272d0c.png

0de81a143a43dff7de5be4ed130d65b8.png

4e6c4621e25d35b4afebc01c6dc7d951.png

52295710762e881b63a302910cb211ff.png

085a6487f4850ec37fb5b9dc14ab08f2.png

3b2a3069b7788a6dae4be1820ac5467c.png

9a877d816ccf8042f04d25653689adf1.png

三,Spring与Hibernate整合

2f0988e4b0c07be0b2185925dc33e797.png

07b865df17e487e22d91fc3347425d0f.png

1e90234e54e81a1a727441000ba3746b.png

373c093d4e9f06c31b2c523c7ea2569d.png

f02c09b519501b590a93a4580ec2cb95.png

074501d43bba646ff10ec019ca9024a6.png

064a6f25ce17e00663c7029e457f8984.png

在web.xml中加入

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

index.jsp

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

*.action

applicationContext.xml(自动生成)

org.hibernate.dialect.MySQLDialect

测试,发布,启动Tomcat,如果启动成功则整合成功。

四、根据数据库反向生成代码

e874cb2f1b26bdedf5f5bc0aa22fb7bd.png

6a6d8519f23dfaefd286d9cad88ccef5.png

37e0eb89358ba54a4256973501965bb9.png

1aed429abf9018a5c88eac2f2c50ead0.png

4fd303002f2d5f5d61ac968419f93bbe.png

Users.java

packagecom.cwq.model;importjava.sql.Timestamp;/*** Users entity.@authorMyEclipse Persistence Tools*/

public class Users implementsjava.io.Serializable {//Fields

privateInteger id;privateString username;privateString password;privateTimestamp birthdate;//Constructors

/**default constructor*/

publicUsers() {}/**full constructor*/

publicUsers(String username, String password, Timestamp birthdate) {this.username =username;this.password =password;this.birthdate =birthdate;

}//Property accessors

publicInteger getId() {return this.id;

}public voidsetId(Integer id) {this.id =id;

}publicString getUsername() {return this.username;

}public voidsetUsername(String username) {this.username =username;

}publicString getPassword() {return this.password;

}public voidsetPassword(String password) {this.password =password;

}publicTimestamp getBirthdate() {return this.birthdate;

}public voidsetBirthdate(Timestamp birthdate) {this.birthdate =birthdate;

}

}

user.hbm.xml

/p>

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

UserDao.java

packagecom.cwq.dao;importjava.sql.Timestamp;importjava.util.List;importorg.hibernate.LockMode;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.context.ApplicationContext;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importcom.cwq.model.Users;/*** A data access object (DAO) providing persistence and search support for Users

* entities. Transaction control of the save(), update() and delete() operations

* can directly support Spring container-managed transactions or they can be

* augmented to handle user-managed Spring transactions. Each of these methods

* provides additional information for how to configure it for the desired type

* of transaction control.

*@seecom.cwq.model.Users

*@authorMyEclipse Persistence Tools*/

public class UsersDAO extendsHibernateDaoSupport {private static final Logger log = LoggerFactory.getLogger(UsersDAO.class);//property constants

public static final String USERNAME = "username";public static final String PASSWORD = "password";protected voidinitDao() {//do nothing

}public voidsave(Users transientInstance) {

log.debug("saving Users instance");try{

getHibernateTemplate().save(transientInstance);

log.debug("save successful");

}catch(RuntimeException re) {

log.error("save failed", re);throwre;

}

}public voiddelete(Users persistentInstance) {

log.debug("deleting Users instance");try{

getHibernateTemplate().delete(persistentInstance);

log.debug("delete successful");

}catch(RuntimeException re) {

log.error("delete failed", re);throwre;

}

}publicUsers findById(java.lang.Integer id) {

log.debug("getting Users instance with id: " +id);try{

Users instance= (Users) getHibernateTemplate().get("com.cwq.model.Users", id);returninstance;

}catch(RuntimeException re) {

log.error("get failed", re);throwre;

}

}publicList findByExample(Users instance) {

log.debug("finding Users instance by example");try{

List results=getHibernateTemplate().findByExample(instance);

log.debug("find by example successful, result size: " +results.size());returnresults;

}catch(RuntimeException re) {

log.error("find by example failed", re);throwre;

}

}publicList findByProperty(String propertyName, Object value) {

log.debug("finding Users instance with property: " + propertyName + ", value: " +value);try{

String queryString= "from Users as model where model." + propertyName + "= ?";returngetHibernateTemplate().find(queryString, value);

}catch(RuntimeException re) {

log.error("find by property name failed", re);throwre;

}

}publicList findByUsername(Object username) {returnfindByProperty(USERNAME, username);

}publicList findByPassword(Object password) {returnfindByProperty(PASSWORD, password);

}publicList findAll() {

log.debug("finding all Users instances");try{

String queryString= "from Users";returngetHibernateTemplate().find(queryString);

}catch(RuntimeException re) {

log.error("find all failed", re);throwre;

}

}publicUsers merge(Users detachedInstance) {

log.debug("merging Users instance");try{

Users result=(Users) getHibernateTemplate().merge(detachedInstance);

log.debug("merge successful");returnresult;

}catch(RuntimeException re) {

log.error("merge failed", re);throwre;

}

}public voidattachDirty(Users instance) {

log.debug("attaching dirty Users instance");try{

getHibernateTemplate().saveOrUpdate(instance);

log.debug("attach successful");

}catch(RuntimeException re) {

log.error("attach failed", re);throwre;

}

}public voidattachClean(Users instance) {

log.debug("attaching clean Users instance");try{

getHibernateTemplate().lock(instance, LockMode.NONE);

log.debug("attach successful");

}catch(RuntimeException re) {

log.error("attach failed", re);throwre;

}

}public staticUsersDAO getFromApplicationContext(ApplicationContext ctx) {return (UsersDAO) ctx.getBean("UsersDAO");

}

}

applicationContext.xml

org.hibernate.dialect.MySQLDialect

com/cwq/model/Users.hbm.xml

五、Struts整合Spring

UserAction.java

packagecom.cwq.action;importjava.util.List;importcom.cwq.dao.UsersDAO;importcom.cwq.model.Users;importcom.opensymphony.xwork2.ActionSupport;public class UserAction extendsActionSupport {private List users = null;privateUsersDAO userDao;public voidsetUserDao(UsersDAO userDao) {this.userDao =userDao;

}

@Overridepublic String execute() throwsException {//TODO Auto-generated method stub//return super.execute();

users =userDao.findAll();returnSUCCESS;

}public ListgetUsers() {returnusers;

}public void setUsers(Listusers) {this.users =users;

}/***@paramargs*/

public static voidmain(String[] args) {//TODO Auto-generated method stub

}

}

applicationContext.xml

加入

org.hibernate.dialect.MySQLDialect

com/cwq/model/Users.hbm.xml

struts.xml

加入

default.jsp

default.jsp

default page

${user.username}--${user.password}--${user.birthdate}

e6c2807fcfd11abee1479055f67bee18.png

如果要指定进入某个action加入下面代码

document.location.href = "showUsers.action";

index.jsp

Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

">

My JSP 'index.jsp' starting page

document.location.href= "showUsers.action";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值