首頁 > 軟體

idea搭建ssh框架的超詳細教學

2022-03-16 10:01:09

一。建立專案

先附上測試的資料庫

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>
      <url-pattern>/*</url-pattern>
  </filter-mapping> -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <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>
    <bean id="userDao" class="com.well.liu.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    <bean id="test" class="com.well.liu.action.TestAction">
        <property name="userService" ref="userService"></property>
    <bean id="userService" class="com.well.liu.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    <!-- 定義事務通知 -->
    <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,出現地址則成功

到此這篇關於idea搭建ssh框架(超詳細)的文章就介紹到這了,更多相關idea搭建ssh框架內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com