gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:15:29 27 4
gpt4 key购买 nike

我正在使用 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 语句来检查代码的位置正在执行,输出如下:

Before calling dao method 164 varun

50000.0 After template.save method After calling dao method

最佳答案

在 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 自动完成的。

关于mysql - 数据未使用 hibernateTemplate 类插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39827123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com