Hibernate4学习一
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate4学习一相关的知识,希望对你有一定的参考价值。
1.导入所有相关jar包
https://pan.baidu.com/s/1i5OakiL
2.配置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> <!-- 数据库连接设置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- Student为你的数据库名称 --> <property name="connection.url">jdbc:mysql://localhost:3306/QQ_DB</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate‘s automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property> <!-- 显示SQL语句 --> <property name="show_sql">true</property> <!-- 格式化输出SQL语句 --> <property name="format_sql">true</property> <!-- validate 加载hibernate时,验证创建数据库表结构 --> <!-- create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 --> <!-- create-drop 加载hibernate时创建,退出是删除表结构 --> <!-- update 加载hibernate自动更新数据库结构 --> <property name="hbm2ddl.auto">create</property> <property name="myeclipse.connection.profile"></property> <!-- 使用annotion注解时,hibernate与数据库对象关系映射表示 --> <!-- 推荐使用annotion注解来表示映射关系 --> <mapping class="QQUser" /> </session-factory> </hibernate-configuration>
3.新建映射类
import java.io.Serializable; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="tb_user") public class QQUser implements Serializable { private String qq_num; private String nick_name; private String gender; private Date birthday; private int star_sit; @Id public String getQq_num() { return qq_num; } public void setQq_num(String qq_num) { this.qq_num = qq_num; } public String getNick_name() { return nick_name; } public void setNick_name(String nick_name) { this.nick_name = nick_name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getStar_sit() { return star_sit; } public void setStar_sit(int star_sit) { this.star_sit = star_sit; } }
4.测试
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub QQUser u = new QQUser(); u.setQq_num("7890654"); u.setNick_name("小明"); u.setGender("男"); Configuration cfg = new Configuration(); // 读取hibernate.cfg.xml中的配置 cfg.configure(); // 获取SessionFactory SessionFactory sf = cfg.buildSessionFactory(); // 获取Session Session session = sf.openSession(); // 开启事务 session.beginTransaction(); // 保存 session.save(u); // 提交事务 session.getTransaction().commit(); // 关闭连接 session.close(); sf.close(); } }
以上是关于Hibernate4学习一的主要内容,如果未能解决你的问题,请参考以下文章