SSH配置

来源:互联网 发布:网络摄像头用户名密码 编辑:程序博客网 时间:2024/06/05 03:46


Hibernate和Spring没有整合:


1.struts2中文乱码,在struts.xml中加(package之外)

<constant name="struts.devMode" value="true" /><constant name="struts.i18n.encoding" value="gbk" />


2.hibernate问题:

(1) 中文乱码,在hibernate.cfg.xml中加入

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk

(2) 使用flush时同步更新数据库,在hibernate.cfg.xml中加入

<property name="connection.autocommit">true</property>

(3) hibernate自动创建数据库,在hibernate.cfg.xml中加入
<property name="hibernate.hbm2ddl.auto">update</property><property name="myeclipse.connection.profile">MySQL</property><property name="current_session_context_class">thread</property>
3.Spring问题:
(1) 配置时,需要加入Spring Web包库

(2) 自动注入 ,注入后需要在类中需要加Getters和Setters方法

<bean autowire="byName" name="userAction" class="com.Action.userAction" />

(3) 如果不需要注入单例

<bean id="user" class="com.VO.User" scope="prototype" />

(4) 不使用注入方法的话

Resource resource=new ClassPathResource("applicationContext.xml");BeanFactory factory =new XmlBeanFactory(resource);user =(User)factory.getBean("user");

(5) 如果applicationContext.xml没有放在WEB-INF下而是在src中,需要在web.xml中配置

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param>

(6) 需要在web.xml中加入一个监听器

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>


DAO使用时,若用BaseDAO,需要的反射语句
Class<T> clazz=(Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

Spring3.1和Hibernate4.1整合
简单说明:
1.整合后hibernate配置文件不再需要,同时SessionFactory也整合到了Spring的配置文件
2.所有的实例,都是通过注释的方式获得,通过扫描包,Spring自动加载注册bean
3.sessionFactory通过实例注入获得,使用sessionFactory.getCurrentSession()方式获得session
4.事务由Spring管理,session的开启关闭也由Spring管理

Spring的配置文件:

<?xml version="1.0" encoding="UTF-8"?> xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  <property name="driverClassName" value="${jdbc.driver}" />  <property name="username" value="${jdbc.username}" />  <property name="password" value="${jdbc.password}" />  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  </property>  <property name="hibernateProperties">    <prop key="hibernate.dialect">     org.hibernate.dialect.MySQLDialect    <prop key="hibernate.autoReconnect">true</prop>    <prop key="hibernate.hbm2ddl.auto">update</prop>  <property name="mappingResources">    <value>com/VO/User.hbm.xml</value>    <value>com/VO/Idcard.hbm.xml</value>    <value>com/VO/Province.hbm.xml</value>   </list>  </property> <bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <tx:annotation-driven transaction-manager="transactionManager" /></beans><beans xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/contextxmlns:tx="http://www.springframework.org/schema/tx"><context:component-scan base-package="com.DAOImpl,com.VO,com.BaseDAO" /><!-- 导入mysql属性信息  --><context:property-placeholder location="classpath:mysql.properties" /><!-- 配置数据源 -->destroy-method="close"><property name="url" value="${jdbc.url}" /></bean><bean id="sessionFactory"<property name="dataSource"><ref bean="dataSource" /><props></prop></props></property><list><value>com/VO/Role.hbm.xml</value></bean><property name="sessionFactory" ref="sessionFactory" /></bean>






SSH框架完整例子

https://git.oschina.net/zzhao114/SSH.git









3 0
原创粉丝点击