020:ID生成策略XML配置(连接mysql)

前端之家收集整理的这篇文章主要介绍了020:ID生成策略XML配置(连接mysql)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

020:ID生成策略XML配置(连接MysqL

1、工程-----右键-----new-----Source Folder【test】

下面对ID生成测试

test下面新建一个包和类:com.bjsxt.hibernate-----HibernateIDTest.java

规定:

(1)在类的后面加test表示这是一个测试类,在方法的前面加Test表示这是一个测试类,在方法的前面加Test表示这是一个测试方法

<id name="id" >
		<generator class="native"></generator>
		</id>


(2)uuid,native是一种ID生成算法

private int id;

……

private Int getId(){

return id;

}

private void setId(int id){

this.id=id;

}

(3)

自动生成,不再设制StudentTest,java里面的s.setId(1);

*******************************************************

Student.java

package com.bjsxt.hibernate;

public class Student {
      private int id;
      private String name;
      private int age;
	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 int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
      
}

Teacher.java

package com.bjsxt.hibernate;


import java.beans.Transient;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;



@Entity
public class Teacher {
      private int id;
      private String name;
      private String title;
      private String yourwifeName;
      private Date birthDate;
      private ZhiCheng zhiCheng;
      
      @Enumerated(EnumType.STRING)
      public ZhiCheng getZhiCheng() {
		return zhiCheng;
	}
	public void setZhiCheng(ZhiCheng zhiCheng) {
		this.zhiCheng = zhiCheng;
	}
	@Temporal(TemporalType.DATE)
      public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}
	@Transient
	public String getYourwifeName() {
		return yourwifeName;
	}
	public void setYourwifeName(String yourwifeName) {
		this.yourwifeName = yourwifeName;
	}
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
    @Column
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
      
}



Student.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">

<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Student" table="student">
		<id name="id" >
		<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<property name="age"></property>
		
    </class>
	
</hibernate-mapping>

HibernateIDTest.java

package com.bjsxt.hibernate;

import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.bjsxt.hibernate.Student;


public class HibernateIDTest {
	private static SessionFactory sessionFactory;
	
	@BeforeClass
	public static void beforeClass() {
			try {
				sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
			} catch (HibernateException e) {
				
				e.printStackTrace();
			}
	}
	@AfterClass
	public static void afterClass() {
		sessionFactory.close();
	}
	
	@Test
	public void testStudentSave() {
	
		
		Student s = new Student();
		s.setName("zhangsan");
		s.setAge(8);
		
		
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(s);
		session.getTransaction().commit();
	}
	
	/*
	@Test
	public void testTeacherSave() {
	
		Teacher t = new Teacher();
		t.setId(1);
		t.setName("t1");
		t.setTitle("middle");
		t.setBirthDate(new Date());
		
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();
	}
	*/
	public static void main(String[] args) {
		beforeClass();
	}
}


hibernate.cfg.xml

<!-- Database connection settings -->
        <!-- 连接数据库 -->
        <property name="connection.driver_class">com.MysqL.jdbc.Driver</property>
        <property name="connection.url">jdbc:MysqL://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>


 <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
原文链接:https://www.f2er.com/xml/298289.html

猜你在找的XML相关文章