数据未使用hibernateTemplate类插入数据库

时间:2016-10-03 08:12:43

标签: mysql spring hibernate

我正在使用spring和hibernate集成,并尝试使用MYSQL作为后端创建一个简单的CRUD应用程序。

Here is my Employee.java 


package com.springhibernate;


public class Employee {
    private int id;
    private String name;
    private float salary;

    public Employee() {
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

}

EmployeeDao.java

    package com.springhibernate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate4.HibernateTemplate;

public class EmployeeDao {
    HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    // method to save employee
    public void saveEmployee(Employee e) {
        System.out.println(e.getId());
        System.out.println(e.getName());
        System.out.println(e.getSalary());
        template.save(e);
        System.out.println("After template.save method");
    }

    // method to update employee
    public void updateEmployee(Employee e) {
        template.update(e);
    }

    // method to delete employee
    public void deleteEmployee(Employee e) {
        template.delete(e);
    }

    // method to return one employee of given id
    public Employee getById(int id) {
        Employee e = (Employee) template.get(Employee.class, id);
        return e;
    }

    // method to return all employees
    public List<Employee> getEmployees() {
        List<Employee> list = new ArrayList<Employee>();
        list = template.loadAll(Employee.class);
        template.setCheckWriteOperations(false);
        return list;
    }

}

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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/nupur" />
        <property name="username" value="root" />
        <property name="password" value="nupur" />
    </bean>

    <bean id="mysessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>

        <property name="mappingResources">
            <list>
                <value>Employee.hbm.xml</value>
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>

            </props>
        </property>
    </bean>

    <bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="mysessionFactory"></property>
        <property name="checkWriteOperations" value="false"></property>
    </bean>

    <bean id="d" class="com.springhibernate.EmployeeDao">
        <property name="template" ref="template"></property>
    </bean>
</beans>

InsertTest.java

package com.springhibernate;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

@SuppressWarnings("deprecation")
public class InsertTest {

    public static void main(String[] args) {

        Resource r = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(r);

        EmployeeDao dao = (EmployeeDao) factory.getBean("d");

        Employee e = new Employee();
        e.setId(164);
        e.setName("varun");
        e.setSalary(50000);

        System.out.println("Before calling dao method");
        dao.saveEmployee(e);
        System.out.println("After calling dao method");
    }

}

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 3, 2016 1:19:10 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.springhibernate.Employee" table="EMPLOYEE">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="salary" type="float">
            <column name="SALARY" />
        </property>
    </class>
</hibernate-mapping>

控制台显示没有错误,数据没有插入数据库,请告诉我哪里出错了?

我已经打印了一些sout语句来检查代码 正在执行,输出如下:

  

在调用dao方法164 varun之前

     

50000.0在template.save方法之后调用dao方法

2 个答案:

答案 0 :(得分:1)

在EmployeeDao.java类中添加声明式事务管理,如下所示。

@Transactional
public void saveEmployee(Employee e) {
    System.out.println(e.getId());
    System.out.println(e.getName());
    System.out.println(e.getSalary());
    template.save(e);
    System.out.println("After template.save method");
}

在Spring中,您应该使用声明式事务管理,它允许您避免打开,提交,关闭和刷新。这一切都由Spring自动完成。

答案 1 :(得分:0)

我还没有查看Eclipse,但我有一些想法:

  1. 模型类应该完全具有构造函数,特别是如果对DaoImpl类使用@Autowired。这里他们是Employee()和Employee(id,name,salary)
  2. 在DaoImpl类中添加@Repository,在Controller中添加@ Controller,在DaoImpl中添加@Transactional,如果使用事务,在服务类中使用@Service。
    这里你注册了,所以不需要@Repository