走近SSH之Hibernate--Hibernate创建数据表并向表中写入数据(Hibernate+MySql)
来源:互联网 发布:魅族note6软件分身 编辑:程序博客网 时间:2024/06/05 07:40
上篇博客《 走近SSH之Hibernate--Hibernate环境搭建(MyEclipse+MySql)》我们已为Hibernate开发做好了准备,接下来我们简单的用Hibernate+MySql做个小例子,来看下Hibernate的工作流程是怎样的。
实例说明:
Hibernate和MySql相结合,Hibernate利用SchemaExport创建数据表,并利用类文件向创建的数据表中添加数据。
注:对象与表对应;属性与列对应!
实例过程:
先看一下hibernate项目目录情况
1.创建用户类:User.java
在src下创建包com.tgbnode.hibernate,在包中创建一个User类,主要是用户的属性:id,用户名,密码,创建时间,失效时间。
package com.tgbnode.hibernate;import java.util.Date;public class User {//idprivate String id;//用户名private String name;//密码private String password;//创建时间private Date createTime;//失效时间private Date expireTime;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getExpireTime() {return expireTime;}public void setExpireTime(Date expireTime) {this.expireTime = expireTime;}}
2.创建User.hbm.xml文件
利用此xml文件,使User.java生成相应的数据库表及字段。
<?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><!--默认把类的名称映射为相同名字的表名,可使用table属性修改表名--> <class name="com.tgbnode.hibernate.User"><!-- 主键采用uuid生成 --><id name="id"><generator class="uuid"/></id><!--默认把类的变量映射为相同名字的表字段,可使用column属性修改表字段--> <property name="name"/><property name="password"/><property name="createTime"/><property name="expireTime"/></class></hibernate-mapping>
3.在src下创建hibernate.cfg.xml文件
此配置文件说明了采用的数据库及映射文件。
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- 数据库驱动 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- 指定使用数据库为hibernate_first,注意这里的端口号3306要与安装MySql时设置的端口号相同 --><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property><!-- 连接数据库的用户名 --><property name="hibernate.connection.username">root</property><!-- 连接MySql使用的密码 --><property name="hibernate.connection.password">123456</property><!-- 选择使用的方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 设置是否显示生成sql语句 --> <property name="hibernate.show_sql">true</property><!--映射文件--><mapping resource="com/tgbnode/hibernate/User.hbm.xml"/></session-factory></hibernate-configuration>
4.建立ExportDB类
通过运行此类创建相应的数据表。
package com.tgbnode.hibernate;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * 将hbm生成ddl * @author azj * */public class ExportDB {public static void main(String[] args) {//默认读取hibernate.cfg.xml文件Configuration cfg = new Configuration().configure(); //SchemaExport工具类接收配置对象(Configuration)导成DDL,把对象类导成表SchemaExport export = new SchemaExport(cfg);//打印到控制台,并输入到数据库export.create(true, true);}}
5.添加log4j.propertes文件
提示给开发者相应的错误信息,使开发变得简单!
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file hibernate.log ####log4j.appender.file=org.apache.log4j.FileAppender#log4j.appender.file.File=hibernate.log#log4j.appender.file.layout=org.apache.log4j.PatternLayout#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=warn, stdout
6.创建类Client类
通过运行此类,向创建好的用户表中添加数据信息。
package com.tgbnode.hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class Client { public static void main(String[] args) {//读取hibernate.cfg.xml文件Configuration cfg = new Configuration().configure();//建立SessionFactorySessionFactory factory = cfg.buildSessionFactory();//取得sessionSession session = null;try {session = factory.openSession();//开启事务session.beginTransaction();User user = new User();user.setName("张三");user.setPassword("123");user.setCreateTime(new Date());user.setExpireTime(new Date());//保存User对象session.save(user);//提交事务session.getTransaction().commit();}catch(Exception e) {e.printStackTrace();//回滚事务session.getTransaction().rollback();}finally {if (session != null) {if (session.isOpen()) {//关闭sessionsession.close();}}}}}
下面我们来测试一下,运行MySql的dos文件:
至此Hibernate第一个实例就做完了,不知道您对Hibernate是否有了一定的了解了呢,知识就是在反复的折腾中弄懂的,至今我对Hibernate也只有皮毛的理解,还需要通过项目来达到熟练,继而精通!不管如何只要相信就会成功!
- 走近SSH之Hibernate--Hibernate创建数据表并向表中写入数据(Hibernate+MySql)
- 走近SSH之Hibernate--Hibernate环境搭建(MyEclipse+MySql)
- 走近SSH之Hibernate--HQL
- 走近SSH之Hibernate--Hibernate入门相关简介
- hibernate向MySQL里面添加数据表和数据
- Hibernate框架下向mysql中写入中文乱码解决方案
- 走近SSH之Hibernate--com.mysql.jdbc.CommunicationsException: Communications link failure due to underlyi
- 深入浅出Hibernate之向Mysql插入BLOB,CLOB数据
- 使用Hibernate向Mysql中插入中文数据出现乱码
- hibernate 写入不了数据
- java hibernate SchemaExport 创建数据表
- hibernate向mysql插入数据乱码问题
- hibernate向Oracle写入数据step by step
- 使用Hibernate向数据库中添加数据
- [hibernate]创建hibernate项目并通过xml文件配置生成数据表
- SSH中hibernate配置mysql乱码问题
- SSH框架-HIbernate之数据持久化
- 利用hibernate数据映射创建数据表(创建自己的jar包库)
- VS2010打不开VS2012 .NET MVC 工程,及打开后部分模块加载不正确的解决办法
- 6 算法
- 商丘哪有卖钟表的 田园挂饰的那种
- easyui validatebox 验证集合
- Daemon的实现
- 走近SSH之Hibernate--Hibernate创建数据表并向表中写入数据(Hibernate+MySql)
- C++作业03_01: 将写的n封信和n个信封全部装错。设Dn为n封信装错信封可能的种类
- 原码反码补码
- AspectJ的Execution表达式
- java.sql.Date与java.util.Date的区别
- 要学习的知识—做个笔记方便查找
- phpcms搬站需要修改的地方,避免拉下
- 算法题:两数组所有和值的最小前 k 项
- WindowsPhone中的ScrollableTextBlock