java中ssh测试接口方法_SSH入门---框架搭建(eclipse环境下)

前情提要:本文是把Spring、Struts2、Hibernate三大框架整合到一起,搭建整合框架的教程,如需查看各个框架的单独搭建,请看我个人的相关文章。

一、新建动态web项目,导包:

我的项目名称是ssh_crmA

首先下载SSH框架搭建所需完整jar包

密码:ovl9

一共43个包,其中标签库的包是用eclipse这个IDE工具才需要的,如果用的是myeclipse开发,则不需要。

提示:

javassist-3.18.1-GA.jar包与hibernate中的重复,请删除低版本的。最终实际导入的是42个包。

将下载的所有包全部复制到WebContent / WEB-INF / lib下。

二、单独配置spring容器:

1)创建配置文件:

在src下创建名为applicationContext.xml的配置文件。

在配置文件中编写一个根元素。

然后进行第二步。

2)导入约束(4个):

首先下载相关约束文件

密码:fvxu

约束文件下载完后,

请参考spring环境搭建第四点,把四个约束文件全部导入进去。

导入约束文件的作用:让我们的电脑在没有网络的情况下编写配置文件有代码提示功能,能防止不必要的错误发生。

3)创建UserAction:

在src下创建一个包com.zl.web.action,在包下创建一个Action类,名为UserAction。

package com.zl.web.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

}

然后在applicationContext.xml配置文件中把UserAction配置进去。

xmlns="http://www.springframework.org/schema/beans"

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-4.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

4)配置spring的监听器:

在WEB-INF下面的web.xml文件中插入以下代码:

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

三、单独配置struts2:

1)导入约束,编写主配置文件:

在src下创建名为struts.xml的文件。

/p>

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

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

/success.jsp

记得在WebContent根目录下编写一个success.jsp文件,内容随便写,这个文件只是暂时的。

2)配置struts2的核心过滤器:

到web.xml下书写以下代码:

struts2

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

struts2

