开发者社区> 问答> 正文

Spring使用JPA,数据提交无效 : 配置报错 

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

展开
收起
kun坤 2020-06-04 13:11:49 715 0
1 条回答
写回答
取消 提交回答
  • 代码有点多!######求大神解决呀!###### 大神来了,,

    <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层
    2020-06-05 13:12:53
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多