(一)基于SSH实现员工管理系统之框架整合篇
来源:互联网 发布:淘宝账号名字怎么修改 编辑:程序博客网 时间:2024/04/30 15:02
1.环境搭建
导入jar包
- Struts2导入jar包:
*最基本的jar包 * struts2/apps/struts2-blank.war/WEB-INF/lib/*.jar*整合Spring框架 * struts2/lib/struts2-spring-plugin-2.3.15.3.jar *整合AJAX * struts2/lib/struts2-json-plugin-2.3.15.3.jar*使用Struts2注解开发 * struts2/lib/struts2-convention-plugin-2.3.15.3.jar
- Hibernate导入jar包
* 核心包:hibernate3.jar* lib/required/*.jar* lib/jpa/*.jar* 引入hibernate整合日志系统的jar包:slf4j-log4j12-1.7.2.jar* 数据库驱动:mysql-connector-java-5.0.8-bin.jar* 数据连接池:c3p0-0.9.1.jar* 二级缓存:(可选的.)* backport-util-concurrent.jar* commons-logging.jar* ehcache-1.5.0.jar
- Spring导入jar包
*Spring3.2 开发最基本jar包 spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jar com.springsource.org.apache.commons.logging-1.1.1.jar(日记整合的包,在spring依赖包下) com.springsource.org.apache.log4j-1.2.15.jar(进行日志记录的包,在spring依赖包下)*AOP开发 spring-aop-3.2.0.RELEASE.jar spring-aspects-3.2.0.RELEASE.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar*Spring Jdbc开发 spring-jdbc-3.2.0.RELEASE.jar*Spring事务管理 spring-tx-3.2.0.RELEASE.jar*Spring整合其他ORM框架 spring-orm-3.2.0.RELEASE.jar*Spring在web中使用 spring-web-3.2.0.RELEASE.jar*Spring整合Junit测试 spring-test-3.2.0.RELEASE.jar(Spring没有引入c3p0和数据库驱动)
引入配置文件
Struts2框架的配置文件
web.xml
<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>
**src下创建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></struts>
- Spring框架的配置文件
web.xml
<!-- 配置Spring的监听器 --> <listener> <!-- 监听器默认加载的是WEB-INF/applicationContext.xml --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring框架的配置文件所在的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"></beans>
log4j.properties
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=d:/mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=info, stdout
- hibernate框架的配置文件
hibernate.cfg.xml(在ssh中该配置文件可以省略)
映射文件:类名.hbm.xml
创建包结构
*cn.my.ssh.action*cn.my.ssh.service*cn.my.ssh.dao*cn.my.ssh.domain(创建一个Product类)
package cn.my.ssh.domain;public class Product { private Integer id; private String name; private Double price; 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 Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public String toString() { return "Product [id=" + id + ", name=" + name + ", price=" + price + "]"; }}
2.Struts2整合Spring
页面创建(保存商品addProduct.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> <h1>保存商品的页面</h1> <s:form action="product_save" namespace="/" method="post" theme="simple"> <table border="1"> <tr> <td>商品名称</td> <td><s:textfield name="name"></s:textfield></td> </tr> <tr> <td>商品价格</td> <td><s:textfield name="price"></s:textfield></td> </tr> <tr> <td colspan="2"><s:submit value="保存商品"></s:submit></td> </tr> </table> </s:form></body></html>
编写action,service,dao
- ProductAction
package cn.my.ssh.action;import cn.my.ssh.domain.Product;import cn.my.ssh.service.ProductService;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * 商品管理action类 * @author xiong * */public class ProductAction extends ActionSupport implements ModelDriven<Product> {// 模型驱动使用的类 Product product = new Product(); public Product getModel() { return product; }// struts2和spring整合按名称注入的业务层类 private ProductService productService = new ProductService(); public void setProductService(ProductService productService) { this.productService = productService; }}
- ProductService
package cn.my.ssh.service;import cn.my.ssh.dao.ProductDao;/** * 商品管理service类 * @author xiong * */public class ProductService {// 业务层注入dao的类 private ProductDao productDao = new ProductDao(); public void setProductDao(ProductDao productDao) { this.productDao = productDao; }}
- ProductDao
package cn.my.ssh.dao;/** * 商品管理dao类 * @author xiong * */public class ProductDao {}
配置action,service,dao的类
service,dao类的配置
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置业务层的类 --> <bean id="productService" class="cn.my.ssh.service.ProductService"> <property name="" ref=""></property> </bean> <!-- 配置dao层的类 --> <bean id="productDao" class="cn.my.ssh.dao.ProductDao"> </bean></beans>
(1)struts2自己管理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="ssh" namespace="/" extends="struts-default"> <action name="product_*" class="cn.my.ssh.action.ProductAction" method="{1}"></action> </package></struts>
添加各层测试代码:
结果:
action类的save方法执行了service层的save方法执行了dao层的save方法执行了
(2)action类由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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置action的类 注意加scope="prototype"不然action会是单例的--> <bean id="productAction" class="cn.my.ssh.action.ProductAction" scope="prototype"> <!-- 手动注入service --> <property name="productService" ref="productService"></property> </bean> <!-- 配置业务层的类 --> <bean id="productService" class="cn.my.ssh.service.ProductService"> <property name="productDao" ref="productDao"></property> </bean> <!-- 配置dao层的类 --> <bean id="productDao" class="cn.my.ssh.dao.ProductDao"> </bean></beans>
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="ssh" namespace="/" extends="struts-default"> <action name="product_*" class="productAction" method="{1}"></action> </package></struts>
Web层获得Service:
传统方式:* 获得WebApplicationContext对象.* 通过WebAppolicationContext中getBean(“”);实际开发中:* 引入了struts2-spring-plugin-2.3.15.3.jar* 有一个配置文件 : struts-plugin.xml开启常量 :<constant name="struts.objectFactory" value="spring" />引发另一个常量的执行:(Spring的工厂类按照名称自动注入)struts.objectFactory.spring.autoWire = name
3.Spring整合Hibernate
创建数据库和映射文件
创建一个ssh的数据库:create database ssh;
创建Product的映射文件(与实体类在同一个包下):
Product.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> <class name="cn.my.ssh.domain.Product" table="product"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name" length="20"></property> <property name="price" column="price"></property> </class></hibernate-mapping>
配置去掉Hibernate配置文件
- 编写jdbc.properties数据库属性文件
jdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/sshjdbc.username=rootjdbc.password=root
- 在applicationContext.xml配置hibernate的相关属性
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 引入外部属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置hibernate相关属性 也可以使用hibernate.cfg.xml配置文件进行配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置hibernate属性 --> <property name="hibernateProperties"> <props> <!-- 配置方言为mysql方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 打印sql语句 --> <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="mappingResources"> <!-- 对象为多个,使用list --> <list> <!-- 这里的路径是相对于src/下的路径 --> <value>cn/my/ssh/domain/Product.hbm.xml</value> </list> </property> </bean> <!-- 配置action的类 注意加scope="prototype"不然action会是单例的--> <bean id="productAction" class="cn.my.ssh.action.ProductAction" scope="prototype"> <!-- 手动注入service --> <property name="productService" ref="productService"></property> </bean> <!-- 配置业务层的类 --> <bean id="productService" class="cn.my.ssh.service.ProductService"> <property name="productDao" ref="productDao"></property> </bean> <!-- 配置dao层的类 --> <bean id="productDao" class="cn.my.ssh.dao.ProductDao"> </bean></beans>
运行后自动在ssh数据库上创建product表
编写dao代码
在dao注入sessionFactory:
1.在dao的类上继承HibernateDaoSupport。2.在配置dao时 <!-- 配置dao层的类 --> <bean id="productDao" class="cn.my.ssh.dao.ProductDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>3.在Dao层调用模版完成保存操作 this.getHibernateTemplate().save(product);
添加事务管理
<!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 注解开启事务 --> <tx:annotation-driven transaction-manager="transactionManager"/>
**然后在业务层类上添加一个注解:
@Transactional**
HibernateTemplate的API:
Serializable save(Object entity) :保存数据 void update(Object entity) :修改数据 void delete(Object entity) :删除数据 <T> T get(Class<T> entityClass, Serializable id) :根据ID进行检索.立即检索 <T> T load(Class<T> entityClass, Serializable id) :根据ID进行检索.延迟检索. List find(String queryString, Object... values) :支持HQL查询.直接返回List集合. List findByCriteria(DetachedCriteria criteria) :离线条件查询. List findByNamedQuery(String queryName, Object... values) :命名查询的方式.
阅读全文
0 0
- (一)基于SSH实现员工管理系统之框架整合篇
- 基于SSH实现员工管理系统之框架整合篇
- 【SSH】:基于Struts2+HIbernate3+Spring3实现员工管理系统之框架整合篇
- 基于SSH实现员工管理系统之框架整合篇 学习笔记
- 基于SSH实现管理系统之框架整合篇 5 Spring整合Hibernate框架
- 基于SSH实现管理系统之框架整合篇 4 Struts整合Spring
- 【SSH】:基于Struts2+HIbernate3+Spring3实现员工管理系统之案例实现篇(上)
- 【SSH】:基于Struts2+HIbernate3+Spring3实现员工管理系统之案例实现篇(下)
- 基于SSH实现管理系统之框架整合篇 3-1 创建项目引入jar包
- 基于SSH实现管理系统之框架整合篇 3-2 引入相应的配置文件
- 基于SSH实现管理系统之框架整合篇 3-3 创建包结构及实体
- 基于JavaEE(SSH)实现员工管理系统案例
- 基于SSH的员工管理系统(一)——包结构
- JavaWeb项目系列(一):员工管理系统(SSH框架)
- JDBC实现员工管理系统(一)
- 基于SSH三大框架的员工管理系统——总结
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- 基于SSM实现的简易员工管理系统(基于阿里云的网站上线篇)
- 视觉跟踪综述
- Novate 封装Rxjava2.0 的使用
- Android在AndroidStudio中引用Module依赖注意事项
- MD5 校验和(checksum)
- MySql 里的IFNULL用法
- (一)基于SSH实现员工管理系统之框架整合篇
- Activity的启动模式和Intent标记位
- 语义分割--Dilated Residual Networks
- iOS开发系列--Objective-C之类和对象
- 学习CI框架数据库相关知识点总结
- 1061. 判断题(15) PAT
- No 'Access-Control-Allow-Origin' header is present on the requested resource
- D 02_css的代码规范
- Java中List集合的逆序排列