(1)生成数据库表的创建:
1 // 默认读取hibernate.cfg.xml文件 2 Configuration cfg = new Configuration().configure(); 3 4 // 生成并输出sql到文件(当前目录)和数据库 5 SchemaExport export = new SchemaExport(cfg); 6 7 // 创建表结构,第一个true 表示在控制台打印sql语句,第二个true 表示导入sql语句到数据库8 export.create(true, true);
(2)Hibernate.cfg.xml文件
1 2 5 67 8 9 10 3911 com.mysql.jdbc.Driver12 1314 jdbc:mysql:///hibernate15 16root 17123456 18 19 2021 org.hibernate.dialect.MySQLDialect22 23 24 25 26true 27 28true 29 30update 31 32 33 3435 36 37 38
(3)log4j.properties文件
1 ### direct log messages to stdout ### 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.Target=System.err 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 6 7 ### direct messages to file mylog.log ### 8 log4j.appender.file=org.apache.log4j.FileAppender 9 log4j.appender.file.File=c\:mylog.log10 log4j.appender.file.layout=org.apache.log4j.PatternLayout11 log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n12 13 ### set log levels - for more verbose logging change 'info' to 'debug' ###14 15 log4j.rootLogger=off, stdout
(4)User.java
1 package cn.lonecloud.domain; 2 3 import java.io.Serializable; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.Method; 6 import java.util.Date; 7 8 public class User implements Serializable,Cloneable { 9 10 private String id;11 private String name;12 private String password;13 private Date birthday;14 public User(){15 System.out.println("user");16 }17 public String getId() {18 return id;19 }20 public void setId(String id) {21 this.id = id;22 }23 public String getName() {24 return name;25 }26 public void setName(String name) {27 this.name = name;28 }29 public String getPassword() {30 return password;31 32 }33 public void setPassword(String password) {34 this.password = password;35 }36 public Date getBirthday() {37 return birthday;38 }39 public void setBirthday(Date birthday) {40 this.birthday = birthday;41 }42 @Override43 public Object clone() throws CloneNotSupportedException {44 Class clazz = User.class;45 User user =null;46 try {47 user = (User)clazz.newInstance();48 /*user.setBirthday(this.birthday);49 user.setId(this.id);50 user.setPassword(this.password);51 user.setName(this.name);*/52 Field[] declaredFields = clazz.getDeclaredFields();53 for(Field field :declaredFields){54 Class parameterTypes = field.getType();55 String fieldName = field.getName();56 String methodName = "set"+fieldName.substring(0,1).toUpperCase()+field.getName().substring(1);57 Method method = clazz.getDeclaredMethod(methodName, parameterTypes);58 field.setAccessible(true);59 method.invoke(user,field.get(this));60 }61 return user;62 } catch (Exception e) {63 e.printStackTrace();64 } 65 return null;66 }67 68 69 }
(5).user.hbm.xml
1 2 56 7 8 189 11 12 1310 14 15 16 17
6.Customer.java
1 package cn.lonecloud.domain; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 public class Customer implements Serializable { 8 9 private String id;10 11 private String customerName;12 13 private Setorders = new HashSet ();14 15 public String getId() {16 return id;17 }18 19 public void setId(String id) {20 this.id = id;21 }22 23 public String getCustomerName() {24 return customerName;25 }26 27 public void setCustomerName(String customerName) {28 this.customerName = customerName;29 }30 31 public Set getOrders() {32 return orders;33 }34 35 public void setOrders(Set orders) {36 this.orders = orders;37 }38 39 40 }
7.Customer.hbm.xml
1 2 56 7 8 209 11 12 1310 14 15 18 1916 17
8.Order.java
1 package cn.lonecloud.domain; 2 3 import java.io.Serializable; 4 5 public class Order implements Serializable{ 6 7 private String id; 8 9 private String orderNumber;10 11 private float price;12 13 private Customer customer;14 15 16 public Customer getCustomer() {17 return customer;18 }19 20 public void setCustomer(Customer customer) {21 this.customer = customer;22 }23 24 public String getId() {25 return id;26 }27 28 public void setId(String id) {29 this.id = id;30 }31 32 public String getOrderNumber() {33 return orderNumber;34 }35 36 public void setOrderNumber(String orderNumber) {37 this.orderNumber = orderNumber;38 }39 40 public float getPrice() {41 return price;42 }43 44 public void setPrice(float price) {45 this.price = price;46 }47 48 49 50 51 52 }
9.Order.hbm.xml
1 2 56 7 8 199 11 12 1310 14 15 16 1817