Hibernate之HelloWorld
2015-07-07 19:43
387 查看
1、新建一个Java Project项目HibernateDay01_HelloWorld
2、导包:
你可以去Hibernate官网www.hibernate.org去下载最新的(例:hibernate-release-4.3.10.Final)包。解压后:
里面的/lib/required下的是必须的jar包,需在新建项目中导入。
/project/etc/hibernate.cfg.xml为配置文件,需把它拷贝到新建项目src文件下。可以看到dtd配置,也可以在导入的jar包(hibernate-core-4.3.10.Final.jar中的/org/hibernate/hibernate-mapping-3.0.dtd)中查找。
3、把hibernate.cfg.xml拷贝到src目录下
4、新建数据库
(此处用的是MySQL数据库)新建数据库:hibernate_basicmapping,数据库中新建表t_user。配置字段:id,int,自增,主键;name,varchar;password,varchar。5、新建一个实体类User.java(net.qbzhong.po包下)
package net.qbzhong.po; public class User { private int id; private String name; private String password; public int getId() { return id; } public void setId(int 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; } }
在User同目录下,新建一个(类名.hbm.xml)User.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="net.qbzhong.po.User" table="t_user"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name"></property> <property name="password" column="password"></property> </class> </hibernate-mapping>
6、新建一个测试类TestClient.java(net.qbzhong.test包下)
package net.qbzhong.test; import net.qbzhong.po.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class TestClient { public static void main(String[] args) { //1.读取配置文件(hibernate.properties) Configuration cfg = new Configuration(); //读取hibernate.cfg.xml配置文件 cfg = cfg.configure(); //2.建立与数据库会话(session)的factory SessionFactory factory = cfg.buildSessionFactory(); //3.与数据库之间的会话 Session session = factory.openSession(); //数据库事务(开启事务) Transaction tx = session.beginTransaction(); //4.生成User的实例 User user = new User(); user.setName("west"); user.setPassword("12345"); //5.保存到数据库 session.save(user); //提交事务 tx.commit(); session.close(); } }
7、最后,别忘了最重要的配置文件hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_basicmapping</property> <!-- 数据库账号 --> <property name="hibernate.connection.username">root</property> <!-- 数据库密码 --> <property name="hibernate.connection.password">root</property> <!-- 在Console中输出sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 配置数据的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 映射指定的实体类文件,将映射文件加入到配置文件中 --> <mapping resource="net/qbzhong/po/User.hbm.xml"/> </session-factory> </hibernate-configuration>
最后,在TestClient.java类中,运行main函数,你会发现,数据库中已经新建了一行数据。而且
<property name="hibernate.show_sql">true</property> //这行代码的作用就是在运行main函数后,在Console视图框中可以输出执行的SQL语句。
相关文章推荐
- Leetcode Kth Smallest Element in a BST
- Android编译输出 资源指定AAPT
- android app 测试case积累
- Java设计模式---单例模式
- 写给自己的学习感言
- 移动web:图片切换(焦点图)
- 基于nodejs 的微信 JS-SDK 简单应用
- lc面试准备:Implement Stack using Queues
- Linux的inode的理解
- 页面链接跳转小知识
- 设计模式迭代器模式
- java 中Date的格式化样式
- 浅谈C#中堆和栈的区别(附上图解)
- Oracle Database 11.2.0.4.0 已在 中标麒麟Linux x86-64 NeoKylin Linux Advanced Server 6 上通过认证
- ios开发常用工具集合网站
- 【扩展欧几里得】Codevs 1200: [noip2012]同余方程
- scala中span和partition区别
- NYOJ 822 画图
- C/C++中的日期和时间 time_t与struct tm变换
- DBMonster入门