java ssh 学习_JavaWeb学习:SSH整合(无障碍整合)

一、创建Web项目,引入jar包

①、Struts2的jar包

asm-7.3.1.jar:提供了字节码的读写的功能,包含了核心的功能,而其他的jar,都是基于这个核心的扩展.

asm-commons-7.3.1.jar:提供了基于事件的表现形式。

asm-tree-7.3.1.jar:提供了基于对象的表现形式。

commons-fileupload-1.4.jar:文件上传组件,2.1.6版本后需要加入此文件

commons-io-2.6.jar:传文件依赖的jar包

commons-lang3-3.8.1.jar:对java.lang包的增强

freemarker-2.3.30.jar:Struts 2的UI标签的模板使用FreeMarker编写

javassist-3.20.0-GA.jar:代码生成工具, struts2用它在运行时扩展 Java类

log4j-api-2.12.1.jar:日志

log4j-core-2.12.1.jar:日志

ognl-3.1.28.jar:对象图导航语言(Object Graph Navigation Language),  struts2框架通过其读写对象的属性

struts2-convention-plugin-2.5.25.jar:Struts2的注解开发包

struts2-core-2.5.25.jar:Struts 2框架的核心类库

struts2-json-plugin-2.5.25.jar:Struts2整合Ajax的开发包

struts2-spring-plugin-2.5.25.jar:Struts2整合Spring的开发包

②、Hibernate的jar包

Hibernate必须包(hibernate-release-5.4.23.Final\lib\required)

数据库驱动

使用C3P0连接池(hibernate-release-5.4.23.Final\lib\optional\c3p0)

注意:Struts2和Hibernate都引入了一个相同的jar包(javassist包)。删除一个

③、Spring的jar包

IOC的开发

com.springsource.org.apache.commons.logging-1.1.1.jar

spring-beans-5.2.9.RELEASE.jar

spring-context-5.2.9.RELEASE.jar

spring-core-5.2.9.RELEASE.jar

spring-expression-5.2.9.RELEASE.jar

AOP的开发

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

spring-aop-5.2.9.RELEASE.jar

spring-aspects-5.2.9.RELEASE.jar

JDBC模板的开发

spring-jdbc-5.2.9.RELEASE.jar

spring-tx-5.2.9.RELEASE.jar (事务管理)

整合web项目的开发

spring-web-5.2.9.RELEASE.jar

整合单元测试的开发

spring-test-5.2.9.RELEASE.jar

整合Hibernate的开发

spring-orm-5.2.9.RELEASE.jar

spring-jdbc-5.2.9.RELEASE.jar

spring-tx-5.2.9.RELEASE.jar (事务管理)

使用C3P0需要引入hibernate-release-5.4.23.Final\lib\optional\c3p0中

c3p0-0.9.5.5.jar

hibernate-c3p0-5.4.23.Final.jar

mchange-commons-java-0.2.19.jar

二、引入配置文件

Struts的配置文件

web.xml:Struts2的核心配置

struts2

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

struts2