/*

四、整合struts2和spring:

1)导包:

这个步骤可以跳过,因为在最初的时候,我们已经把全部的包都导入进去了。这个地方要导入的包是struts2-spring-plugin-2.3.24.jar。

2)配置常量:

在com.zl.service包下编写UserService接口:

package com.zl.service;

import com.zl.domain.User;

public interface UserService {

User getUserByCodePassword(User u);

}

然后在com.zl.service.impl包下创建UserServiceImpl实现类:

package com.zl.service.impl;

import com.zl.domain.User;

import com.zl.service.UserService;

public class UserServiceImpl implements UserService {

@Override

public User getUserByCodePassword(User u) {

System.out.println("getUserByCodePassword");

return null;

}

}

注意:此时缺少实体User

在com.zl.domain包下创建User实体类。记得生成get/set方法。

此时,修改applicationContext.xml配置文件:

xmlns="http://www.springframework.org/schema/beans"

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-4.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

e0b354ae32a4

再配置struts.xml文件

/p>

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

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

/success.jsp

e0b354ae32a4

五、单独配置hibernate:

1)导包:

此步骤在刚开始的时候已导入全部的包,跳过

2)为刚刚编写的User实体类编写实体映射文件。

在实体类User的同目录下,创建User.hbm.xml文件。

e0b354ae32a4

注意:其中Customer、LinkMan及其映射文件,这里不需要管,因为这是我做项目的时候所需要的,此处只是搭建ssh环境,只需要User实体类就够了

3)编写主配置文件:

记得先导入约束。

在src下创建hibernate.cfg.xml配置文件:

/p>

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

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

com.mysql.jdbc.Driver

jdbc:mysql:///hibernate1

root

root

org.hibernate.dialect.MySQLDialect

true

true

update

4)编写测试类:

测试hibernate是否配置成功。

新建com.zl.test包,在包中新建测试类HibernateTest

package com.zl.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.junit.Test;

import com.zl.domain.User;

public class HibernateTest {

@Test

public void test(){

Configuration conf = new Configuration().configure();

SessionFactory sf = conf.buildSessionFactory();

Session session = sf.openSession();

Transaction tx = session.beginTransaction();

//--------------------------------------------------

User u = new User();

u.setUser_code("tom");

u.setUser_name("汤姆");

u.setUser_password("12345");

u.setUser_state('2');

session.save(u);

//--------------------------------------------------

tx.commit();

session.close();

sf.close();

}

}

如果数据被写入数据库,则说明hibernate配置成功。

注意:只需要创建一个数据库,与hibernate主配置文件中的数据库名对应,不需要创建表,因为hibernate会帮我们创建表,创建的表与实体类对应。

六、整合Spring和Hibernate:

1)将sessionfactory配置到spring容器中:

在applicationContext.xml配置文件中加入以下代码:

方案一:不推荐使用

方案二:推荐使用

com.mysql.jdbc.Driver

jdbc:mysql:///crm

root

root

org.hibernate.dialect.MySQLDialect

true

true

update

七、整合Spring和C3P0连接池:

首先在src根目录下创建db.properties文件。

jdbc.jdbcUrl=jdbc:mysql:///crm

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.user=root

jdbc.password=root

然后在applicationContext.xml配置文件中加入以下代码:

然后修改第六点的方案二:修改后的代码如下:

org.hibernate.dialect.MySQLDialect

true

true

update

修改步骤:

1)把必选配置的关于连接数据库的注释掉

e0b354ae32a4

2)然后加入一行代码:

e0b354ae32a4

八、整合Hibernate模板(Template)操作数据库:

1)创建UserDao接口:

在src下创建com.zl.dao包,在包中创建UserDao接口:

package com.zl.dao;

import com.zl.domain.User;

public interface UserDao {

//根据登陆名称查询user对象

User getByUserCode(String usercode);

}

2)创建UserDaoImpl实现类:

创建包com.zl.dao.impl,在包中创建UserDaoImpl,实现UserDao接口:

package com.zl.dao.impl;

import org.hibernate.HibernateException;

import org.hibernate.Query;

import org.hibernate.Session;

import org.springframework.orm.hibernate5.HibernateCallback;

import org.springframework.orm.hibernate5.HibernateTemplate;

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

import com.zl.dao.UserDao;

import com.zl.domain.User;

//HibernateDaoSupport 为dao注入sessionFactory

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

private HibernateTemplate ht;

@Override

public User getByUserCode(final String usercode) {

//HQL操作

return getHibernateTemplate().execute(new HibernateCallback() {

@Override

public User doInHibernate(Session session) throws HibernateException {

String hql="from User where user_code=?";

Query query = session.createQuery(hql);

query.setParameter(0, usercode);

User user = (User) query.uniqueResult();

return user;

}

});

//Criteria操作

/*

* DetachedCriteria dc = DetachedCriteria.forClass(User.class);

*

* dc.add(Restrictions.eq("user_code", usercode));

*

* List list = (List)

* getHibernateTemplate().findByCriteria(dc);

*

* if(list != null && list.size()>0){

* return list.get(0);

* }else{

* return null;

* }

*/

}

}

3)在spring中配置dao:

在applicationContext.xml配置文件中加入以下代码:

e0b354ae32a4

九、整合Spring的aop事务:

方案一:通过xml配置aop事务:

1)配置核心事务管理器:

在applicationContext.xml配置文件中写入以下代码:

e0b354ae32a4

2)配置通知:

在核心事务管理器下方写入以下代码:

3)将通知织入目标对象:

在配置通知的下方写入以下代码:

方案二:通过注解配置aop事务:

十、扩大Session的作用范围:

为了避免使用懒加载时出现no session问题。

1)配置filter:

在WEB-INF下的web.xml中配置filter:

openSessionInView

org.springframework.orm.hibernate5.support.OpenSessionInViewFilter

openSessionInView

/*

特别注意:因为一旦进入struts的流程,struts就不会放行,所以要把下面的代码放入到struts核心过滤器的前面:

openSessionInView

/*

也就是说,最终的代码相对位置应该是下面的形式:

openSessionInView

org.springframework.orm.hibernate5.support.OpenSessionInViewFilter

struts2

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

openSessionInView

/*

struts2

/*

e0b354ae32a4

最终的web.xml文件代码如下:

ssh_crmA

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

openSessionInView

org.springframework.orm.hibernate5.support.OpenSessionInViewFilter

struts2

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

openSessionInView

/*

struts2

/*

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

最终的applicationContext.xml文件代码如下:

xmlns="http://www.springframework.org/schema/beans"

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-4.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

org.hibernate.dialect.MySQLDialect

true

true

update

最终的struts.xml文件代码如下:

/p>

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

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

/success.jsp

至于为什么没有贴hibernate.cfg.xml的代码,因为hibernate的主配置文件已经注入在spring的主配置文件中,hibernate的主配置文件已经没有用了,相当于没有这个文件。

到此位置,SSH三大框架的整合框架的搭建已经全部完成。

本文仅供学习参考之用,转载请注明来源。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值