1>搭建好环境
引入hibernate jar包
2>准备Hibernate.cfg.xml启动配置文件 并且配置数据库连接
root
123
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
com.mysql.jdbc.Driver
org.hibernate.dialect.MySQLDialect
thread
true
true
3>创建库表
create table t_hibernate_user
(
id int not null primary key, -- ID:主键,注:没有设置为自动增长
user_name varchar(50) not null, -- 用户名
user_pwd varchar(10) not null, -- 密码
real_name varchar(50), -- 真实姓名
sex char(2), -- 性别
birthday datetime, -- 出生日期
create_datetime timestamp NULL default CURRENT_TIMESTAMP, -- 创建日期时间,默认为系统当前时间
remark varchar(1024) -- 备注
);
insert into t_hibernate_user(id,user_name,user_pwd,real_name,sex,birthday,create_datetime,remark)
values(1,'zs','123','张三','男',20001010,null,'abcd');
insert into t_hibernate_user(id,user_name,user_pwd,real_name,sex,birthday,remark)
values(2,'ls','123','李四','男',20001110,'xyz');
4>写实体类(pojo)
package com.zking.one.entity;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -3363120264501521428L;
private Integer id;
private String userName;
private String userPwd;
private String realName;
private String sex;
private java.sql.Date birthday;
private java.sql.Timestamp createDatetime;
private String remark;
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public java.sql.Date getBirthday() {
return birthday;
}
public void setBirthday(java.sql.Date birthday) {
this.birthday = birthday;
}
public java.sql.Timestamp getCreateDatetime() {
return createDatetime;
}
public void setCreateDatetime(java.sql.Timestamp createDatetime) {
this.createDatetime = createDatetime;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName
+ ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark="
+ remark + "]";
}
}
5>为实体类写映射文件"User.hbm.xml"
6>在hibernate.cfg.xml添加映射的实体
7>写测试类
测试类:
package com.zking.one.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.zking.one.entity.User;
public class Demo1_add {
public static void main(String[] args) {
/**
* 1. 获得Configuration 读取配置文件
* 2.创建SessionFactory
* 3.打开Session
* 4.开启事务
* 5.使用session操作数据
* 6.提交事务
* 7.关闭资源
*
*/
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");//读取配置文件
SessionFactory sessionFactory=cfg.buildSessionFactory(); //创建SessionFactory
Session session = sessionFactory.openSession();//打开SessionFactory
Transaction transaction = session.beginTransaction(); //开启事务
User user=new User();
user.setUserName("admin");
user.setUserPwd("123");
session.save(user);//执行数据库操作 (save新增)
//user.setId(3);
//user.setUserPwd("123");
//user.setUserName("唐思语");
//session.update(user);//修改
//User user2=session.get(User.class, 2);//查询
//System.out.println(user2);
//
//if(user2!=null) {
// user2.setUserName("唐思语");//修改
//}
// User user3=session.get(User.class, 4);//查询
//
//if(user3!=null) {
// session.delete(user3);//删除
//}
transaction.commit();//提交事务
session.close();//关闭资源
}
}
注意事项
4.2.1 hibernate默认使用的是手动事务,因此必须显示的开启和提交事
务
4.2.2 删除操作时,必须先查再删