/*

struts.xml:管理Action

/p>

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

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

Hibernate的配置文件

hibernate.cfg.xml

/p>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

com.microsoft.sqlserver.jdbc.SQLServerDriver

jdbc:sqlserver://localhost:1433;databaseName=HibernateDB;

sa

AAA@111

org.hibernate.dialect.SQLServer2008Dialect

true

true

update

2

thread

org.hibernate.connection.C3P0ConnectionProvider

5

20

120

3000

映射文件(类名.hbm.xml):持久化类必要配置       Hibernate抓取策略

Spring的配置文件

web.xml:Spring的核心监听器

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

applicationContext.xml:Spring的核心配置(主要用于管理Bean,整合Struts2和Hibernate)

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

日志配置文件

三、创建包结构

c25d3a5257a8781b86249568382394e4.png

四、创建相关类

e2d5878dab02c309355b3d90535df9d8.png

五、编写Url

- 新增客户

六、编写新增页面

客户名称:客户级别 :信息来源 :所属行业 :固定电话 :移动电话 :

七、疏通基盘

①、编写Action

public class CustomerAction extends ActionSupport implements ModelDriven{private Customer customer=newCustomer();

@OverridepublicCustomer getModel() {returncustomer;

}public voidsave() {

System.out.println("Action中save方法执行了");

}

}

②、配置struts.xml

八、在Action访问Service

①、传统方式

WebApplicationContext webApplicationContext =WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());

Object bean= webApplicationContext.getBean("CustomerServiceImpl");

②、Spring整合Struts2方式一:Action由Struts2管理

Ⅰ、引入struts-spring-plugin.jar

Ⅱ、在struts.xml中设置常量struts.objectFactory

开启此常量会使得struts.objectFactory.spring.autowire=name常量有效,此常量(struts.objectFactory.spring.autowire)主要将Service按照名称(是指applicationContext.xml中bean标签中id)自动注入到Action中

Ⅲ、配置applicationContext.xml,将CustomerServiceImpl交给Spring管理

Ⅳ、编写Action

public class CustomerAction extends ActionSupport implements ModelDriven{private Customer customer=newCustomer();

@OverridepublicCustomer getModel() {returncustomer;

}//属性注入

privateCustomerService customerService;public voidsetCustomerService(CustomerService customerService) {this.customerService =customerService;

}publicString save() {

System.out.println("Action中save方法执行了");

customerService.save(customer);returnNONE;

}

}

③、Spring整合Struts2方式一:Action由Spring管理

Ⅰ、引入struts-spring-plugin.jar

Ⅱ、将Action交给Spring管理,并注入Service

注意:配置Action为多例(scope="prototype")

Ⅲ、配置struts.xml的Action,action标签中class=“”的值为上面Spring的bean标签中的id的值

九、Service访问Dao

①、将Dao交给Spring管理

②、Dao注入到Service中

public class CustomerServiceImpl implementsCustomerService {//属性注入

privateCustomerDao customerDao;public voidsetCustomerDao(CustomerDao customerDao) {this.customerDao =customerDao;

}

@Overridepublic voidsave(Customer customer) {

System.out.println("CustomerServiceImpl的save方法执行了");

customerDao.save(customer);

}

}

十、Dao方法Hibernate

①、创建持久化类

Ⅰ、创建类

public classCustomer {privateLong cust_id;privateString cust_name;privateString cust_source;privateString cust_industry;privateString cust_level;privateString cust_phone;privateString cust_mobile;

}

Ⅱ、映射文件

Ⅲ、hibernate.cfg.xml添加映射

②、Spring整合Hibernate(需要引入spring-orm-5.2.9.RELEASE.jar)

Ⅰ、使用Hibernate的配置文件

1、在Spring的配置文件中,加载Hibernate的配置文件

2、DAO继承HibernateDaoSupport,Spring提供了一个Hibernate的模板类简化Hibernate开发

2.1、HibernateDaoSupport中由sessionfactory属性,可用于属性注入,并且在设置sessionFactory时,会创建Hibernate的模板类(hibernateTemplate)

public final voidsetSessionFactory(SessionFactory sessionFactory) {if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {this.hibernateTemplate =createHibernateTemplate(sessionFactory);

}

}

2.2、编写Dao

public class CustomerDaoImpl extends HibernateDaoSupport implementsCustomerDao {

@Overridepublic voidsave(Customer customer) {

System.out.println("CustomerDaoImpl的save方法执行了");this.getHibernateTemplate().save(customer);

}

}

2.3、在Dao中注入sessionfactory

测试抛出异常:Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

2.3.1、解决方案:配置Spring的事务管理

2.3.1.1、配置事务管理器

2.3.1.2、开启事务注解

2.3.1.3、在Service上使用事务注解

@Transactionalpublic class CustomerServiceImpl implements CustomerService {

Ⅱ、将hibernate的配置交给Spring管理

1、hibernate配置文件中有以下内容

数据库连接

Hibernate的相关属性配置

方言

显示SQL

格式化SQL

C3P0连接池相关配置

映射配置

2、将hibernate的配置交给Spring管理

2.1、设置数据库连接(配置到属性文件jdbc.properties中)

jdbc.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver

jdbc.url=jdbc:sqlserver://localhost:1433;databaseName=HibernateDB;

jdbc.username=sa

jdbc.password=AAA@111

2.2、applicationContext.xml配置Hibernate

org.hibernate.dialect.SQLServer2008Dialect

true

true

update

com/xxx/ssh/domain/Customer.hbm.xml

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值