Eclipse中搭建SSH环境

工具:eclipse+mysql

SSH版本:struts-2.3.16.1   spring-framework-4.2.2.RELEASE   hibernate-release-5.2.2.Final 

本人观看视频并按照视频步骤后写出的,视频链接:点击打开链接,侵权必删

一、导入jar包

新建web项目“ssh”,默认点击“下一步”,在最后勾选“web.xml”复选框

在src目录中新建四个包,com.ssh.action,com.ssh.dao,com.ssh.entity,com.ssh.service

百度“SSH整合jar包”下载,将其导入“lib”文件夹中

二、搭建Struts2

在com.ssh.action包中新建“UserAction.java”

public class UserAction extends ActionSupport {
	
	public String login() {
		return "login";
	}
}

在src目录下新建Struts2核心配置文件“struts.xml”,并配置“UserAction.java”

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	
	<package name="default" extends="struts-default" >
		
		<action name="user_login" class="com.ssh.action.UserAction" method="login">
			<result name="login">/login.jsp</result>
		</action>
	
	</package>


</struts>

在“web.xml”中配置struts2过滤器

<?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">
  
   <!-- struts过滤器 -->
  <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>
  
  <display-name>ssh</display-name>
  <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>

struts2搭建完成

三、搭建Hibernate

在com.ssh.entity中创建实体类“User.java”

public class User {
	
	private int uid;
	private String username;
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

}

在com.ssh.entity中创建“User.hbm.xml”

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
<hibernate-mapping>
	<class name="com.ssh.entity.User" table="User">
		<id name="uid" column="UserID">
			<generator class="native"></generator>
		</id>
		<property name="username" column="UserName"></property>
	</class>
</hibernate-mapping>

在src目录下新建Hibernate核心配置文件“hibernate.cfg.xml”,并完成与User的映射

<?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/mydemo</property>  
		<property name="hibernate.connection.username">root</property>  
		<property name="hibernate.connection.password">123456</property>		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
		<property name="hbm2ddl.auto">update</property>
		<property name="hibernate.show_sql">true</property>  
		<property name="hibernate.format_sql">true</property>  
		<!-- 完成映射 -->
		<mapping resource="com/ssh/entity/User.hbm.xml"/>   
    </session-factory>  
</hibernate-configuration>

四、搭建Spring

在src目录中创建Spring核心配置文件“applicationContext.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:p="http://www.springframework.org/schema/p" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation=" 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
		
</beans>

在“web.xml”中配置Spring监听器,并制定spring配置文件的位置

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

五、Struts2与Spring的整合

在“applicationContext.xml”中配置“UserAction.java”

	<bean id="userAction" class="com.ssh.action.UserAction" scope="prototype"></bean>

修改“struts.xml”中class属性值为bean标签的id属性值

<action name="user_login" class="userAction" method="login">
	<result name="login">/login.jsp</result>
</action>

六、Spring与Hibernate的整合

把“hibernate.cfg.xml”中关于数据库连接的配置删除

在“applicationContext.xml”中配置c3p0数据库连接池

<!-- c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <property name="user" value="root"></property>  
        <property name="password" value="123456"></property>  
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>  
</bean> 

在“applicationContext.xml”中配置sessionFactory,注意hibernate版本

<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >  
        <property name="dataSource" ref="dataSource"></property>  
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>   
</bean>

七、(完成互相的注入)在dao中使用hibernateTemplate

前置工作:在com.ssh.dao中新建接口“UserDao.java”,新建类“UserDaoImpl.java”继承UserDao接口

                在com.ssh.service中新建类“UseerService.java”

在action中注入service

UserAction.java

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

	public String login() {
		return "login";
	}
}

在service中注入dao

UserService.java

public class UserService {
	
	private UserDao userDao;

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

在dao中注入hibernateTemplate,注意hibernate版本

UserDaoImpl.java

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao { 
	
}

在“applicationContext.xml”文件完成配置

	<bean id="userAction" class="com.ssh.action.UserAction" scope="prototype">
		<property name="userService" ref="userServiceuserService"></property>
	</bean>
	
	<bean id="userService" class="com.ssh.service.UserService">
		<property name="userDao" ref="userDaoImpl"></property>
	</bean>
	
	<bean id="userDaoImpl" class="com.ssh.dao.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property
	</bean>

八、事务处理器

在“applicationContext.xml”中配置事务管理器

<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
</bean>  
<!-- 开启注解,指定事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />

在service上添加注解“@Transactional”

@Transactional
public class UserService {
	
	private UserDao userDao;

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

SSH框架搭建完成

附录:

文件结构


struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

	<package name="demo" extends="struts-default" namespace="/">
		
		<action name="user_login" class="userAction" method="login">
			<result name="login">/login.jsp</result>
		</action>
	
	</package>

</struts>

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.dialect">org.hibernate.dialect.MySQLDialect</property>  
		<property name="hbm2ddl.auto">update</property>
		<property name="hibernate.show_sql">true</property>  
		<property name="hibernate.format_sql">true</property>  
		
		<mapping resource="com/ssh/entity/User.hbm.xml"/>  
    </session-factory>  
</hibernate-configuration>

applicationContext.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:p="http://www.springframework.org/schema/p" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation=" 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
	
    <!-- c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <property name="user" value="root"></property>  
        <property name="password" value="123456"></property>  
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>  
    </bean> 
    
    <!-- sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >  
        <property name="dataSource" ref="dataSource"></property>  
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>   
    </bean>
    
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>  
    <!-- 开启注解,指定事务管理器 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
	
	
	<bean id="userAction" class="com.ssh.action.UserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
	
	<bean id="userService" class="com.ssh.service.UserService">
		<property name="userDao" ref="userDaoImpl"></property>
	</bean>
	
	<bean id="userDaoImpl" class="com.ssh.dao.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
</beans>



  • 0
    点赞
  • 0
    评论
  • 3
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏作者

昨年_

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值