MyEclipse搭建SSH并实现增删改查

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_37001576/article/details/80089526

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


完成之后目录结构


二、            为工程加上Struts2的支持


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

Struts Core libraries

Struts必须包

StrutsSpring libraries

Spring支持包



三、            为工程加上Spring的支持


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

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

四、            为工程加上hibernate的支持



然后点finish,完成hibernate的配置

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

<!-- spring监听器 -->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

运行结果



项目结构如下


新建一个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

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.xwd.entity.User" table="user">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="pwd"/>
    </class>

</hibernate-mapping>

新建一个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配置如下

<?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: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">




<!-- 创建数据源对象 -->
<!-- 初始化数据源:数据库的连接信息:用户名,密码,url,driverClass -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>

<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="ds"></property>
    <property name="hibernateProperties"> 
    <props>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    <!-- 映射文件的位置 -->
<property name="mappingResources">
<list>
<value>com/xwd/entity/User.hbm.xml</value>
</list>
</property>
</bean>



<bean id="td" class="com.xwd.dao.TestDao">
<property name="sf" ref="sf"></property>
</bean>

<bean id="ts" class="com.xwd.service.TestService">
<property name="testdao" ref="td"></property>
</bean>

<bean id="ta" class="com.xwd.action.TestAction">
<property name="testservice" ref="ts"></property>
</bean>




<!-- 配置声明事务 -->
<!-- 创建事务管理器对象 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf"></property>
</bean>


<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false"/>
<tx:method name="remove*" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" propagation="REQUIRED" read-only="false"/>
<tx:method name="getAll*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--通过aop把代码卡在方法的前后 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xwd.dao.*.*(..))"/>
</aop:config>

</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" extends="struts-default">
<action name="testaction_*" class="ta" method="{1}">
<result name="success">/list.jsp</result>
</action>
</package>
</struts>

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

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页