标签:src oid 打印 public dial 数据 存在 结构 .config
1 package gdpi.annotation; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 import javax.persistence.CascadeType; 8 import javax.persistence.Column; 9 import javax.persistence.Entity; 10 import javax.persistence.GeneratedValue; 11 import javax.persistence.GenerationType; 12 import javax.persistence.Id; 13 import javax.persistence.OneToMany; 14 import javax.persistence.Table; 15 import javax.persistence.Transient; 20 21 @Entity // @Entity注解 声明这个类为一个实体类 22 @Table(name="t_grade") // @Table 指明这个实体类对应数据库t_grade 表 23 public class Grade implements Serializable{ 24 25 @Transient // 指明属性在数据库中不存在 26 private static final long serialVersionUID = 1L; 27 @Id // 指定属性为唯一标识 28 @GeneratedValue(strategy = GenerationType.IDENTITY) // 主键生成策略 29 private Integer gid; 30 31 @Column(name="gname") // 将实体类的属性和数据库表的字段作一个映射 32 private String gname; 33 34 @Column(name="gdesc") 35 private String gdesc; 36 37 public Grade() { 38 super(); 39 // TODO Auto-generated constructor stub 40 } 41 42 public Grade(Integer gid, String gname, String gdesc) { 43 super(); 44 this.gid = gid; 45 this.gname = gname; 46 this.gdesc = gdesc; 47 } 48 49 50 public Integer getGid() { 51 return gid; 52 } 53 54 public void setGid(Integer gid) { 55 this.gid = gid; 56 } 57 58 public String getGname() { 59 return gname; 60 } 61 62 public void setGname(String gname) { 63 this.gname = gname; 64 } 65 66 public String getGdesc() { 67 return gdesc; 68 } 69 70 public void setGdesc(String gdesc) { 71 this.gdesc = gdesc; 72 } 73 74 @Override 75 public String toString() { 76 return "Grade [gid=" + gid + ", gname=" + gname + ", gdesc=" + gdesc + "]"; 77 } 78 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!--1、配置数据库连接信息 --> 8 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="hibernate.connection.url">jdbc:mysql:///hibernate_study</property> <--hibernate_study 为我的本地数据库--> 10 <property name="hibernate.connection.username">root</property> <-- root 为我的本地数据库用户名--> 11 <property name="hibernate.connection.password">root</property> <-- root 为我的本地数据库密码--> 12 13 <!--2、配置hibernate的信息 --> 14 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 15 <property name="hibernate.format_sql">true</property> 16 <property name="hibernate.show_sql">true</property> 17 <property name="hibernate.hbm2ddl.auto">update</property> 18 19 <-- 引入实体 class:为实体类的类路径--> 20 <mapping class="gdpi.annotation.Grade"/> 21 22 23 </session-factory> 24 </hibernate-configuration>
1 package gdpi.annotation; 2 3 import java.util.List; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.cfg.Configuration; 8 import org.junit.Test; 9 10 import gdpi.annotation.Grade; 11 12 /** 13 * 测试类 14 * */ 15 @SuppressWarnings("deprecation") 16 public class Test { 17 private static Configuration cfg = null; 18 private static SessionFactory sf = null; 19 private static Session session = null; 20 21 static{ 22 cfg = new Configuration(); 23 cfg.configure(); 24 sf = cfg.buildSessionFactory(); 25 session = sf.openSession(); 26 } 27 28 public static Session getSession() { 29 return session; 30 } 31 32 @Test 33 public void test(){ 34 // 获取Session 对象,类似于JDBC中的Connection对象 35 Session session = getSession(); 36 List<Grade> list = session.createQuery("from Grade").list(); // 使用hql查询语句,查询该表中的所有数据 37 if(list != null){ 38 for(Grade g:list){ 39 System.out.println(g); 40 } 41 } 42 } 43 }
控制台打印:
本地MySQL数据库
标签:src oid 打印 public dial 数据 存在 结构 .config
原文地址:https://www.cnblogs.com/cairiqiang/p/9349801.html