SSH整合方案一(带有hibernate.cfg.xml)
整体结构
1.导入响应的jar包
2.在web.xml中配置struts的过滤器和spring整合web的监听器
<!--配置Spring整合web的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!--配置struts的过滤器-->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置struts核心过滤器和Spring监听器
3.编写Action,使用继承ActionSupport的方式创建Action,并使用模型驱动来封装表单
在Action中编写需要调用的Service层的属性,并提供set方法
public class CustomerAction extends ActionSupport implements ModelDriven{
private CustomerService customerService; public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
//必须手动new
private Customer customer=new Customer();
//模型和属性驱动
/**
* 保存客户
* @return
*/
public String add(){
customerService.save(customer);
return NONE;
}
@Override
public Object getModel() {
return customer;
}
}
CustomerAction
4.编写struts的配置文件,struts.xml中我们主要配置Action,将Action的创建由Spring来管理,在struts.xml主要对bean的引用
(1)将Action的创建由Spring管理
<!--配置Action必须多例-->
<bean id="customerAction" class="cn.zqr.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"/>
</bean>
Spring管理的customerAction
(2)在struts.xml主要对bean的引用
<package name="crm" namespace="/" extends="struts-default">
<!--Action由Spring管理,class值为Spring的id-->
<action name="customer_*" class="customerAction" method="{1}">
</action>
</package>
struts引用Spring创建的Action
5.编写service层,service中主要需要对dao层的调用,和开启事务两步
对dao层的调用,采用Spring对bean的管理方法,在类中提供dao层的对象,并提供对应的set方法
@Transactional
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
} /**
* 保存客户信息
* @param customer
*/
@Override
public void save(Customer customer) {
customerDao.add(customer); }
}
CustomerServiceImpl
<!--配置service-->
<bean id="customerService" class="cn.zqr.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
<!--配置sessionFacotry-->
<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<!--配置事务-->
<bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--开始事务的注解-->
<tx:annotation-driven/>
配置serice并配置sessionFactory,配置事务,开启注解事务
事务的管理和dao层的持久化都依赖于session,使用Spring管理sessionFactory的创建,并开始事务注解
6.编写dao层,dao层采用继承HibernateDaoSupport类的方式实现,需要使用HibernateDaoSupport类中的方法获取jdbc模板
dao层代码
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao { /**
* 保存客户
*/
@Override
public void add(Customer customer) {
System.out.println("持久层Customer customer"+customer); this.getHibernateTemplate().save(customer); }
}
dao层代码
dao层的配置
<!--配置dao-->
<bean id="customerDao" class="cn.zqr.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Spring中dao层的配置
编写实体类和配置hibernate配置文件不做详解,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/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:///ssh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置C3P0的连接池 -->
<property name="connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> <!-- 映射配置文件 --> <mapping resource="Customer.hbm.xml"></mapping>
</session-factory> </hibernate-configuration>
hibernate.cfg.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> <class name="cn.zqr.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id> <property name="cust_name" column="cust_name"/>
<property name="cust_user_id" column="cust_user_id"/>
<property name="cust_create_id" column="cust_create_id"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_linkman" column="cust_linkman"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/> </class> </hibernate-mapping>
customer.hbm.xml
package cn.zqr.domain; public class Customer { private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile; public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id
+ ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="
+ cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="
+ cust_phone + ", cust_mobile=" + cust_mobile + "]";
} }
Customer 类
SSH整合方案一(带有hibernate.cfg.xml)的更多相关文章
- ssh整合(spring + struts2 + hibernate)xml版
1.1分层 1.2jar节点 <dependencies> <dependency> <groupId>junit</groupId> <arti ...
- Spring整合Hibernate的时候使用hibernate.cfg.xml
Spring整合Hibernate其实也就是把Hibernate的SessionFactory对象封装成:org.springframework.orm.hibernate3.LocalSession ...
- ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表
Spring整合Hibernate Spring的Web项目中,web.xml文件会自动加载,以出现欢迎首页.也可以在这个文件中对Spring的配置文件进行监听,自启动配置文件, 以及之前Struts ...
- ssh整合思想 Spring与Hibernate的整合ssh整合相关JAR包下载 .MySQLDialect方言解决无法服务器启动自动update创建表问题
除之前的Spring相关包,还有structs2包外,还需要Hibernate的相关包 首先,Spring整合其他持久化层框架的JAR包 spring-orm-4.2.4.RELEASE.jar ( ...
- spring applicationContext.xml和hibernate.cfg.xml设置
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- hibernate.cfg.xml常见配置
转载自:http://blog.csdn.net/qiaqia609/article/details/9456489 <!--标准的XML文件的起始行,version='1.0'表明XML的版本 ...
- hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释
原文地址:http://blog.csdn.net/qiaqia609/article/details/9456489 hibernate.cfg.xml -标准的XML文件的起始行,version= ...
- hibernate配置文件hibernate.cfg.xml的详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?x ...
- hibernate.cfg.xml配置文件和hbm.xml配置文件
http://blog.sina.com.cn/s/blog_a7b8ab2801014m0e.html hibernate.cfg.xml配置文件格式 <?xml version=" ...
随机推荐
- poj-3279 poj-1753(二进制枚举)
题目链接:http://poj.org/problem?id=3279 题目大意: 有一个m*n的棋盘(1 ≤ M ≤ 15; 1 ≤ N ≤ 15),每个格子有两面分别是0或1,每次可以对一个格子做 ...
- 跟angular2学一键开启项目--关于上个react-redux项目的一键调试
一键调试类似于webpack的hot-loader,但是这个hot-loader并不怎么好用,想省事的同学可以配置一下就完了. 今天介绍browser-sync,用它来一键开启项目.它可以监听任意文件 ...
- 模拟赛 yjqb
对于这种“不能交叉”的条件,不是很好处理.那么就考虑一下dp dp[i][j]表示,考虑A中用前i个,考虑连接B中用前j个,最大匹配.(类似LCS的DP) 转移:dp[i][j]=max(dp[i][ ...
- 洛谷P1072 Hankson的趣味题
这是个NOIP原题... 题意: 给定 a b c d 求 gcd(a, x) = b && lcm(c, x) = d 的x的个数. 可以发现一个朴素算法是从b到d枚举,期望得分50 ...
- bzoj3238 差异
题目链接 思路 观察题目中的式子,可以发现前两项是定值.所以只需要求出最后一项就行了. 然后题目就转化为了求字符串中所有后缀的\(lcp\)长度之和. 可以想到用后缀数组.在后缀数组上两个后缀的\(l ...
- 计算基因上外显子碱基覆盖度(exon coverage depth):Samtool工具使用
假设想要计算ATP1A4基因上的外显子碱基覆盖度 首先查询这个基因所有exon的起始和终止位置,查询链接:http://grch37.ensembl.org/Homo_sapiens/Transcri ...
- 斯坦福大学公开课机器学习: advice for applying machine learning - evaluatin a phpothesis(怎么评估学习算法得到的假设以及如何防止过拟合或欠拟合)
怎样评价我们的学习算法得到的假设以及如何防止过拟合和欠拟合的问题. 当我们确定学习算法的参数时,我们考虑的是选择参数来使训练误差最小化.有人认为,得到一个很小的训练误差一定是一件好事.但其实,仅仅是因 ...
- ElasticSearch6.5.0【Java客户端之TransportClient】
说明 TransportClient:网上流传最多的客户端,目前最新版本 Java REST Client:官方推荐的客户端, 官方:我们要在Elasticsearch 7.0的版本中不赞成使用Tra ...
- macs 学习
点击 首先andriomianfest主配文件(主要配置文件),来启动主要的activity对象,然后通过该对象调用create方法来加载布局文件xml active通过布局文件的控件生成相应的对象. ...
- mysql安装和操作
1.install: 下载地址:https://dev.mysql.com/downloads/ 2.下载zip包解压: 3.自己在该文件夹下创建 my.ini,并编辑内容: [mysql] # 设置 ...