Hibernate + Spring不会保存记录

时间:2015-08-27 10:49:39

标签: java spring hibernate spring-mvc jpa

我尝试在我的数据库中添加记录但没有保存。我寻找一些解决方案但没有任何效果。当我尝试从数据库中删除用户时它不起作用。任何人都可以告诉我什么是错的

@Transactional
public void addUser(User user) {
    userDao.addUser(user);
}   @Transactional
public List getAllUsers() {
    return userDao.getAllUsers();
}
@Autowired 
private UserDao userDao;

控制器

@RequestMapping(value = "/Users")
public String goUsers(Map <String,Object> map)
{
    User user= new User();
    map.put("user",user);
    map.put("AllUsers",UserS.getAllUsers());
    return "Users";
}

@RequestMapping(value = "/SuppUser",method=RequestMethod.POST)
public String goUsers(@ModelAttribute User user,BindingResult result,@RequestParam String action,Map<String,Object> map) 
{
    User userResult = new User();
    switch(action.toLowerCase()){
    case "Ajouter":
        UserS.addUser(user);
        userResult= user;
        break;
    case "Rechercher":
        User userSearch = UserS.serachUserByLogin(user.getIdUser()) ;
        userResult= userSearch != null ? userSearch : new User();
        break;
    case "Supprimer":
        UserS.deleteUser(user.getIdUser());
        userResult= user;
        break;
    }
    map.put("user", userResult);
    map.put("AllUsers", UserS.getAllUsers());
    return "Users"; 
}

用户Dao

@Autowired
private SessionFactory session;

@Override
public void addUser(User user) {
    session.getCurrentSession().save(user);
}

Jsp文件

</div>
<form:form action="SuppUser" method="POST" commandName="user">
<pre>
User ID
<form:input path="idUser"/>
<br>
Nom
<form:input path="nom"/>
<br>
Prenom
<form:input path="prenom"/>
<br>
email
<form:input path="email"/>
<br>
Login
<form:input path="login"/>
<br>
Password
<form:password path="pw"/>
<br>
Sexe
<form:input path="sexe"/>
<br>
Telephone
<form:input path="tel"/>
<br>
Type
<form:input path="typeUser"/>
<br>
</pre>
<input type="submit" name="action" value="Ajouter">
<input type="submit" name="action" value="Rechercher">
<input type="submit" name="action" value="Supprimer">


</form:form>

JDBC

` jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.url=jdbc:mysql://localhost:3306/novec_covoiturage
jdbc.username=root
jdbc.password=`

hibernate

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
    <session-factory>
        <mapping class="com.app.cov.model.User" />
    </session-factory>
</hibernate-configuration>

Servlet

    <annotation-driven />


<context:property-placeholder location="/jdbc.properties"/>
<!--Data source has the database information -->
 <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <beans:property name="driverClassName" value="${jdbc.driverClassName}"/>
     <beans:property name="url" value="${jdbc.url}"/>
     <beans:property name="username" value="${jdbc.username}"/>
      <beans:property name="password" value="${jdbc.password}"/>
  </beans:bean>

  <!-- SessonFactory for Hibernate -->
    <beans:bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="configLocation">
            <beans:value>classpath:hibernate.cfg.xml</beans:value>
        </beans:property>
        <beans:property name="configurationClass">
             <beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">${jdbc.dialect}</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
   </beans:bean>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>
<tx:annotation-driven />
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"></beans:property>
</beans:bean>
<context:component-scan base-package="com.app.cov" />

Hibernate cfg

0 个答案:

没有答案