ssh 框架引入service_搭建ssh框架相关配置文件总结

搭建ssh框架相关配置文件总结

第一步 导入jar包

第二步 搭建struts2环境

(1)创建action(继承actionnsuport),创建struts.xml配置文件,配置actionpublic class UserAction extends ActionSupport{}<?xml  version="1.0" encoding="UTF-8"?>

struts PUBLIC

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

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

(2)配置struts2的过滤器,在web.xml中

struts2

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

struts2

/*

第三步 搭建hibernate环境

(1)创建实体类,Userpublic class User {

private Integer uid;

private String username;

private String password;

private String email;

private String code;

private Boolean state;

//再生成set/get方法

}

实体类编写规则1 实体类里面属性私有的

2 私有属性使用公开的set和get方法操作

3 要求实体类有属性作为唯一值(一般使用id值)

4 实体类属性建议不使用基本数据类型,使用基本数据类型对应的包装类

(1)八个基本数据类型对应的包装类

- int–Integer

- char—Character、

- 其他的都是首字母大写 比如double–Double

(2)比如 表示学生的分数,假如int score;

- 比如学生得了0分 ,int score = 0;

- 如果表示学生没有参加考试,int score = 0;不能准确表示学生是否参加考试

l解决:使用包装类可以了, Integer score = 0,表示学生得了0分,

l表示学生没有参加考试,Integer score = null;

(2)配置实体类和数据库表映射关系,User.hbm.xml<?xml  version="1.0" encoding="UTF-8"?>

hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

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

(3)创建hibernate核心配置文件,hibernate.cfg.xml

- 引入映射配置文件

(配置文件需要引入约束)<?xml  version="1.0" encoding="UTF-8"?>

hibernate-configuration PUBLIC

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

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

true

true

update

org.hibernate.dialect.MySQLDialect

第四步 搭建spring环境

(1)创建spring核心配置文件<?xml  version="1.0" encoding="UTF-8"?>

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

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

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

(2)让spring配置文件在服务器启动时候加载,在web.xml中

- 配置监听器

- 指定spring配置文件位置

contextConfigLocation

classpath:bean.xml

......

org.springframework.web.context.ContextLoaderListener

第五步 struts2和spring整合

(1)把action在spring配置(action多实例的)

(2)在struts.xml中action标签class属性里面写 bean的id值bean.xml

struts.xml

第六步 spring和hibernate整合

(1)把hibernate核心配置文件中数据库配置,在spring里面配置

(2)把hibernate的sessionFactory在spring配置

第七步 完成互相注入关系(在dao里面使用hibernateTemplate)

(1)在dao注入hibernateTemplate对象

(2)在hibernateTemplate对象中注入sessionFactorypublic class UserAction extends ActionSupport{

private UserService userService;

public void setUserService(UserService userService) {

this.userService = userService;

}

}@Transactional

public class UserService {

private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

}

/**

问题:如果在service类上面没有添加注解,出现异常

问题:只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。

错误原因:

OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。

然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,

最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。

在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。

也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。

所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。

*/public interface UserDao {}public class UserDaoImpl extends HibernateDaoSupport implements UserDao {}

最后结构图:

测试:启动服务器(我试过tomcat和resin),如果没有报错,并且数据库中存在数据表,则说明成功。

注意:数据库必须有,表可以没有;log4j.properties是一个可以显示详细日志信息的配置文件。

补:

在web.xml中在配置如下,原因是防止"no session"问题

openSessionInViewFilter

org.springframework.orm.hibernate5.support.OpenSessionInViewFilter

openSessionInViewFilter

/*

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值