Spring使用JPA,数据提交无效

wj2699 发布于 2013/06/06 09:44
阅读 1K+
收藏 1

applicationContext.xml配置文件如下:


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<context:component-scan base-package="com.linksaint.oa.form">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<context:annotation-config />

	<tx:annotation-driven />

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
		<property name="url"
			value="jdbc:sqlserver://192.168.137.1:1433;databaseName=test" />
		<property name="username" value="sa" />
		<property name="password" value="123456" />
	</bean>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
		<property name="jpaDialect" ref="jpaDialect" />
	</bean>

	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="catalog" />
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
	</bean>

	<bean id="jpaVendorAdapter"
		class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
		<property name="showSql" value="true" />
		<property name="generateDdl" value="true" />
	</bean>

	<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

</beans>
Spirng MVC json-servlet.xml  的配置文件如下:



<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<context:component-scan base-package="com.linksaint.oa.form">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<mvc:annotation-driven />

</beans>


persistence.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
	version="1.0">
	<persistence-unit name="catalog" transaction-type="RESOURCE_LOCAL" />
</persistence>

保存数据代码:

@Transactional
	public void stroe(Catalog catalog) {
		entityManager.persist(catalog);
		//entityManager.merge(catalog);
	}
 @RequestMapping(value = "/catalog/add.json", method = RequestMethod.POST)
 @ResponseBody
 public Response add(@RequestBody Catalog catalog) {
     catalogDao.stroe(catalog);
     return Response.success();
 }

不知道配置哪边有问题,数据怎么也提交不到数据库。spring版本3.2,hibernate版本4.2

加载中
0
wj2699
wj2699
代码有点多!
0
wj2699
wj2699
求大神解决呀!
0
唐代de豆腐
唐代de豆腐

大神来了,,

<context:component-scan base-package="com.linksaint.oa.form">
12         <context:include-filter type="annotation"
13             expression="org.springframework.stereotype.Controller" />

14     </context:component-scan>
把那个om.linksaint.oa.form定位到web下 确保不会扫描到service层

返回顶部
顶部