博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Hibernate] - Annotations
阅读量:4542 次
发布时间:2019-06-08

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

Hibernate使用Annotations最简单例子:

 

hibernate.cfg.xml

com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1/testdb
root
1
org.hibernate.dialect.MySQL5Dialect
thread
org.hibernate.cache.internal.NoCacheProvider
true
update

 

HibernateUtil.java

package com.my.dao.util;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;public class HibernateUtil {    private static final SessionFactory sessionFactory = buildSessionFactory();    private static SessionFactory buildSessionFactory() {        try {            // Create the SessionFactory from hibernate.cfg.xml            Configuration configuration = new Configuration();            return configuration.configure().buildSessionFactory(                    new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());        } catch (Throwable ex) {            // Make sure you log the exception, as it might be swallowed            System.err.println("Initial SessionFactory creation failed." + ex);            throw new ExceptionInInitializerError(ex);        }    }    public static SessionFactory getSessionFactory() {        return sessionFactory;    }    }

 

POJO

package com.my.bean;import java.util.Date;import javax.persistence.*;@Entity@Table(name="user")public class User {    @Id @GeneratedValue @Column(name="user_id", nullable=false)    private long userID;        @Column(name="user_name", length=100, nullable=false)    private String userName;        @Column(name="create_time", nullable=false)    private Date createTime;    public long getUserID() {        return userID;    }    public void setUserID(long userID) {        this.userID = userID;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public Date getCreateTime() {        return createTime;    }    public void setCreateTime(Date createTime) {        this.createTime = createTime;    }}

 

Test:

package com.my.init;import java.util.Date;import org.hibernate.Session;import org.hibernate.Transaction;import com.my.bean.User;import com.my.dao.util.HibernateUtil;public class Test {    public static void main(String[] args) {        Session session = HibernateUtil.getSessionFactory().openSession();        Transaction tx = session.beginTransaction();                try {            User user = new User();            user.setUserName("Robin");            user.setCreateTime(new Date());                        session.save(user);                        tx.commit();        } catch (Exception e) {            tx.rollback();            e.printStackTrace();        }                session.close();    }}

参考文献:

 

转载于:https://www.cnblogs.com/HD/p/3935360.html

你可能感兴趣的文章
mybatis + log4j 打印mybatis的sql
查看>>
解决Visual Studio:"无法导入以下密钥文件: xxxx.pfx,该密钥文件可能受密码保护"
查看>>
[NOI2013]矩阵游戏
查看>>
转:不规则按钮实现
查看>>
poj 3744 Scout YYF I (矩阵快速幂)
查看>>
bzoj 2075: [POI2004]KAG
查看>>
python 3 day1(下)
查看>>
调试CS5343总结报告
查看>>
hdu 2586 How far away ? Lca的模板了、、
查看>>
JavaScript筑基篇(二)->JavaScript数据类型
查看>>
Google常用拓展插件
查看>>
一个麻省理工学院毕业生对中国教育的反思
查看>>
Drupal7重置密码方法
查看>>
WEB安全问题
查看>>
C++重载运算符练习--对people类重载“= =”运算符和“=”运算符
查看>>
Nmap命令的实用范例
查看>>
7-1 查找整数编程总结
查看>>
安装PHP以及搭建博客(一)
查看>>
关于WORD文档的读取乱码问题
查看>>
[问题记录.dotnet]取网卡信息报错"找不到"-WMI - Not found
查看>>