到你了

该我上场表演啦!

导航

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

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

工具:Eclipse+Tomcat+Mysql

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

 

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

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

  1.web.xml()

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3   <display-name>ssh</display-name>
 4     
 5      <welcome-file-list>
 6         <welcome-file>index.jsp</welcome-file>
 7       </welcome-file-list>
 8     
 9   
10    <!-- spring启动文件路径 -->
11     <context-param>  
12         <param-name>contextConfigLocation</param-name>  
13         <param-value>classpath:applicationContext.xml</param-value>  
14     </context-param>
15 
16       <!-- spring监听器的作用:提供实例 -->
17       <!-- spring的监听器配置开始 -->
18     <listener>
19         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
20     </listener>
21   
22      <filter>
23           <filter-name>struts2</filter-name>
24           <filter-class>
25               org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
26           </filter-class>
27       </filter>
28         <!-- spring的监听器配置结束 -->
29     <!-- END SNIPPET: filter -->
30      <filter-mapping>
31       <filter-name>struts2</filter-name>
32       <url-pattern>*.action</url-pattern>
33   </filter-mapping>    
34   
35 </web-app>
View Code

  2.struts.xml

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

  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>
View Code

四、编写各个包下的代码,和修改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);
}
View Code
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);
}
View Code

dao包下代码:

package dao;

import common.BaseDao;
import entity.User;

public interface UserDao extends BaseDao<User>{

}
View Code
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;
    }
    

}
View Code

service包下代码

package service;

import common.BaseDao;
import entity.User;

public interface UserService extends BaseDao<User>{

}
View Code
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;
    }

}
View Code

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;
    }

}
View Code
<?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>
View Code

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";
    }
    
}
View Code

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

 

posted on 2018-03-30 11:59  到你了  阅读(291)  评论(0编辑  收藏  举报