Spring整合Hibernate

Spring+Hibernate5.4
1.创建Web项目,引入jar包,配置Spring
这个之前的文章已经讲过了就不再赘述
2.整合Hibernate
整合hibernate主要是整合会话到applicationContext.xml文件中
有两种整合方式,第一种是直接在applicationContext.xml文件中调用Hibernate.cfg.xml,这种方式不推荐使用
第二种是把Hibernate.cfg.xml的核心配置配置到applicationContext.xml文件中

appicationContext.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/cache" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/jdbc/spring-cache.xsd">







							
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">


 

<property name="hibernateProperties">
<props>
 <prop key="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driverprop>
 <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/login_database?serverTimezone=GMTprop> 
 <prop key="hibernate.connection.username">rootprop>
 <prop key="hibernate.connection.password">zby001231prop>
 <prop key="hibernate.format_sql">org.hibernate.dialect.MySQLDialectprop>
 
 
 <prop key="hibernate.show_sql">trueprop>
 <prop key="hibernate.format_sql">trueprop>
 <prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>

 <property name="mappingDirectoryLocations" value="classpath:domain">property>

 bean>
beans>

Employee.java文件

package domain;

import java.util.Date;

public class Employee {
     
	private Integer id;
	private String name;
	private String email;
	private Date hiredate;
	
	public Integer getId() {
     
		return id;
	}
	public void setId(Integer id) {
     
		this.id = id;
	}
	public String getName() {
     
		return name;
	}
	public void setName(String name) {
     
		this.name = name;
	}
	public String getEmail() {
     
		return email;
	}
	public void setEmail(String email) {
     
		this.email = email;
	}
	public Date getHiredate() {
     
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
     
		this.hiredate = hiredate;
	}


		public Employee( String name, String email, Date hiredate) {
     
		
		
			this.name = name;
			this.email = email;
			this.hiredate = hiredate;

		}
		public Employee(){
     }
	
}

Employee.hbm.xml



 <hibernate-mapping>
 
 <class name="domain.Employee" table="employee">
 
 
 <column name="id"/>
   <generator class="native"/>
 id>
 <property name="email" type="java.lang.String">
 <column name="email" length="64"/>
 property>
 <property name="hiredate" type="java.util.Date">
 <column name="hiredate"/>
 property>
 <property name="name" type="java.lang.String">
 <column name="name" length="64"/>
 property>

 class>
 hibernate-mapping>

测试文件testSpring.java

package Test;

import static org.junit.Assert.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jpa.criteria.expression.function.TrimFunction;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import domain.Employee;
import string.TestService;


public class testSpring {
     

	@Test
	public void test() {
     
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");
		
		Session s=sf.openSession();
		Transaction tx=s.beginTransaction();
		Employee employee=new Employee("aa","[email protected]",new java.util.Date());
		
	
		s.save(employee);

		tx.commit();
		System.out.println("成功");
		//关闭session
		if(s.isOpen()){
     
			s.close();
		}
	}

}



最后,一定要在数据库里面把ID设置为自动递增,否则会报Field ‘id’ doesn’t have a default value
Spring整合Hibernate_第1张图片

你可能感兴趣的:(J2EE,hibernate,spring,java)