博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate学习(一)创建数据表
阅读量:5241 次
发布时间:2019-06-14

本文共 6831 字,大约阅读时间需要 22 分钟。

(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 6
7
8
9
10
11 com.mysql.jdbc.Driver12
13
14 jdbc:mysql:///hibernate15
16
root
17
123456
18
19
20
21 org.hibernate.dialect.MySQLDialect22
23 24
25
26
true
27
28
true
29
30
update
31 32 33
34
35
36
37 38
39

(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 5
6
7
8
9
10
11 12
13
14
15
16 17
18

 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 Set
orders = 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 }
View Code

7.Customer.hbm.xml

1 
2 5
6
7
8
9
10
11 12
13
14
15
16
17
18 19
20
View Code

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 }
View Code

9.Order.hbm.xml

1 
2 5
6
7
8
9
10
11 12
13
14
15
16
17
18
19
View Code

 

转载于:https://www.cnblogs.com/lonecloud/p/5967658.html

你可能感兴趣的文章
文本相似度比较(网页版)
查看>>
Jenkins关闭、重启,Jenkins服务的启动、停止方法。
查看>>
2019.01.13 bzoj4538: [Hnoi2016]网络(树链剖分)
查看>>
codeforces 315 308
查看>>
BZOJ3998 [TJOI2015]弦论 【后缀自动机】
查看>>
CF E2 - Array and Segments (Hard version) (线段树)
查看>>
svn 架设
查看>>
k8s部署rocketmq 双主
查看>>
Linux SPI总线和设备驱动架构之四:SPI数据传输的队列化
查看>>
SIGPIPE并产生一个信号处理
查看>>
CentOS
查看>>
Linux pipe函数
查看>>
java equals 小记
查看>>
爬虫-通用代码框架
查看>>
2019春 软件工程实践 助教总结
查看>>
YUV 格式的视频呈现
查看>>
Android弹出框的学习
查看>>
现代程序设计 作业1
查看>>
事件和信号量
查看>>
在android开发中添加外挂字体
查看>>