ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法

Posted Advancing Swift

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法相关的知识,希望对你有一定的参考价值。

自动调用Spring的bean.xml配置文件

需要web.xml启动文件

代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>2018-01-03_Spring_Hibernate</display-name>
  
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:bean.xml</param-value>
  </context-param>
  
  <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>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

其中调用了过滤器和监听器

Spring核心配置文件bean.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- c3p0连接池得到dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    
    <bean id="userAction" class="com.swift.action.UserAction" scope="prototype">
    <property name="userService" ref="userService"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <!-- Hibernate核心配置文件没有连接数据库,所以需要注入 -->
    <property name="dataSource" ref="dataSource"></property>
    <!-- Hibernate核心配置文件的位置 -->
    <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    
    <bean id="userService" class="com.swift.service.UserService">
    <property name="userDao" ref="userDaoImplements"></property>
    </bean>
    
    <bean id="userDaoImplements" class="com.swift.dao.UserDaoImplements">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
    
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
       <!-- 前一个sessionFactory是HibernateTemplate类内部的 -->
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    

</beans>

配置文件注入对象属性,注意需要类当中声明属性并设置setter方法

层层调用

UserAction类代码如下:

package com.swift.action;

import com.opensymphony.xwork2.ActionSupport;
import com.swift.service.UserService;

public class UserAction extends ActionSupport {
    private UserService userService;
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @Override
    public String execute() throws Exception {
        System.out.println("action..................");
        userService.add();
        return NONE;
    }

}

UserService类代码如下:

package com.swift.service;

import com.swift.dao.UserDao;

public class UserService {

    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add() {
        System.out.println("UserService.................");
        userDao.add();
    }
    
}

UserDao接口代码如下:

 package com.swift.dao; public interface UserDao { public void add(); } 

UserDao接口的实现类UserDaoImplement代码如下:

package com.swift.dao;

import org.springframework.orm.hibernate5.HibernateTemplate;

import com.swift.entity.User;

public class UserDaoImplements implements UserDao{
    private HibernateTemplate hibernateTemplate;
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public void add() {
        User user=new User();
        user.setUsername("small-fly");
        user.setAddress("War of Mercenaries");
        hibernateTemplate.save(user);
    }

}

hibernate核心配置文件hibernate.cfg.xml文件代码

<?xml version="1.0" encoding=\'UTF-8\'?>
<!DOCTYPE hibernate-configuration PUBLIC   
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sw_database</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property> -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
        
        
        <property name="hibernate.show_sql">true</property>
     <property name="hibernate.format_sql">true</property>
        <!-- create: 先删表,再建表。 create-drop: 启动时建表,退出前删表。 update: 如果表结构不一致,就创建或更新。 
            validate: 启动时验证表结构,如果不致就抛异常。 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        

        <!--指定映射文件,可映射多个映射文件 -->
        <mapping resource="User.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

hibernate映射文件代码如下:

<?xml version="1.0" encoding=\'UTF-8\'?>     
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 实体类映射文件 -->
<hibernate-mapping>

    <class name="com.swift.entity.User" table="t_user">
        <!-- 主键 -->
        <id name="uid">
            <generator class="native"></generator>
        </id>
        <!-- 其他属性 -->
        <property name="username"/>
        <property name="address"/>
    </class>
    
</hibernate-mapping>

运行action出现问题

原因,jar包问题,重新整理OK的SSH jar包合集

下载包地址:

 链接: https://pan.baidu.com/s/1c16Aapa 密码: 4b4d

以上是关于ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法的主要内容,如果未能解决你的问题,请参考以下文章

ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点

(转)hibernate-5.0.7+struts-2.3.24+spring-4.2.4三大框架整合

ssh整合学习

Spring第四天——SSH整合

SSH框架系列之 Spring 整合 Hibernate 框架

SSH程序框架之Spring与HIbernate整合