我在做spring springmvc hibernate 的練習 下面是我的配置文件dispatcherServlet.xml和applicationContext的配置文件
我現在只是想在上傳圖片時順便把路徑寫到資料庫但是每次都會出現異常Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
看起來好像是我交易沒作用還是怎樣之類的 但是該加的我都有加 還是一直出現 參數傳遞都沒有問題 這問題困擾我很久了 請求各位幫忙
我沒有用到spring boot的東西 那個 @Modifying對我不起作用 我覺得可能是我配置文件出錯
dispatcherServlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.web.pet.member.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSizePerFile">
<value>10485760</value>
</property>
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<context:annotation-config />
</beans>
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:tt="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:annotation-config />
<mvc:annotation-driven />
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="jdbcUrl"
value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=PetDB"></property>
<property name="user" value="scott"></property>
<property name="password" value="tiger"></property>
<property name="initialPoolSize" value="3"></property>
<property name="minPoolSize" value="3"></property>
<property name="maxPoolSize" value="5"></property>
<property name="acquireIncrement" value="3"></property>
<property name="maxStatements" value="8"></property>
<property name="maxStatementsPerConnection" value="5"></property>
<property name="maxIdleTime" value="1800"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<array>
<value>com.web.pet.member.model</value>
<value>com.web.pet.mom.model</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">false</prop>
<prop key="connection_pool_size">8</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext
</prop>
</props>
</property>
</bean>
<tx:annotation-driven
transaction-manager="transactionManager" proxy-target-class="true" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<context:component-scan
base-package="com.web.pet.member.service" />
<context:component-scan
base-package="com.web.pet.member.dao" />
</beans>
我的controller
package com.web.pet.member.controller;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.web.pet.member.model.Member;
import com.web.pet.member.service.MemberService;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
@RequestMapping("/Gusty")
@Controller
public class MemberCURD {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
private static final String CHARSET_CODE = "UTF-8";
@Autowired
private MemberService memberService;
@RequestMapping(value="/upload")//照片上傳
public void photoupload(MultipartFile image,HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException {
PrintWriter out = response.getWriter();
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding(CHARSET_CODE);
List<Member> list = new ArrayList<Member>();
Member member = new Member();
list = memberService.ajaxloginService(Integer.valueOf(request.getSession().getAttribute("user").toString()));
if (!image.isEmpty()) {
String path="C:\\AdvancedWebWorkSpace\\PetProject_Final\\src\\main\\webapp\\Member\\image\\"+list.get(0).getName()+"\\";
String filename = image.getOriginalFilename();
File filepath = new File(path, filename);
if (!filepath.isDirectory()) {
filepath.getParentFile().mkdir();
}
image.transferTo(new File(path + File.separator + filename));
member.setU_Id(Integer.valueOf(request.getSession().getAttribute("user").toString()));
member.setGender("女");
member.setImg("image/"+list.get(0).getName()+"/"+filename);
if(memberService.fileuploadService(member)>0) {
out.print("<script>");
out.print("window.alert('照片上傳成功'); window.location.href='../Member/Member.jsp';");
out.print("</script>");
}else {
out.print("<script>");
out.print("window.alert('照片上傳失敗'); window.location.href='../Member/Member.jsp';");
out.print("</script>");
}
}
else {
out.print("<script>");
out.print("window.alert('請選擇照片'); window.location.href='../Member/Member.jsp';");
out.print("</script>");
}
}
}
我的Service
package com.web.pet.member.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import com.web.pet.member.dao.MemberDao;
import com.web.pet.member.model.Member;
@EnableTransactionManagement
@Transactional
@Service
public class MemberService {
@Autowired
MemberDao memberDao;
public int fileuploadService(Member member) {
return memberDao.fileuploadDao(member);
}
}
我的Dao
package com.web.pet.member.dao;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.web.pet.member.model.Member;
@Repository
public class MemberDao {
@Autowired
private SessionFactory sessionFactory;
public int fileuploadDao(Member member) {
int count=0;
System.out.println(member.getU_Id());
System.out.println(member.getGender());
System.out.println(member.getImg());
sessionFactory.getCurrentSession().merge(member);
String hql="UPDATE Member set img = :userimg where u_Id = :userid";
count = sessionFactory.openSession().createQuery(hql)
.setParameter("userimg", member.getImg())
.setParameter("userid", member.getU_Id())
.executeUpdate();
return count;
}
}