1. 程式人生 > >MyEclipse搭建SSH並實現增刪改查

MyEclipse搭建SSH並實現增刪改查

一、            新建一個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