idea搭建ssh框架(超详细)

一。创建项目

先附上测试的数据库

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(10) COLLATE utf8_bin DEFAULT NULL,
  `remark` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', null);
INSERT INTO `user` VALUES ('2', 'lisi', '123', null);
INSERT INTO `user` VALUES ('3', 'wangan', '456', null);
INSERT INTO `user` VALUES ('4', 'xinxi', '000', null);

1.new->project出现如下

点击next后出现如下填写GroupId和ArtifactId在点击next直至finish

2.构建目录结构

在main下新建java和resources目录如下并将java目录标记为Sources Root,resources标记为Resources Root

 

在java下新建如下包package

二。Struts2

1.首先引入struts2依赖

注:如果引入所有(spring和hibernate)依赖则在struts.xml中的对应action的类class就会找不到

         <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.5.1</version>
            <scope>compile</scope>
        </dependency>
               

2.WEB-INF下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>Archetype Created Web Application</display-name>

  <!--Hibernate Open Session in View Filte -->
  <!-- <filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
      singleSession默认为true,若设为false则等于没用OpenSessionInView
      <init-param>
          <param-name>singleSession</param-name>
          <param-value>true</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>hibernateFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping> -->

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

3.在resources下添加struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Struts。xml的配置文件 -->
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action,," />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />

<package name="default" extends="struts-default" namespace="/">
<!-- 如果不指定method,那么默认方法是execute-->

          <!--name:Action类的名字,具有唯一性,页面上通过name来访问Action类 -->

        <action name="test" class="com.well.liu.action.TestAction" method="test">

              <!-- 配置结果集 :name属性的值一定要与execute方法的返回值一致-->

            <result name="test">test.jsp</result>
</action>

</package>
</struts>

4.在action包下创建TestAction类,为了测试在webapp下新建了test.jsp页面

package com.well.liu.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

    public String test(){
        System.out.println("测试struts2");
        return "test";
    }

}

5.部署

点击如下图

点击+号找到Local

接下来修改名字,选择Deployment点击+号,选择ssh:war exploded,另外Application context填写的如果是ssh,那么访问的时候就是localhost:8080/ssh,部署结束。

6.启动测试

启动后默认访问的是index.jsp页面,可以访问http://localhost:8080/ssh/test

如果页面乱码在头部加上<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

 三。Spring

1.首先引入spring相关依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.5.1</version>
        </dependency>

2.在web.xml中添加listener,并在resources下新建spring,xml文件

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

spring.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    
    <bean id="test" class="com.well.liu.action.TestAction">

    </bean>

</beans>

如果你的spring.xml文件uri地址出错,解决点击菜单栏“File”→“Settings”→“Languages&Frameworks”

将之前的struts.xml中的<action name="test" class="com.well.liu.action.TestAction" method="test">class改为"test",也就是说与spring.xml中bean的id相对应,然后运行即可

四。Hibernate

1.引入hibernate依赖

         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.5.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

2.View--》Tool windows---->Database

找到所需连接的数据库

依次填写如下即可

右键点击项目添加support支持

找到Hibernate并且打勾添加 import database schema打勾 含义:导入数据库模式

填写如下信息(数据源、实体生成包model位置、勾选需要生成的实体、勾选右下角最下面的两个按钮)即可生成实体和映射文件

3.编写代码

spring.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/user?characterEncoding=utf-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!--SessionFactory配置-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                <value>com/well/liu/model/UserEntity.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <bean id="userDao" class="com.well.liu.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="test" class="com.well.liu.action.TestAction">
        <property name="userService" ref="userService"></property>
    </bean>

    <bean id="userService" class="com.well.liu.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 定义事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定义方法的过滤规则 -->
        <tx:attributes>
            <!-- 所有方法都使用事务 -->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 定义AOP配置 -->
    <aop:config>
        <!-- 定义一个切入点 -->
        <aop:pointcut expression="execution (* com.well.liu.service.*.*(..))" id="services"/>
        <!-- 对切入点和事务的通知,进行适配 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="services"/>
    </aop:config>

</beans>

在包dao下新建UserDao

package com.well.liu.dao;

import com.well.liu.model.UserEntity;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UserDao extends HibernateDaoSupport {

    public UserEntity getUser(int id){
        return this.getHibernateTemplate().get(UserEntity.class,id);
    }

}

在包service下新建UserService

package com.well.liu.service;

import com.well.liu.model.UserEntity;

public interface UserService {

    public UserEntity getUser(int id);

}

在包impl下新建UserServiceImpl

package com.well.liu.service.impl;

import com.well.liu.dao.UserDao;
import com.well.liu.model.UserEntity;
import com.well.liu.service.UserService;


public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public UserEntity getUser(int id) {
        UserEntity userEntity = userDao.getUser(id);
        return userEntity;
    }
}

TestAction类

package com.well.liu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.well.liu.model.UserEntity;
import com.well.liu.service.UserService;

public class TestAction extends ActionSupport {

    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String test(){
        UserEntity userEntity = userService.getUser(1);
        System.out.println(userEntity);
        return "test";
    }

}

启动后会报如下错误

 

 在网上找了资料将classpath改成classpath*、在pom中增加配置都还是同样的问题

最后将UserEntity.hbm.xml移动到resources下启动成功后后访问http://localhost:8080/ssh/test,出现地址则成功

 

posted on 2019-05-28 11:18  Java乐享  阅读(25975)  评论(4编辑  收藏  举报

导航