首页 > 编程语言 >java8 idea+struts2+spring+hiberate

java8 idea+struts2+spring+hiberate

时间:2022-09-26 16:59:47浏览次数:52  
标签:return name spring UserEntity public struts2 hiberate org example

前置

  • idea
  • jdk 8u202
  • tomcat 8.5.82

创建项目

file -> new -> project -> maven arthetype->webapp

pom.xml

struct2 dependency
点击查看代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>ssh</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ssh Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>


    <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>
  </dependencies>
  <build>
    <finalName>ssh</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>6</source>
          <target>6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

web.xml

web.xml struct2
点击查看代码
<?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>

    <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>
``
</details>

### struts.xml
######  resources/struts.xml
<details>
<summary>点击查看代码</summary>

<package name="default" extends="struts-default" namespace="/">
    <action name="test" class="org.example.action.TestAction" method="test">
        <!-- 配置结果集 :name属性的值一定要与execute方法的返回值一致-->
        <result name="test">test.jsp</result>
    </action>
</package>

配置tomcat

  • idea 右侧上的 edit configuration -> add configuration -> Tomcat server -> loacal -> deloyment-> application context -> /ssh
  • idea 右侧上的 edit configuration -> add configuration -> Tomcat server -> loacal -> deloyment-> deloyment as the server startup-> + -> arfiface->ssh:war exploded
测试路由
file org.example.action.TestAction
method : test
点击查看代码
org.example.action;

import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
  public String test(){
      System.out.println("测试struts2");
      return "test";
  }
}
###### webapp/test.jsp
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
 <h1>test.jsp</h1>
</body>

添加spring

pom.xml dependency
点击查看代码
  <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>

    <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>
###### web.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>
###### 新建 resources/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 classpath:/org/springframework/beans/factory/xml/spring-beans-2.5.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 classpath:/org/springframework/transaction/config/spring-tx-2.5.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/test?characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--SessionFactory配置-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
          destroy-method="destroy">
        <property name="dataSource" ref="dataSource"/>

        <property name="mappingResources">
            <list>
<!--                <value>classpath:user.hbm.xml</value>-->
                <value>user.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>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.format_sql.auto">true</prop>
            </props>
        </property>
    </bean>

    <bean id="userDao" class="org.example.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="test" class="org.example.action.TestAction">
        <property name="userService" ref="userService"></property>
    </bean>

    <bean id="userService" class="org.example.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>
###### 新建 resources/user.hbm.xml
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.example.model">
    <class name="org.example.model.UserEntity" table="user">
        <id name="id" column="ID" type="integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
        <property name="password" column="password" type="string"/>
        <property name="remark" column="remark" type="string"/>
    </class>
</hibernate-mapping>
新建org.example.model.UserEntity
  • idea-> 项目右键->add frameworks supperts-> import database schema -> 选择对应的数据表-> mdoel mapping-> ok
  • 手工处理
点击查看代码
package org.example.model;

import javax.persistence.*;

@Entity
@Table(name = "user", schema = "test", catalog = "")
public class UserEntity {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id")
    private int id;
    @Basic
    @Column(name = "name")
    private String name;
    @Basic
    @Column(name = "password")
    private String password;
    @Basic
    @Column(name = "remark")
    private String remark;

    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 String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserEntity that = (UserEntity) o;

        if (id != that.id) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        if (remark != null ? !remark.equals(that.remark) : that.remark != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (remark != null ? remark.hashCode() : 0);
        return result;
    }
}

###### org.example.service.UserService
点击查看代码
package org.example.service;

import org.example.model.UserEntity;

public interface UserService {

    public UserEntity getUser(int id);

}
##### org.example.service.impl.UserServiceImpl
点击查看代码
package org.example.service.impl;

import org.example.dao.UserDao;
import org.example.model.UserEntity;
import org.example.service.UserService;


public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

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

    public UserEntity getUser(int id) {
        UserEntity userEntity = userDao.getUser(id);
        return userEntity;
    }
}
org.example.dao.UserDao
点击查看代码
package org.example.dao;

import org.example.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);
    }

}
修改mainaction
点击查看代码
package org.example.action;

import com.opensymphony.xwork2.ActionSupport;
import org.example.model.UserEntity;
import org.example.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";
}

测试

浏览器输入 lcoalhost:8010/ssh/test

标签:return,name,spring,UserEntity,public,struts2,hiberate,org,example
From: https://www.cnblogs.com/guanchaoguo/p/16731501.html

相关文章