整体结构:

1.引入相关jar包

2.编写实体类和映射文件

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 实体类

<?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>

实体类映射文件

3.配置struts核心过滤器,和Spring文件加载的监听器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--配置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>
</web-app>

struts核心过滤器的配置和Spring文件加载监听器

4.编写Action并配置,采用模型驱动的方式对表单进行封装,采用Spring注入的方式获取serice实例,并配置Action

/**
* 客户的控制层
*/
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;
}
}

Action

  <!--配置service-->
<bean id="customerService" class="cn.zqr.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>

使用Spring管理Action

  <!--配置Action必须多例-->
<bean id="customerAction" class="cn.zqr.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"/>
</bean>

Spring配置文件中配置Action

在struts中引入Action

<?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="crm" namespace="/" extends="struts-default">
<!--Action由Spring管理,class值为Spring的id-->
<action name="customer_*" class="customerAction" method="{1}">
</action>
</package> </struts>

struts.xml中引入Action

5.配置service

service层代码:

/**
* 客户业务层
*/
@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); }
}

Service层代码

 

service需要开事务,在Spring中配置事务,开启事务需要使用session,所以首先配置SessionFactory,创建session工厂需要数据源,所以需要优先配置数据源

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

配置数据源C3P0

   <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>Customer.hbm.xml</value>
</list>
</property> </bean>

配置SessionFactory以及hibernate相关属性

最后配置事务并开启

 <!--配置事务-->
<bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean>
<!--开始事务的注解-->
<tx:annotation-driven/>

配置事务并开启

6.编写dao层,dao层需要使用Hibernate模板(HibernateTemplate),为了简化开发,可以直接继承HibernateDaoSupport,然后向其注入sessionFactory

/**
* 客户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-->
<bean id="customerDao" class="cn.zqr.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

dao层的配置

SSH整合方案二(不带hibernate.cfg.xml)的更多相关文章

  1. SSH整合方案一(带有hibernate.cfg.xml)

    整体结构 1.导入响应的jar包 2.在web.xml中配置struts的过滤器和spring整合web的监听器 <!--配置Spring整合web的监听器--> <listener ...

  2. ssh整合之二hibernate单独搭建

    1.首先我们需要去拷贝我们的hibernate所需的jar包  这里还需要加入我们C3P0的jar包,因为我们hibernate中使用的C3P0连接池 2. 编写我们的关系映射文件Customer.c ...

  3. Spring整合Hibernate的时候使用hibernate.cfg.xml

    Spring整合Hibernate其实也就是把Hibernate的SessionFactory对象封装成:org.springframework.orm.hibernate3.LocalSession ...

  4. SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]

    非常诡异的报错,信息如下:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(* ...

  5. [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. hibernate.properties与hibernate.cfg.xml 区别

    Hibernate的数据库连接信息是从配置文件中加载的. Hibernate的配置文件有两种形式:一种是XML格式的文件,一种是properties属性文件. 一)hibernate.cfg.xml ...

  7. hibernate.cfg.xml配置(Oracle+c3p0)

    说明:数据库:Oracle10g:连接池:c3p0 结构: 一.配置hibernate.cfg.xml <?xml version="1.0" encoding=" ...

  8. eclipse为hibernate.cfg.xml添加自动提示【转】

    在hibernate.cfg.xml头部部分如下: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate C ...

  9. Hibernate的配置文件,hibernate.cfg.xml

    单纯的只针对持久层框架 Hibernate 配置文件的一些总结 一.Hibernate底层原理 1. Hibernate保存原理 目的:把domain对象保存到数据库的表,形成一条记录. sql: i ...

随机推荐

  1. 「JLOI2015」城池攻占 解题报告

    「JLOI2015」城池攻占 注意到任意两个人的战斗力相对大小的不变的 可以离线的把所有人赛到初始点的堆里 然后做启发式合并就可以了 Code: #include <cstdio> #in ...

  2. 全局变量 static变量

    变量 作用域 全局变量( external linkage ) 定义在函数外 Int a=1 作用于整个工程 在连接两个文件时若有两个a会报错 Staic 函数外(internal linkage) ...

  3. hdu 2149 (巴什博奕)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2149 Problem Description 虽然不想,但是现实总归是现实,Lele始终没有逃过退学的 ...

  4. 洛谷P2150 寿司晚宴

    解:发现每个质数只能属于一个人,于是想到每个质数有三种情况:属于a,属于b,都不属于. 然后考虑状压每个人的质数集合,可以得到30分. 转移就是外层枚举每个数,内层枚举每个人的状态,然后看能否转移.能 ...

  5. n+lognlogV查找最大值

    来自Blogewoosh #6. 啃了一下,写个翻译吧. 问题:你有一个数组,你不知道每个元素的大小,但是能够提出询问:a[x]是否>=v?你需要找出这个数组的最大值,只能询问n + lognl ...

  6. A1120. Friend Numbers

    Two integers are called "friend numbers" if they share the same sum of their digits, and t ...

  7. 编译 pcre - 开源的正则表达式(库)

    PCRE百科介绍: PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl ...

  8. 模块---hashlib、configparse、logging

    一.hashlib模块 hashlib模块介绍:hashlib这个模块提供了摘要算法,例如 MD5.hsa1 摘要算法又称为哈希算法,它是通过一个函数,把任意长度的数据转换为一个长度固定的数据串,这个 ...

  9. springmvc跨域上传文件问题

    把以下文件放到webapps的root文件夹下: 1.clientaccesspolicy.xml <?xml version="1.0" encoding="ut ...

  10. Luogu P2575 高手过招

    题目链接 \(Click\) \(Here\) 关键在于转换成阶梯\(Nim\)的模型.最开始把题目看错了,理解正确后发现棋子可以向后跳不止一位,那么就比较简单了. 这里把空格看做阶梯,棋子看做硬币, ...