Hibernate4入门学习过程记录一
来源:互联网 发布:游戏程序员 职业规划 编辑:程序博客网 时间:2024/06/05 23:53
使用IDEA,src下的资源文件无法自动编译输出到out目录下,网上给出4种解决方案,请参考http://blog.csdn.net/shifangwannian/article/details/48882201
本人采用建立resources文件夹并Mark Directory as --->Resources Root的方案,是实际项目中这种方法也是最常用的。
简单的过程不做记录,记下关键性配置文件,以及方法。
hibernate.cfg.xml文件
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <!-- DB schema will be updated if needed --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/domain/User.hbm.xml"></mapping> </session-factory></hibernate-configuration>
User.java
package com.domain;public class User { private String id; private String name; private Integer age; private String address; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; if (name != null ? !name.equals(user.name) : user.name != null) return false; if (age != null ? !age.equals(user.age) : user.age != null) return false; if (address != null ? !address.equals(user.address) : user.address != null) return false; return true; } public int hashCode() { int result = id != null ? id.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (age != null ? age.hashCode() : 0); result = 31 * result + (address != null ? address.hashCode() : 0); return result; }}
<?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="com.domain.User"> <id name="id"> <generator class="uuid"/> </id> <property name="name"/> <property name="age"/> <property name="address"/> </class></hibernate-mapping>
test01.java
package com.test;import com.domain.User;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.hibernate.tool.hbm2ddl.SchemaExport;public class test01 { /** * 根据hibernate配置文件,生成数据库 */ public static void exportEntityToDatabase(){ Configuration configuration = new Configuration().configure(); SchemaExport schemaExport = new SchemaExport(configuration); schemaExport.create(true, true); } /** * hibernate4 保存对象 */ public static void saveUserTest(){ Configuration configuration = new Configuration().configure(); Session session = null; ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); try { session = sessionFactory.openSession(); session.beginTransaction(); User user = new User(); user.setName("小新"); user.setAge(22); user.setAddress("浙"); session.save(user); session.getTransaction().commit(); }catch (Exception e){ e.printStackTrace(); session.getTransaction().rollback(); }finally { if (session != null && session.isOpen()){ session.close(); } } } public static void main(String[] args) {// exportEntityToDatabase(); saveUserTest(); }}
阅读全文
0 0
- Hibernate4入门学习过程记录一
- ExtJs学习过程记录(一)
- FPGA学习过程记录一
- Hibernate4教程一:入门介绍
- [学习记录]Unity界面入门---(一)
- 记录c语言入门学习一
- Mybatis学习记录(一)--Mybatis入门
- Mybatis学习记录(一)--Mybatis入门
- osg入门学习知识点记录一
- hibernate4-过程
- hibernate4 入门
- Spring4 与 Hibernate4 整合过程中的问题记录
- 【WebService学习过程记录(一)】照猫画虎---运行示例
- 【WebService学习过程记录(一)】照猫画虎---运行示例
- 【WebService学习过程记录(一)】照猫画虎---运行示例
- VCGlib入门记录一
- hibernate4.x(一)——hibernate入门之helloworld
- Hibernate4自学入门(一)——初识Hibernate
- Java学习——分支结构
- 数独算法的解析(山羊原创)
- MySQL中SELECT+UPDATE处理并发更新问题解决方案分享
- DataTotalMapReduce (流量统计)
- [YZOJ]P2417-[FJWC2016]翻转硬币-最短路建模
- Hibernate4入门学习过程记录一
- Qt学习之2D绘图(画刷和画笔)
- WebPvMapReduce
- Tornado概况
- pip download timeout 下载慢,超时解决方法
- 【error】django.db.utils.OperationalError: (1193, "Unknown system variable 'storage_engine'")
- 【JavaSE_学习笔记】List接口
- 第四周项目五C/C++循环双链表应用
- python爬虫用户代理