1、我用到的jar包

    

2、整合实例

2.1、MySQL数据库建表语句

 create database school; -- 创建数据库
use school; -- 使用school数据库
4 create table user( -- 创建user表
id int(4) primary key auto_increment,
name varchar(20) not null,
age int(4) not null
);
inert into user(name,age) values("张三",);

2.2、配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- Spring监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/beans_*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Struts2过滤器 -->
<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>
</web-app>

2.3、创建实体类(User)

 package com.shore.entity;

 /**
* @author DSHORE/2019-11-12
*
*/
public class User {
private Integer id;
private String name;
private Integer age; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

2.4、配置实体类User的hibernate文件(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="com.shore.entity">
<class name="User">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="age" type="java.lang.Integer"/>
</class>
</hibernate-mapping>

2.5、创建dao层IUserDao接口类以及UserDao接口实现类

 //接口类
public interface IUserDao {
public User getByName(String name);//根据用户名查询
} //对应的接口实现类
public class UserDao implements IUserDao {
//从IoC容器注入SessionFactory
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} @Override //根据用户名查询
public User getByName(String name) {
Query query = sessionFactory.getCurrentSession().createQuery("from User where name=:name"); //:name是命名参数
query.setParameter("name", name);
@SuppressWarnings("unchecked")
List<User> users = query.list();
if (users != null && users.size() > 0) {
return users.get(0);
}
return null;
}
}

2.6、创建service层IUserService接口类以及UserService接口实现类

 //接口类
public interface IUserService {
public User getByName(String name);
} //对应的接口实现类
public class UserService implements IUserService { private IUserDao userDao;
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
} @Override
public User getByName(String name) {
return userDao.getByName(name);
}
}

2.7、创建UserAction

 package com.shore.action;

 import com.opensymphony.xwork2.ActionSupport;
import com.shore.service.IUserService; /**
* @author DSHORE/2019-11-16
*
*/
public class UserAction extends ActionSupport{
private static final long serialVersionUID = -3099218232367860074L; private IUserService userService; public String login() {
System.out.println(userService.getByName("张三"));
return SUCCESS;
} public void setUserService(IUserService userService) {
this.userService = userService;
}
}

2.8、配置Spring文件(beans_common.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: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/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"> <!-- c3p0数据库连接池配置 -->
<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/school"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="100"></property>
<property name="maxStatements" value="200"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="2"></property>
</bean> <!-- Hibernate核心配置文件(hibernate.cfg.xml)全交给Spring去管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- c3p0的数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- Hibernate基础参数配置 -->
<property name="hibernateProperties">
<props> <!-- 注意:这个是Spring配置文件,故下面的key要写全名,即:前面加上hibernate.xxxxxx -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- Hibernate 映射文件的配置 -->
<property name="mappingLocations">
<list>
<value>classpath:com/shore/entity/*.hbm.xml</value>
</list>
</property>
</bean> <!-- dao层 -->
<bean id="userDao" class="com.shore.dao.impl.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- service层 -->
<bean id="userService" class="com.shore.service.impl.UserService">
<property name="userDao" ref="userDao"></property>
</bean> <!-- action层 -->
<bean id="userAction" class="com.shore.action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean> <!-- ############Spring声明式事务管理配置########### -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 配置事务增强(针对DAO层) -->
<tx:advice transaction-manager="transactionManager" id="transactionAdvice">
<tx:attributes> <!-- *代表DAO层的所有方法 -->
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- AOP配置:配置切入点表达式 -->
<aop:config> <!-- 第一个*表示返回值类型;第二个*表示service层下的所有接口实现类;第三个*表示每个接口实现类下的所有方法 -->
<aop:pointcut expression="execution(* com.shore.service.impl.*.*(..))" id="pt"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>

2.9、配置Struts2 文件(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>
<!-- true支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <!-- true --> <package name="user" namespace="/user" extends="struts-default">
<action name="userAction" class="com.shore.action.UserAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

测试结果图:

  

总结:

1、与传统方式相比,SSH框架 代码层(dao层、service层 等)耦合度较低,方便后期维护。

2、entity、dao、service、action层、以及db层(连接数据库)都交给spring IOC容器来管理,大大降低各层之间的耦合度,极大方便该项目后期的维护。

3、Hibernate或Spring IOC容器管理project与DB之间的连接Spring IOC容器管理dao层(及BaseDao)、service层、action层Struts2管理action层与前端页面的数据交互

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/11874947.html

版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

最新文章

  1. 【Java EE 学习 78 中】【数据采集系统第十天】【Spring远程调用】
  2. java复习集合类之List接口
  3. 怎样打开64位 Ubuntu 的32位支持功能?
  4. Sql 中text类型字段判断是否为空
  5. SQL SERVER实例解析
  6. [Java] MAP、LIST、SET集合解析
  7. js中的命名规范
  8. Android中GridLayout与GridView区别
  9. 【转】Gabor 入门
  10. 自适应网页设计/响应式Web设计
  11. 安装gitlab8.0在reconfigure报错
  12. 20155306 2017-2018-1《信息安全系统设计》第二周课堂测试以及myod的实现
  13. TabTopAutoLayout【自定义顶部选项卡区域(带下划线)(动态选项卡数据且可滑动)】
  14. LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)
  15. nginx学习笔记(一)
  16. v2ray和ss的安装资料整理
  17. ARC下野指针 EXC_BAD_ACCESS错误
  18. 【HTTP缓存】浏览器缓存理论知识
  19. 【Hbase学习之二】Hbase 搭建
  20. 51nod 1577 异或凑数

热门文章

  1. nginx 设置开机启动
  2. 数据结构——java实现栈
  3. 情感交流篇:HTML页面如何与后端联系
  4. hdu 6216 A Cubic number and A Cubic Number
  5. Myeclipse debug 调式java 报错,留做后面解决!
  6. Map集合中key不存在时使用toString()方法、valueOf()方法和强制转换((String))之间的区别
  7. Unity 宽度适配 NGUI
  8. Vue页面手动刷新,导航栏激活项还原到初始状态问题解决方案
  9. js对象转数组
  10. K2 BPM_万翼科技携手上海斯歌,全面启动K2平台升级项目_十年专注业务流程管理系统