SpringData无法插入数据

Freefish1994 2017-04-22 11:33:51
只能查询数据库中的信息无法插入,控制台只会打印查询语句,插入数据时没有反应,后台无报错,Debug加断点后发现前台的表单数据也传到后台,但是数据库就是无法插入数据,用的数据库是MySQL
applicationContext.xml配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.website"></context:component-scan>
<bean id="config"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${mydb.username}"></property>
<property name="password" value="${mydb.password}"></property>
<property name="url" value="${mydb.url}"></property>
<property name="driverClassName" value="${mydb.driverClassName}"></property>
<property name="maxActive" value="${connection.maxActive}"></property>
<property name="maxIdle" value="${connection.maxIdle}"></property>
<property name="minIdle" value="${connection.minIdle}"></property>
<property name="maxWait" value="${connection.maxWait}"></property>
<property name="removeAbandoned" value="${connection.removeAbandoned}"></property>
<property name="removeAbandonedTimeout" value="${connection.removeAbandonedTimeout}"></property>
<property name="logAbandoned" value="${connection.logAbandoned}"></property>
</bean>

<!-- 配置 JPA 的 EntityManagerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<property name="packagesToScan" value="com.website"></property>
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
</props>
</property>
</bean>

<!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>

<!-- 配置基于注解的事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 配置 Spring Data -->
<jpa:repositories base-package="com.website.core"
entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
</beans>


UserService层
	@Transactional(readOnly = false)
public void addOrModifyUser(final UserDTO userDTO) {
User user = new User();
if (userDTO != null) {
if (StringUtils.isBlank(userDTO.getId())) {
userDTO.setPassword(MD5Util.MD5Encode(userDTO.getPassword()));
userRepository.saveAndFlush(this.buildUserFromUserDTO(userDTO,user));
} else {
userDTO.setPassword(userRepository.findOne(userDTO.getId()).getPassword());
userRepository.saveAndFlush(this.buildUserFromUserDTO(userDTO,user));
}
}
}

private User buildUserFromUserDTO(final UserDTO userDTO, final User user) {
if(StringUtils.isNotBlank(userDTO.getId()))
user.setId(userDTO.getId());
user.setName(userDTO.getName());
user.setPassword(userDTO.getPassword());
user.setLogin(userDTO.getLogin());
user.setSex(Integer.valueOf(userDTO.getSex()));
user.setPhone(userDTO.getPhone());
user.setEmail(userDTO.getEmail());
user.setRole(Integer.valueOf(userDTO.getRole()));
return user;
}
...全文
174 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