hibernate入门简单案例
来源:互联网 发布:淘宝联盟返利是真的吗? 编辑:程序博客网 时间:2024/04/30 16:49
使用eclipse ee安装hibernate-tools插件,方便配置hibernate
首先导入hibernate和mysql所需的jar包
现在数据库中创建好对应的database、
在src里创建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> <property name="connection.username">steve</property> <property name="connection.password">845109060</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="hibernate/hibernate.hbm.xml"/> </session-factory></hibernate-configuration>
然后创建实体类Student(按照标准javabean方式创建)
package hibernate;public class Student {private int id;private String name;private String sex;private String address;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 getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public Student() {}public Student(int id, String name, String sex, String address) {super();this.id = id;this.name = name;this.sex = sex;this.address = address;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", sex=" + sex+ ", address=" + address + "]";}}
配置实体类的映射文件Student.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"><!-- Generated 2015-12-29 20:14:53 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping> <class name="hibernate.Student" table="STUDENT"> <id name="id" type="int"> <column name="ID" /> <generator class="assigned" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="sex" type="java.lang.String"> <column name="SEX" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" /> </property> </class></hibernate-mapping>
注意配置完后再hibernat.cfg.xml中注册一下
写测试类
package hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;import antlr.debug.TraceAdapter;public class Testabc {private SessionFactory sessionfactory;private Session session;private Transaction transcation;@Beforepublic void before(){//创建配置对象Configuration config=new Configuration().configure();//创建服务注册对象ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();//创建会话工厂对象sessionfactory=config.buildSessionFactory(serviceRegistry);//创建会话对象session=sessionfactory.openSession();//创建事物transcation=session.beginTransaction();}@Testpublic void test(){Student s=new Student(1,"steve","man","america");//保存对象进数据库session.save(s);}@Afterpublic void after(){//提交会话transcation.commit();session.close();sessionfactory.close();}}
运行后数据库中就创建好了Student表,并把Student的对象存入数据库的记录中。
0 0
- hibernate入门简单案例
- hibernate的入门的一个简单案例
- Hibernate之入门案例
- hibernate 入门案例
- Hibernate框架入门案例
- hibernate快速入门案例
- hibernate入门案例
- hibernate入门案例
- Hibernate入门案例
- Hibernate配置入门案例
- Hibernate-入门案例
- Hibernate入门配置案例
- Hibernate入门案例
- Hibernate入门案例
- JavaMail简单入门案例
- AngularJS简单入门案例
- struts2入门简单案例
- seajs简单入门案例
- mac重启需要多次输入钥匙串密码的问题
- 为RecyclerView添加头部和脚部的UI控件:Bookends
- NSRunLoop
- codeforces D. The Union of k-Segments 排序
- 查看Linux系统的发行版本,32位还是64位
- hibernate入门简单案例
- 03 给定链表头结点, 依次输出从尾节点到头结点的数据
- hdu 1358 Period KMP算法
- OkHttpClient
- LeetCode之Tree题目汇总
- 《Monkey Android》第8课FrameLayout、GridLayout
- ZBar大图像处理加速
- 《Monkey Android》第9课Toast土司
- 软件设置安装问题之vs