Hibernate和Spring没有整合:
1.struts2中文乱码,在struts.xml中加(package之外)
<constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="gbk" />
(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问题:
(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>
Class<T> clazz=(Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
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/context xmlns: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>
https://git.oschina.net/zzhao114/SSH.git