使用的都是最新的版本,在junit测试是顺利的,但是放到web上测试始终都无法插进数据库,也不报错,弄了很久,郁闷死了。再网上查了资料,大多数人都涉及到事物,但是我已经添加了注解了并且做了配置了。
下面是配置文件
db-context.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: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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- DBCP DataSource -->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/spring" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- JPA EntityManagerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Persistence Context Annotated -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
id="persistenceAnnotationBeanPostProcessor" />
<!-- Transaction -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
persistence.xml(在META-INF下)
<?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_2_0.xsd"
version="2.0">
<persistence-unit name="persist" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.transaction.flush_before_completion"
value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
</persistence>
Person.java
@Entity
public class Person {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
// ...
}
PersonDaoImpl.java
@Repository
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class PersonDaoImpl implements PersonDao {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Person persist(Person person) {
entityManager.persist(person);
return person;
}
// ...
}
PersonController.java
@Controller
@Scope("prototype")
@RequestMapping("/person")
public class PersonController {
private PersonService personService;
@Inject
public void setPersonService(PersonService personService) {
this.personService = personService;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView add(@RequestParam("username") String name) {
Person person = new Person();
person.setName(name);
System.out.println(name); // 可以获取到
personService.create(person); // 不报错,但是检查数据库没有
ModelAndView mav = new ModelAndView();
mav.setViewName("result");
return mav;
}
// ...
}
PersonServiceTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-context.xml")
public class PersonControllerTest {
private PersonController controller;
@Inject
public void setController(PersonController controller) {
this.controller = controller;
}
// 这个测试是通过的,(加事物注解和不加都通过,不加直接写进数据库)
<a href="http://my.oschina.net/test45" class="referer" target="_blank">@Test</a>
public void testAdd() {
// PersonController controller = new PersonController();
assertEquals("longkai", controller.add("longkai").getModelMap().get("haha"));
}
}
省略了springmvc和context基于注解的代码,还有一个service类(直接调用dao方法),这个问题困扰我好久了,恳请各位帮忙解决一下!不甚感激啊。。。
springmvc配置文件中将 配置到controller那一层,别配置在基础包上面。
第二srping配置文件中将controller排除掉:
<context:component-scan base-package="com.famous.easybug">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。