Eclipse搭建SSH框架(Struts2+Spring+Hibernate)

时间:2021-11-11 07:20:52

见识少的我经过一天多的研究才知道,在MyEclipse中搭好的框架的配置文件和jar包是通用的。接下来——亮剑!

工具:Eclipse+Tomcat+Mysql

一、先在Eclipse中配置好Tomcat服务器,新建Web工程,建立目录包结构。这些不详细说了。

Eclipse搭建SSH框架(Struts2+Spring+Hibernate)

二、添加ssh所需要的jar包(有点多)。

Eclipse搭建SSH框架(Struts2+Spring+Hibernate)

Eclipse搭建SSH框架(Struts2+Spring+Hibernate)

Eclipse搭建SSH框架(Struts2+Spring+Hibernate)

三、修改web.xml文件和添加struts.xml,application.xml配置文件。

  1.web.xml()

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssh</display-name> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- spring启动文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- spring监听器的作用:提供实例 -->
<!-- spring的监听器配置开始 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<!-- spring的监听器配置结束 -->
<!-- END SNIPPET: filter -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping> </web-app>

  2.struts.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd"><!-- 注意版本 -->
<!-- 这里的包名和命名空间可随便起,但要有意义,访问顺序是先找到包名-命名空间名找到动作类 --> <struts>
<package name="user" namespace="/user" extends="struts-default">
<action name="UserAction" class="action.UserAction" >
<result name="success">/index2.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>

  3.application.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" default-autowire="byName"> <context:annotation-config />
<!-- spring 扫描路径,注意当前工程只需要扫描dao和service,srpingmvc或者struts2注解才有变化 -->
<context:component-scan base-package="dao,service" /> <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/young">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:entity/</value>
</list>
</property>
</bean>
<!-- 配置声明式事务管理(采用注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>

四、编写各个包下的代码,和修改struts.xml以及application.xml文件,一般是改路径就可以。这里的数据库映射文件和实体类是可以生成的,百度搜索“Eclpse中生成ssh框架中的实体类”,要安装一个Hibernate的插件。以及配置数据库连接。

common包下代码:

package common;

import java.util.List;

public interface BaseDao<T> {
public List<T> findAll();
public int insert(T t);
public int update (T t,int id);
public int delete(int id);
public T findById(int id);
}
package common;

import java.util.List;

public interface BaseService <T>{
public List<T> findAll();
public boolean insert(T t);
public boolean update (T t,int id);
public boolean delete(int id);
public T findById(int id);
}

dao包下代码:

package dao;

import common.BaseDao;
import entity.User; public interface UserDao extends BaseDao<User>{ }
package dao.impl;

import java.util.List;

import javax.websocket.Session;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository; import dao.UserDao;
import entity.User;
@Repository
public class UserDaoimpl extends HibernateDaoSupport implements UserDao { @Override
public List<User> findAll() {
// TODO Auto-generated method stub
String sql="select * from user";
Query query=getSession().createSQLQuery(sql).addEntity(User.class);
List<User> list=query.list();
return list ;
} @Override
public int insert(User t) {
// TODO Auto-generated method stub
return 0;
} @Override
public int update(User t, int id) {
// TODO Auto-generated method stub
return 0;
} @Override
public int delete(int id) {
// TODO Auto-generated method stub
return 0;
} @Override
public User findById(int id) {
// TODO Auto-generated method stub
return null;
} }

service包下代码

package service;

import common.BaseDao;
import entity.User; public interface UserService extends BaseDao<User>{ }
package service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import dao.UserDao;
import entity.User;
import service.UserService;
@Transactional
@Service
public class UserServiceimpl implements UserService{
@Autowired
private UserDao userdao;
@Override
public List<User> findAll() {
// TODO Auto-generated method stub
return userdao.findAll();
} @Override
public int insert(User t) {
// TODO Auto-generated method stub
return 0;
} @Override
public int update(User t, int id) {
// TODO Auto-generated method stub
return 0;
} @Override
public int delete(int id) {
// TODO Auto-generated method stub
return 0;
} @Override
public User findById(int id) {
// TODO Auto-generated method stub
return null;
} }

entity包代码:

package entity;
// Generated 2018-3-29 10:41:32 by Hibernate Tools 3.4.0.CR1 /**
* User generated by hbm2java
*/
public class User implements java.io.Serializable { private Integer userid;
private String userName;
private String pwd; public User() {
} public User(String userName, String pwd) {
this.userName = userName;
this.pwd = pwd;
} public Integer getUserid() {
return this.userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public String getUserName() {
return this.userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPwd() {
return this.pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} }
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-3-29 10:41:32 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="entity.User" table="user" catalog="young">
<id name="userid" type="java.lang.Integer">
<column name="userid" />
<generator class="identity" />
</id>
<property name="userName" type="string">
<column name="userName" length="10" />
</property>
<property name="pwd" type="string">
<column name="pwd" length="10" />
</property>
</class>
</hibernate-mapping>

action包代码:

package action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import entity.User;
import service.UserService;
@Controller
public class UserAction extends ActionSupport{
@Autowired
private UserService userService;//自动注入 public String toLogin(){
List<User>list=userService.findAll();
for (User user : list) {
System.out.println(user.getUserName());
} return "success";
} }

最后运行大功告成,结果图如下:

Eclipse搭建SSH框架(Struts2+Spring+Hibernate)