struts2 hibernate mysql 实例_Struts2+Spring+Hibernate集成实例

在本教程中,它显示的集成 “Struts2 + Spring + Hibernate“,请务必检查以下之前继续学习教程。

参见集成步骤总结:

获取所有的依赖库(很多)。

注册 Spring 的 ContextLoaderListener 来整合 Struts2 和 Spring。

使用 Spring 的 LocalSessionFactoryBean 来集成 Spring 和 Hibernate。

完成所有连接。

请参阅它们之的关系:

Struts 2 Spring Hibernate

这将是一个很长的教程,相关解释并不是很多,请务必阅读上述2篇文章的详细情况说明以方面学习。

这将要创建一个客户页面,以添加客户和列表的自定义函数。前端使用Struts2显示,Spring作为依赖注入引擎,而 Hibernate 用来执行数据库操作。让我们开始...

1. 工程文件夹结构

在本章中,我们创建一个 ssh 的web工程,工程的目录结构如下图所示:

99a24fd8b017f15c66e00d72cd4a6bd8.png

bed3476ff4f589d18742b54174ee87c9.png

2. MySQL表结构结构

客户(customer)表脚本。

DROP TABLE IF EXISTS `yiibai`.`customer`;

CREATE TABLE `yiibai`.`customer` (

`CUSTOMER_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,

`NAME` varchar(45) NOT NULL,

`ADDRESS` varchar(255) NOT NULL,

`CREATED_DATE` datetime NOT NULL,

PRIMARY KEY (`CUSTOMER_ID`)

) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

3. Hibernate相关配置

只有模型和映射文件是必需的,因为这里要用Spring处理Hibernate配置。

Customer.java – 创建客户表对应的一个类。

package com.yiibai.customer.model;

import java.util.Date;

public class Customer implements java.io.Serializable {

private Long customerId;

private String name;

private String address;

private Date createdDate;

//getter and setter methods

}

Customer.hbm.xml – Hibernate的客户映射文件。

/p>

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

table="customer" catalog="yiibai">

5. Struts2相关

实现了 Bo 和 DAO 设计模式。所有Bo和DAO将由Spring Spring bean配置文件注入。在DAO中,让它扩展Spring的HibernateDaoSupport来集成 Spring 和 Hibernate。

CustomerBo.java

package com.yiibai.customer.bo;

import java.util.List;

import com.yiibai.customer.model.Customer;

public interface CustomerBo{

void addCustomer(Customer customer);

List listCustomer();

}

CustomerBoImpl.java

package com.yiibai.customer.bo.impl;

import java.util.List;

import com.yiibai.customer.bo.CustomerBo;

import com.yiibai.customer.dao.CustomerDAO;

import com.yiibai.customer.model.Customer;

public class CustomerBoImpl implements CustomerBo{

CustomerDAO customerDAO;

//DI via Spring

public void setCustomerDAO(CustomerDAO customerDAO) {

this.customerDAO = customerDAO;

}

//call DAO to save customer

public void addCustomer(Customer customer){

customerDAO.addCustomer(customer);

}

//call DAO to return customers

public List listCustomer(){

return customerDAO.listCustomer();

}

}

CustomerDAO.java

package com.yiibai.customer.dao;

import java.util.List;

import com.yiibai.customer.model.Customer;

public interface CustomerDAO{

void addCustomer(Customer customer);

List listCustomer();

}

CustomerDAOImpl.java

package com.yiibai.customer.dao.impl;

import java.util.List;

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

import com.yiibai.customer.dao.CustomerDAO;

import com.yiibai.customer.model.Customer;

public class CustomerDAOImpl extends HibernateDaoSupport

implements CustomerDAO{

//add the customer

public void addCustomer(Customer customer){

getHibernateTemplate().save(customer);

}

//return all the customers in list

public List listCustomer(){

return getHibernateTemplate().find("from Customer");

}

}

CustomerAction.java – Struts2 的动作不再需要扩展ActionSupport,它将由 Spring 来处理。

package com.yiibai.customer.action;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import com.yiibai.customer.bo.CustomerBo;

import com.yiibai.customer.model.Customer;

import com.opensymphony.xwork2.ModelDriven;

public class CustomerAction implements ModelDriven{

Customer customer = new Customer();

List customerList = new ArrayList();

CustomerBo customerBo;

//DI via Spring

public void setCustomerBo(CustomerBo customerBo) {

this.customerBo = customerBo;

}

public Object getModel() {

return customer;

}

public List getCustomerList() {

return customerList;

}

public void setCustomerList(List customerList) {

this.customerList = customerList;

}

//save customer

public String addCustomer() throws Exception{

//save it

customer.setCreatedDate(new Date());

customerBo.addCustomer(customer);

//reload the customer list

customerList = null;

customerList = customerBo.listCustomer();

return "success";

}

//list all customers

public String listCustomer() throws Exception{

customerList = customerBo.listCustomer();

return "success";

}

}

6. Spring相关配置

几乎所有的配置都是在这里完成是由Spring专门来整合。

CustomerBean.xml – 声明 Spring 的 bean:Action, BO 和 DAO.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

database.properties – 声明数据库详细信息

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/yiibai

jdbc.username=root

jdbc.password=password

DataSource.xml – 创建一个数据库源的Bean

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

WEB-INF/classes/config/database/properties/database.properties

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

HibernateSessionFactory.xml – 创建一个SessionFactory Bean来集成Spring和Hibernate。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

org.hibernate.dialect.MySQLDialect

true

com/yiibai/customer/hibernate/Customer.hbm.xml

SpringBeans.xml – 创建一个核心 Spring 的 bean 配置文件,作为中央的 bean 管理层。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

7. JSP 页面

JSP页面来显示使用 Struts2 标签的元素。

customer.jsp

Struts 2 + Spring + Hibernate integration example

Add Customer

All Customers

Customer IdNameAddressCreated Date

8. struts.xml

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

class="customerAction" method="addCustomer" >

pages/customer.jsp

class="customerAction" method="listCustomer" >

pages/customer.jsp

9. Struts 2 + Spring

要集成Struts2和Spring,只需注册ContextLoaderListener监听器类,定义一个“contextConfigLocation”参数要求Spring容器来解析“SpringBeans.xml”,而不使用默认的“applicationContext.xml”。

web.xml

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Struts 2 Web Application

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

contextConfigLocation

/WEB-INF/classes/SpringBeans.xml

org.springframework.web.context.ContextLoaderListener

10. 运行实例

在浏览器中打开网址 : http://localhost:8080/ssh/listCustomerAction.action

a25207e6760b7b69cef23b3b458d495f.png

2eafa38be5d5f3fe04e309f2f52cef0e.png

参考

代码下载 - http://pan.baidu.com/s/1mgzt1Xm (含ssh相关类库,详见 lib 目录,文件大小约:18M)。

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值