第一个Hibernate程序
来源:互联网 发布:淘宝店铺怎么快速升钻 编辑:程序博客网 时间:2024/05/17 03:52
一. 准备工作
1.1jar包准备
Hibernate必须的jar包
hibernate-release-5.0.7.Final中的lib/required
数据库驱动jar包:(MYSQL)
日志jar包
2.2配置文件
Hibernate配置文件连接上数据库相关配置(hibernate.cfg.xml)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--显示执行的SQL语句-->
<property name="show_sql">true</property>
<!--连接字符串-->
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<!--连接数据库的用户名-->
<property name="connection.username">root</property>
<!--数据库用户密码-->
<property name="connection.password">root</property>
<!--数据库驱动-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--JDBC连接池(使用内置的连接池)-->
<property name="connection.pool_size">1</property>
<!--设置Hibernate自动管理上下文的策略-->
<property name="current_session_context_class">thread</property>
<!--选择使用的方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--在启动时删除并重新创建数据库-->
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/hb/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射配置文件描述与数据库中表的映射关系(XXX.hbm.xml)
<?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>
<!--name实体类的全名 table表名catelog数据库的名称-->
<class name="com.hb.entity.User" table="n_user">
<id name="id">
<generator class="uuid"></generator>
</id>
<property name="name"></property>
<property name="psw"></property>
<property name="brithday"></property>
</class>
</hibernate-mapping>
log4j.xml文件(略)
二. 保存数据
2.1实体类
package com.hb.entity;
import java.util.Date;
import javax.persistence.Entity;
@Entity
public class User {
private String id;
private String name;
private String psw;
private Date brithday;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String id, String name, String psw, Date brithday) {
super();
this.id = id;
this.name = name;
this.psw = psw;
this.brithday = brithday;
}
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 getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
}
2.2创建表
package com.hb.entity;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().configure().build();
//读取配置文件
MetadataImplementor metadata=(MetadataImplementor) new MetadataSources(serviceRegistry).buildMetadata();
new SchemaExport(metadata).create(true, true);
}
}
2.3插入数据
package com.hb.entity;
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文件
Configurationconfiguration=new Configuration().configure();
//建立SessionFactory
SessionFactorysessionFactory=configuration.buildSessionFactory();
//取得Session
Sessionsession=null;
try{
session=sessionFactory.openSession();
//开始事物
session.beginTransaction();
Useruser=new User();
user.setName("张三");
user.setPsw("123");
user.setBrithday(new Date());
//保存User对象
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exceptione){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
}
}
- 第一个Hibernate程序
- 第一个hibernate程序
- hibernate第一个程序
- 第一个hibernate程序
- 第一个hibernate程序
- 第一个Hibernate程序
- 第一个Hibernate程序
- 第一个Hibernate程序
- Hibernate第一个程序
- hibernate 第一个程序
- 第一个hibernate程序
- 第一个Hibernate程序
- Hibernate第一个程序
- 第一个hibernate程序
- Hibernate第一个程序
- Hibernate第一个程序
- 第一个hibernate程序
- 第一个hibernate程序
- 人工智能硅脑竞赛:传统芯片巨头VS AI芯片创企
- Hadoop之高可用原理
- 语音合成技术
- 小米手机测试安装失败Installation failed with message:INSTALL_CANCELED_BY_USER
- 分类模型与算法--距离判别法
- 第一个Hibernate程序
- Atom beautify不可用,提示uncrustify未安装错误
- lua中表table的拷贝而不是引用的操作方法
- JavaScript中location.hash详解
- Python-3.6.2
- C++:指针函数与函数指针
- Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 4. 函数
- JSON数组对象和对象数组
- [整理]WebUploader + SpringMVC 实现多文件断点续传之二 多文件断点续传