Chinaunix首页 | 论坛 | 博客
  • 博客访问: 256047
  • 博文数量: 45
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 488
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-13 14:43
文章分类

全部博文(45)

文章存档

2014年(2)

2013年(35)

2012年(8)

我的朋友

分类: Java

2013-07-29 20:11:35

    环境:hibernate-release-4.2.3.Final; junit-4.10.jar以及mysql-connector-java-commercial-5.1.25-bin.jar

    hibernate.cfg.xml
点击(此处)折叠或打开
  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.         "">

  5. <hibernate-configuration>

  6.     <session-factory>

  7.         <!-- Database connection settings -->
  8.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9.         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
  10.         <property name="connection.username">root</property>
  11.         <property name="connection.password">root</property>

  12.         <!-- JDBC connection pool (use the built-in) -->
  13.         <!-- <property name="connection.pool_size">1</property> -->

  14.         <!-- SQL dialect -->
  15.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

  16.         <!-- Enable Hibernates automatic session context management -->
  17.         <!-- <property name="current_session_context_class">thread</property> -->

  18.         <!-- Disable the second-level cache -->
  19.         <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

  20.         <!-- Echo all executed SQL to stdout -->
  21.         <property name="show_sql">true</property>

  22.         <!-- Drop and re-create the database schema on startup -->
  23.         <property name="hbm2ddl.auto">create</property>

  24.         <!-- <mapping resource="com/hsh/hibernate/model/Student.hbm.xml"/> -->
  25.         <mapping class="com.hsh.hibernate.model.Teacher"/>

  26.     </session-factory>

  27. </hibernate-configuration>

    
Teacher.java
点击(此处)折叠或打开
  1. package com.hsh.hibernate.model;

  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.EnumType;
  5. import javax.persistence.Enumerated;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. import javax.persistence.Table;
  10. import javax.persistence.TableGenerator;

  11. @Entity
  12. /*@TableGenerator(
  13.     name="TEACHER_GEN",
  14.     table="TEACHER_GENERATOR_TABLE",//数据库中表名,使用键值对字段名
  15.     pkColumnName = "PK_key",//数据库中字段名
  16.     valueColumnName = "PK_VALUE",//数据库中字段名
  17.     pkColumnValue="_Teacher",//对应的数据库
  18.     allocationSize=20//步长,表示PK_VALUE字段值减去1 再乘以20就是所对应_Teacher表中字段的值
  19. )*/
  20. @Table(name="_Teacher")//表示不是对应Teacher表,而是对应_Teacher
  21. public class Teacher {
  22.     
  23.     private int id;
  24.     private String name;
  25.     private String title;
  26.     private Sex sex;
  27.     @Enumerated(EnumType.STRING)
  28.     public Sex getSex() {
  29.         return sex;
  30.     }
  31.     public void setSex(Sex sex) {
  32.         this.sex = sex;
  33.     }
  34.     @Id//设置主键
  35.     @Column(name="_id")//id属性存入_id列中
  36.     @GeneratedValue//自动增值,mysql中相当于auto increment,根据数据库自动选择
  37.     //@GeneratedValue(strategy=GenerationType.IDENTITY)自动增值,指定类型;IDENTITY适用于sql,不适用于oracle.
  38.     //@GeneratedValue(strategy=GenerationType.TABLE,generator="TEACHER_GEN")//适用于跨数据平台,一般比较少用
  39.     public int getId() {
  40.         return id;
  41.     }
  42.     public void setId(int id) {
  43.         this.id = id;
  44.     }
  45.     @Column(name="_name")
  46.     public String getName() {
  47.         return name;
  48.     }
  49.     public void setName(String name) {
  50.         this.name = name;
  51.     }
  52.     public String getTitle() {
  53.         return title;
  54.     }
  55.     public void setTitle(String title) {
  56.         this.title = title;
  57.     }
  58.     
  59. }
    
    Teacher_Test.java
点击(此处)折叠或打开
  1. package com.hsh.hibernate.model;

  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5. import org.hibernate.service.ServiceRegistryBuilder;
  6. import org.junit.AfterClass;
  7. import org.junit.BeforeClass;
  8. import org.junit.Test;


  9. public class Teacher_Test {
  10.     private static SessionFactory sf=null;
  11.     @BeforeClass
  12.     public static void beforeClass(){
  13.         sf=new Configuration().configure().buildSessionFactory(new ServiceRegistryBuilder().applySettings(new Configuration().configure().getProperties()).buildServiceRegistry());
  14.     }
  15.     @Test
  16.     public void testTeacherSave(){
  17.         Teacher t =new Teacher();
  18.         //t.setId(1);
  19.         t.setName("t1");
  20.         t.setTitle("middle");
  21.         t.setSex(Sex.m);
  22.         //Configuration config = new Configuration().configure();
  23.         //SessionFactory sf = config.buildSessionFactory(new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry());
  24.         Session session=sf.openSession();
  25.         session.beginTransaction();
  26.         session.save(t);
  27.         session.getTransaction().commit();

  28.     }
  29.     
  30.     @AfterClass
  31.     public static void afterClass(){
  32.         sf.close();
  33.     }
  34. }


阅读(716) | 评论(0) | 转发(0) |
0

上一篇:C#调用matlab

下一篇:C#利用zedGraph可视化

给主人留下些什么吧!~~