手动添加SSH支持、使用c3p0
之前做的笔记,现在整理一下;大家有耐心的跟着做就能成功;
SSH(struts2、spring、hibernate)
* struts2
* 充当mvc的角色
* hibernate
dao层用hibernate技术来实现
* spring
* spring的声明式事务管理
* 应用spring的IOC和di做到完全的面向接口编程
先添加一个数据库做测试用:使用的是mysql5.0
create database testoa default character set utf8;
show create database testoa;
1.新建一个Web Project项目 TestSSH
2.添加Struts2支持:添加jar包
struts-2.1.8.1/apps/struts2-blank-2.1.8.1.war/WEB-INF/lib 下的所以jar包
struts2-core.jar 核心jar包
xwork-2.jar xwork核心jar包
ognl.jar ognl表达式
freemarker.jar FreeMarker模板
commons-logging.jar 日志
commons-fileupload.jar 文件上传
commons-io.jar 文件上传依赖的包
配置文件:
配置Strut2的主过滤器: 将struts-2.1.8.1/apps/struts2-blank-2.1.8.1.war/WEB-INF/web.xml下的下面内容复制到项目的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>
拷贝Strut2的主配置文件:
在struts-2.1.8.1/apps/struts2-blank-2.1.8.1.war/WEB-INF/classes/struts.xml下,
拷贝到工程的src目录下
修改几个基本的配置:
<struts>
<!-- 配置问开发模式( 显示信息 和 配置文件修改后不需要重启) -->
<constant name="struts.devMode" value="true" />
<!-- 配置扩展名为action -->
<constant name="struts.action.extension" value="action" />
</struts>
Struts2的环境添加完了;
3.添加Hibernate支持
添加jar包:
核心jar包hibernate-distribution-3.6.0.Final/hibernate3.jar ;
hibernate-distribution-3.6.0.Final/lib/ required下的所有包:
还有hibernate-distribution-3.6.0.Final/lib/jpa/ hibernate-jpa-2.0-api-1.0.0.Final.jar;
还有hibernate-distribution-3.6.0.Final/lib/ optional /c3p0/ c3p0-0.9.1.jar;
还有数据库驱动:F:\快盘\JAVA\jdbc\ mysql-connector-java-5.1.5-bin.jar ;
hibernate-distribution-3.6.0.Final/ project/ etc/ hibernate.cfg.xml 拷贝到项目的src下;
添加基本的配置:
<hibernate-configuration> <session-factory> <!-- 数据库信息(连接信息写到spring的配置文件中) --> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="connection.url">jdbc:mysql:///testoa</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">jerome</property> <!-- 其他配置 --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hqu.oa.domain"> <class name="User" table="hqu_user"> <id name="id"> <generator class="native" /> </id> <property name="name" /> </class> </hibernate-mapping>
随便选一个符合条件的就可以,拷贝到项目src下;)
添加Spring支持:
添加jar包:
Spring核心包:
spring-framework-2.5.6-with-dependencies.zip/spring-framework-2.5.6/dist/spring.jar ;
依赖的:
日志:
lib/jakarta-commons/ commons-logging.jar ;
面向切面编程(AOP)用到的:(
/lib/ aspectj/ aspectjrt.jar和aspectjweaver.jar
和生成代理用的:
ib/cglib/cglib-nodep-2.1_3.jar ; )
还有一个可加的做MD5加密的 这里面有工具类:
/lib/ jakarta-commons/ commons-codec.jar ;
配置文件:
( F:\Java\spring-framework-2.5.6\samples\imagedb\war\WEB-INF下的applicationContext.xml
拷贝到src,)新建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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="com.hqu.oa"></context:component-scan> </beans>
到此都添加ssh完了,接下去开始整合:SSH整合:
先Spring和Hibernate整合:
Spring来管理事务,管理sessionFactory applicationContext.xml 做如下配置:
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="com.hqu.oa"></context:component-scan>
<!-- 加载外部的properties配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据库连接池(c3p0) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置声明式的事务管理(采用基于注解的方式) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
在src创建jdbc.properties文件 内容为:
jdbcUrl = jdbc:mysql:///testoa
driverClass = com.mysql.jdbc.Driver
username = root
password = jerome
这时,hibernate.cfg.xml下的数据连接信息就可以注释了:
测试:
创建SpringTest类 com.hqu.oa.test 下;
代码如下:
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//测试SessionFactory管理是否正确
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
//测试事务
@Test
public void testTransaction() throws Exception {
}
单元测试运行如下:
org.hibernate.impl.SessionFactoryImpl@...
说明成功;
测试事务:
添加Service: TestService在com.hqu.oa.test 下;
添加一个User实体:User在com.hqu.oa.domain下 :
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
写映射文件:
将之前的*.hbm.xml拷贝到com.hqu.oa.domain下:
改名为 User.hbm.xml :
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hqu.oa.domain"> <class name="User" table="hqu_user"> <id name="id"> <generator class="native" /> </id> <property name="name" /> </class> </hibernate-mapping>
在hibernate.cfg.xml 导入映射文件:
<!-- 导入映射文件 -->
<mapping resource="com/hqu/oa/domain/User.hbm.xml" />
在TestService 中保存两个用户:
@Service("testService")
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void saveTwoUsers() {
Session session = sessionFactory.getCurrentSession();
session.save(new User());
//int a = 1 / 0;//这里会抛异常
session.save(new User());
}
}
在SpringTest单元测试:
//测试事务
@Test
public void testTransaction() throws Exception {
TestService testService = (TestService) ac.getBean("testService");
testService.saveTwoUsers();
}
testService 就是上面的 @Service("testService") ;
测试成功;控制台输出两条插入的语句
再测试 : 将TestService 的异常代码打开
单元测试运行,查看数据库没新数据,说明事务成功;
Spring和Hibernate整合完成;
Spring和Struts2整合:
整合之前写一个action 和整合之后做对比:
创建action TestAction在com.hqu.oa.test下:
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("---->>>TestAction.execute()");
return "success";
}
}
在struts.xml配置文件配置:
<package name="default" extends="struts-default" namespace="/"> <!-- 测试用的action --> <action name="test" class="com.hqu.oa.test.TestAction"> <result name="success">/index.jsp</result> </action> </package>
部署,启动tomcat 浏览器输入:http://localhost:8080/TestSSH/test.action
控制台输出:---->>>TestAction.execute()
说明action成功;
接下来做Spring和Struts2整合:
添加F:\Java\struts\struts-2.2.1\lib下的struts2-spring-plugin-2.2.1.jar;
这样就自动整合了 做如下配置:
在web.xml配置spring监听器:用于在应用程序启时初始化
ApplicationContext对象。
<!-- 配置Spring的监听器,用于初始化ApplicationContext对象 --> <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>
在TestService上加上注解:
@Controller
@Scope("prototype") //多例
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("---->>>TestAction.execute()");
return "success";
}
}
修改struts.xml文件:
<package name="default" namespace="/" extends="struts-default"> <!-- 测试用的action,当与Spring整合后,class属性写的就是Spring中bean的名称 --> <action name="test" class="testAction"> <result name="success">/test.jsp</result> </action> </package>
重动 tomcat 测试,成功;
Spring和Struts2整合成功;
三个框架一起用:
在action调用Service:
在 TestAction下:
@Controller
@Scope("prototype")//多例
public class TestAction extends ActionSupport {
@Resource
private TestService testService;
@Override
public String execute() throws Exception {
System.out.println("---->>>TestAction.execute()");
testService.saveTwoUsers();
return "success";
}
}
重启tomcat,事务也管用,成功;完成;
工作量好大~
demo 链接:http://pan.baidu.com/s/1mgDqhmS 密码:zdev
手动添加SSH支持、使用c3p0的更多相关文章
- 手动加入SSH支持、使用c3p0
之前做的笔记,如今整理一下.大家有耐心的跟着做就能成功: SSH(struts2.spring.hibernate) * struts2 * 充当mvc的角色 * hibernate ...
- 读书笔记---《Docker 技术入门与实践》---为镜像添加SSH服务
之前说到可以通过attach和exec两个命令登陆容器,但是如果遇到需要远程通过ssh登陆容器的场景,就需要手动添加ssh服务. 下面介绍两种方法创建带有ssh服务的镜像,commit命令创建和通过D ...
- 为Android设备添加A2SD支持
相信很多用Android设备的用户都有这个问题,内部存储太小导致应用只能装那么几个,虽然rom也有提供移动到sd卡的选项,但是仅仅是移动程序文件到sd卡,并不能解决多少问题,多装几个还是会 ...
- debian添加中文支持
转载:http://www.shunix.com/debian-chinese-support-472/ debian与ubuntu有很大的相似性,但是debian相对更原始,比如在语言支持这一块 ...
- 向 Git 服务器添加 SSH 公钥
. . . . . 在网上很少找到文章有提到如何将自己的 Git 远程仓库配置成可以通过 SSH 公钥方式认证的,而几乎清一色都是告诉你怎么通过 web 界面向 GitHub 添加 SSH 公钥.LZ ...
- 为镜像添加SSH服务
操作Docker容器介绍了一些进入容器的办法,比如attach.exec等命令,但是这些命令都无法解决远程管理容器的问题.因此,当需要远程登录到容器内进行一些操作的时候,就需要SSH的支持了. 如何自 ...
- Windows服务的手动添加和删除方法
Windows服务的手动添加和删除方法 服务,是指执行指定系统功能的程序.例程或进程,以便支持其他程序,尤其是低层(接近硬件)程序.其实,服务就是一种特殊的应用程序,它从服务启动开始就一直处于运行状态 ...
- 添加SSH密钥到GitHub
$ clip < ~/.ssh/id_rsa.pubbash: /c/Users/UsersName/.ssh/id_rsa.pub: No such file or directory [转] ...
- 仅仅需手动添加一行代码就可以让Laravel4执行在SAE (v. 1.0.0)
Github:https://github.com/chariothy/laravel4-sae (已更新至v1.1.0) laravel4-sae (v. 1.0.0) 仅仅需手动添加一行代码就可以 ...
随机推荐
- [BZOJ 3332]旧试题
Description 圣诞节将至.一年一度的难题又摆在wyx面前——如何给妹纸送礼物. wyx的后宫有n人,这n人之间有着复杂的关系网,相互认识的人有m对.wyx想要量化后宫之间的亲密度,于是准备给 ...
- bzoj 2118: 墨墨的等式
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+-+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- [bzoj4763]雪辉&[bzoj4812][Ynoi2017]由乃打扑克
来自FallDream的博客,未经允许,请勿转载,谢谢. cut掉部分题面. 给一个n个点的树,点有点权,有m次询问,每次询问多条链的并有多少种不同的点权以及它的mex mex就是一个集合中最小的没有 ...
- JSP 基本语法
1 JSP 的由来 servlet产生后,存在很大的问题,为了表现页面的效果,需要输出大量的HTML 语句,表现为一个个字符串,不仅利于开发,也不利于后期的维护,由此产生了JSP.主要用于将Servl ...
- Linux查看Tomcat是否多次重启命令
1.查看命令 ps -ef|grep apache-tomcat-9090|grep -v grep|awk '{print $2}' 如果存在两个端口则多次重启, 2.停掉命令: kill -9 ...
- Linux配置服务器的一点总结
一.Linux初始化服务 首先搞清楚四个概念: 进程:正在运行的程序,有自己独立的内存空间. 线程:是进程的下属单位,开销较进程小,没有自己独立的内存空间. 作业:由一系列进程组成,来完成某一项任务. ...
- Java 中 json字符串转换为类
使用到alibaba.fastjson包 具体实现 JSONObject jsonObject = JSONObject.parseObject(msg); SmsSenderStatus smsSe ...
- RTX 无法刷新组织架构的处理方法总结
文章一: 刷新组织架构问题1."客户端不能获取正确的组织架构"或"新增加的人员刷新不了组织架构"首先要判断是RTX服务器引起的异常还是一些客户端出现的异常,判断 ...
- div英文内容超过div长度
添加css word-break: normal;word-wrap: break-word;
- 四柱加强版汉诺塔HanoiTower----是甜蜜还是烦恼
我想很多人第一次学习递归的时候,老师或者书本上可能会举汉诺塔的例子. 但是今天,我们讨论的重点不是简单的汉诺塔算法,而是三柱汉诺塔的延伸.先来看看经典的三柱汉诺塔. 一.三柱汉诺塔(Hanoi_Thr ...