先给eclipse安装hibernate插件
打开eclipse工具,选择HELP---Install new software----输入网址
http://download.jboss.org/jbosstools/updates/development
安装Hibernate Tools
初探Hibernate
Students.java
需要
1.参数私有化
2.一个无参构造函数
3.需要所有参数的setter/getter方法
import java.util.Date;
public class Students {
private int sid;
private String sname;
private String gender;
private Date birthday;
private String address;
public Students() {
super();
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
+ ", address=" + address + "]";
}
}
hibernate.cfg.xml
原来数据库中没有Students表。不过没关系。
hbm2ddl.auto是create。
意思是每次插入数据前,都要先dop,再create
<?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">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</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="Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Students.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 2017-4-28 16:17:27 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
测试类
import java.util.Date;
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;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(config.getProperties())
.buildServiceRegistry();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
}
@After
public void destory(){
//提交事务
transaction.commit();
//关闭会话
session.close();
//关闭sessionFactory
sessionFactory.close();
}
@Test
public void testSaveStudents() {
Students s = new Students();
s.setSid(3);
s.setSname("张三丰");
s.setGender("男");
s.setBirthday(new Date());
s.setAddress("武当山");
//保存对象到数据库
session.save(s);
}
}
运行结果
Hibernate:
drop table if exists STUDENTS
Hibernate:
create table STUDENTS (
SID integer not null,
SNAME varchar(255),
GENDER varchar(255),
BIRTHDAY datetime,
ADDRESS varchar(255),
primary key (SID)
)
Hibernate:
insert
into
STUDENTS
(SNAME, GENDER, BIRTHDAY, ADDRESS, SID)
values
(?, ?, ?, ?, ?)
同时查看数据库,会发现一条记录
2-5 Session详解(上)
注意如果是openSession,需要每次都要手动关闭Session,
如果是getCurrentSession,会自动关闭Session
2-7 hbm配置文档
通常使用的是native。和assigned
3-2.单一主键
3-3.基本类型
xxx.hbm.xml
如果想要“年月日,时分秒”,type="java.util.date"或者type="timestamp"
如果想要“年月日”,type="date"
如果想要“时分秒”,type="time"
注意timestamp,date,time都是hibernate类型
3-4.对象类型
3-5.组件属性
3-6.单表操作CRUD实例
import java.util.Date;
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;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(config.getProperties())
.buildServiceRegistry();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
session = sessionFactory.openSession();
//开启事务
transaction = session.beginTransaction();
}
@After
public void destory(){
//提交事务
transaction.commit();
//关闭会话
session.close();
//关闭sessionFactory
sessionFactory.close();
}
@Test
public void testSaveStudents() {
Students s = new Students();
s.setSname("张三丰");
s.setGender("男");
s.setBirthday(new Date());
s.setAddress("武当山");
//保存对象到数据库
session.save(s);
}
@Test
public void testGetStudents(){
Students s = (Students)session.get(Students.class, 0);
//System.out.println(s);
}
@Test
public void testLoadStudents(){
Students s = (Students)session.load(Students.class, 0);
//System.out.println(s);
}
@Test
public void testUpdateStudents(){
Students s = (Students)session.get(Students.class, 0);
s.setGender("女");
session.update(s);
System.out.println(s);
}
@Test
public void testDeleteStudents(){
Students s = (Students)session.get(Students.class, 0);
session.delete(s);
}
}