0
回答
spring3和hibernate4
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" >

	<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
	<context:component-scan base-package="*" />

	<!-- 启动注解 -->
	<mvc:annotation-driven/>
	<mvc:default-servlet-handler/>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 配置数据源 -->
	<bean id="dataSource"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/jpatest?characterEncoding=UTF-8&amp;</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>123456</value>
		</property>
	</bean>

	<!--配置SessionFactory -->
	<bean id="sessionFactory"
	class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="annotatedClasses">
			<list>
				<value>model.Student</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.current_session_context_class">thread</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql" >true</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
			</props>
		</property>
	</bean>

	<!-- 事务管理 -->
	<bean id="transactionManager"
	class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
举报
CodeGeass
发帖于5年前 0回/356阅

以下是问题补充:

  • @CodeGeass : <prop key="hibernate.current_session_context_class">thread</prop> 不加报错 No Session found for current thread 加了也报错 save is not valid without active transaction​ (5年前)
  • @CodeGeass :而且会创建两次表 我在实体类用的注解 下面是部分代码 @Entity public class Student { private Integer stuId; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Integer getStuId() { return stuId; } (5年前)
  • @CodeGeass :<prop key="hibernate.current_session_context_class">thread</prop> 问题是解决了 我想知道这个属性为什么会和事务冲突? (5年前)
顶部