SSH整合:Spring5.2.2+Struts2.5.22+Hibernate5.8

SSH整合:Spring5.2.2+Struts2.5.22+Hibernate5.8

一、环境搭建

数据库:MySQL8
JDK:13

jar包

  • Spring
    SSH整合:Spring5.2.2+Struts2.5.22+Hibernate5.8_第1张图片
  • Struts
    SSH整合:Spring5.2.2+Struts2.5.22+Hibernate5.8_第2张图片
  • Hibernate
    SSH整合:Spring5.2.2+Struts2.5.22+Hibernate5.8_第3张图片
  • Spring整合Hibernate
    在这里插入图片描述
  • Struts整合Spring
    在这里插入图片描述
  • -C3p0
    在这里插入图片描述
  • 连接数据库
    在这里插入图片描述
  • 在JDK8以上使用Hibernate需导入
    在这里插入图片描述

二、Spring整合Hibernate,带有hibernate.cfg.xml的案例编写

1. 建表,创建映射类,配置映射类配置文件文件



<hibernate-mapping>
	
    <class name="映射类的带包的全类名" table="与类对应的数据库中表的名字">
        <id name="id" column="字段名">
            <generator class="native">generator>
        id>
        <property name="普通属性" column="字段名">property>
    class>
hibernate-mapping>

2. dao层

public class UserDaoImpl implements UserDao {
	//使用HibernateTemplate模板
    private HibernateTemplate hibernateTemplate;
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
    @Override
    public void save(User user) {
        this.hibernateTemplate.save(user);
    }
}

3.Service层

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    public void save(User user) {
        userDao.save(user);
    }
}

4.Hibernate配置文件hibernate.cfg.xml



<hibernate-configuration>
    <session-factory>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
        
        <property name="hibernate.show_sql">trueproperty>
        <property name="hibernate.format_sql">trueproperty>
        
        <mapping resource="spring_hibernate/User.hbm.xml">mapping>
    session-factory>
hibernate-configuration>

5.Spring配置文件applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
	
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver">property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC">property>
        <property name="user" value="root">property>
        <property name="password" value="123">property>
    bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        <property name="configLocation" value="classpath:spring_hibernate/hibernate.cfg.xml">property>
    bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>
    
    <bean id="userDao" class="spring_hibernate.dao.impl.UserDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate">property>
    bean>
    
    <bean id="userService" class="spring_hibernate.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao">property>
    bean>
    
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
        
        <tx:attributes>
            <tx:method name="save"/>
        tx:attributes>
    tx:advice>
    
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* spring_hibernate.service..*.*(..))">aop:advisor>
    aop:config>
beans>

6.测试类

//Spring整合JUnit
@RunWith(SpringJUnit4ClassRunner.class)
//加载applicationContext.xml
@ContextConfiguration(locations = "classpath:spring_hibernate/applicationContext.xml")
public class testApp {
    @Autowired
    private UserService userService;
    @Test
    public void test(){
        User user = new User();
        user.setUsername("Jack");
        user.setPassword("123");
        user.setAge(12);
        userService.save(user);
    }
}

三、Spring整合Hibernate,不带有hibernate.cfg.xml以及dao层继承HibernateDaoSupport的案例编写

1.修改dao层

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    @Override
    public void save(User user) {
        //从父类HibernateDaoSupport中获取HibernateTemplate
        this.getHibernateTemplate().save(user);
    }
}

2.删除hibernate.cfg.xml,将hibernate的一些配置配置到applicationContext.xml中


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver">property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC">property>
        <property name="user" value="root">property>
        <property name="password" value="123">property>
    bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.format_sql">trueprop>
            props>
        property>
        
        <property name="mappingLocations" value="classpath:spring_hibernate/*/User.hbm.xml">property>
    bean>
    
    
    <bean id="userDao" class="spring_hibernate.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>
    
    <bean id="userService" class="spring_hibernate.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao">property>
    bean>
    
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        
        
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>
    
    <tx:advice id="txAdvice" transaction-manager="txManager">
        
        <tx:attributes>
            <tx:method name="save"/>
        tx:attributes>
    tx:advice>
    
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* spring_hibernate.service..*.*(..))">aop:advisor>
    aop:config>
beans>

四、Struts整合Spring

1.创建Action

public class UserAction extends ActionSupport implements ModelDriven<User> {
	//模型驱动封装数据
    User user = new User();
    @Override
    public User getModel() {
        return user;
    }
	//Spring动态注入userService
    private UserService userService;
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public String save(){
        userService.save(user);
        return "success";
    }
}

2.在applicationContext中配置Action的bean

<bean id="userAction" class="spring_hibernate.action.UserAction">
        <property name="userService" ref="userService">property>
bean>

3.配置struts.xml



<struts>
    
    <package name="default" namespace="/" extends="struts-default">
       	
        <global-allowed-methods>regex:.*global-allowed-methods>
        
        <action name="userAction_*" class="userAction" method="{1}">
            <result>/success.jspresult>
        action>
    package>
struts>

注意:如果applicationContext.xml中的userService的id和Action中Userservice的属性同名的话,第2步可以省略,将第3步中action的class改为全限定类名即可

ApplicationContext.xmlSSH整合:Spring5.2.2+Struts2.5.22+Hibernate5.8_第4张图片因此修改struts.xml
在这里插入图片描述

4.编写index.jsp

姓名: 密码: 年龄:

你可能感兴趣的:(SSH)