MyEclipse搭建SSH并实现增删改查

一、            新建一个web项目命名为ssh


MyEclipse搭建SSH并实现增删改查_第1张图片

完成之后目录结构

MyEclipse搭建SSH并实现增删改查_第2张图片

二、            为工程加上Struts2的支持

MyEclipse搭建SSH并实现增删改查_第3张图片

MyEclipse搭建SSH并实现增删改查_第4张图片

然后是选择struts的包,根据需要将struts的包添加进来,一般需要添加的包如下

Struts Core libraries

Struts必须包

StrutsSpring libraries

Spring支持包

MyEclipse搭建SSH并实现增删改查_第5张图片


三、            为工程加上Spring的支持

MyEclipse搭建SSH并实现增删改查_第6张图片

MyEclipse搭建SSH并实现增删改查_第7张图片

如果选择next进入下一步,里面是指定spring配置文件的位置,

一般都不需要修改直接点finish就可以了,这样就使你的工程添加进了spring的支持

四、            为工程加上hibernate的支持

MyEclipse搭建SSH并实现增删改查_第8张图片

MyEclipse搭建SSH并实现增删改查_第9张图片

MyEclipse搭建SSH并实现增删改查_第10张图片

MyEclipse搭建SSH并实现增删改查_第11张图片

MyEclipse搭建SSH并实现增删改查_第12张图片

然后点finish,完成hibernate的配置

在web.xml文件必须添加如下配置


 
  org.springframework.web.context.ContextLoaderListener
 

 
  contextConfigLocation
  classpath:applicationContext.xml
 

运行结果

MyEclipse搭建SSH并实现增删改查_第13张图片


项目结构如下

MyEclipse搭建SSH并实现增删改查_第14张图片

新建一个TestAction

package com.xwd.action;


import java.util.List;
import com.xwd.entity.User;
import com.xwd.service.TestService;


public class TestAction {
private User user;
private TestService testservice;
private List list;

public String execute(){
int save=testservice.add(user);
System.out.println("save:"+save);
list=testservice.findAll(User.class);
return "success";

}

public String delete(){
testservice.delete(User.class,user.getId());
System.out.println("delete:"+1);
//锟斤拷询锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷,锟斤拷示页锟斤拷
list=testservice.findAll(User.class);
return "success";

}

public String update(){
int update = testservice.update(user);
System.out.println("update:"+update);
//锟斤拷询锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷,锟斤拷示页锟斤拷
list = testservice.findAll(User.class);
return "success";

}

public User getUser() {
return user;
}


public void setUser(User user) {
this.user = user;
}


public TestService getTestservice() {
return testservice;
}


public void setTestservice(TestService testservice) {
this.testservice = testservice;
}


public List getList() {
return list;
}


public void setList(List list) {
this.list = list;
}

}

新建一个TestDao

package com.xwd.dao;


import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class TestDao {
private SessionFactory sf;
public int insert(Object o){
try {
Session session=sf.getCurrentSession();
session.save(o);
return 1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}

public int delete(Class cls,Serializable id){
try {
Session session=sf.getCurrentSession();
Object o = session.get(cls,id);
session.delete(o);
return 1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}

public int update(Object o){
try {
Session session=sf.getCurrentSession();
session.update(o);
return 1;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}

public List getAll(Class cls){
Session session=sf.getCurrentSession();
Query query = session.createQuery("from "+cls.getSimpleName());
System.out.println("TestDao getAll cls.getSimpleName()"+cls.getSimpleName());
return query.list();
}

public SessionFactory getSf() {
return sf;
}

public void setSf(SessionFactory sf) {
this.sf = sf;
}

}

新建一个User

package com.xwd.entity;


public class User {
private int id;
private String name;
private int pwd;


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public int getPwd() {
return pwd;
}


public void setPwd(int pwd) {
this.pwd = pwd;
}


public User(int id, String name, int pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}


public User() {
}


@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd=" + pwd +
'}';
}

}

新建一个User.hbm.xml

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

   
       
           
       

       
       
   

新建一个TestService

package com.xwd.service;

import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.xwd.dao.TestDao;
import com.xwd.entity.User;


public class TestService {
private SessionFactory sf;
private TestDao testdao;


public int add(Object o){//添加
System.out.println("我在TestService类中,添加的方法运行了"+o);
return testdao.insert(o);
}

public int delete(Class cls,Serializable id){//删除
System.out.println("我在TestService类中,删除的方法运行了");
return testdao.delete(cls, id);
}

public int update(Object o){//修改
System.out.println("我在TestService类中,修改的方法运行了"+o);
return testdao.update(o);
}
public List findAll(Class cls){//查询
System.out.println("我在TestService类中,查询的方法运行了");
return testdao.getAll(cls);
}
public SessionFactory getSf() {
return sf;
}
public void setSf(SessionFactory sf) {
this.sf = sf;
}
public TestDao getTestdao() {
return testdao;
}

public void setTestdao(TestDao testdao) {
this.testdao = testdao;
}

}

applicationContext.xml配置如下


xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context.xsd">














   
     
   
    true
    org.hibernate.dialect.SQLServerDialect
    update
   

   

   


com/xwd/entity/User.hbm.xml













































Struts.xml配置如下


"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">



/list.jsp




源码下载  点击打开链接https://download.csdn.net/download/sinat_37001576/10375278

你可能感兴趣的:(JAVA)