1.实体类规范(javaBean)
1.提供无参构造方法
2.成员变量私有化 提供get/set方法
double 表示学分 0--100.0 没考试 0
Double 有null值 缺考 用空值表示 0分
3.基本数据类型尽量使用包装类型(可以多表示一个值null 例如考试分数 缺考和0分)
4.实体中必须要提供一个与表中的主键对应的属性id
hibernate是通过主键来区别对象是否相同
5.不要使用final修饰实体类(代理对象是要继承该类的)
2.主键类型
1.自然主键
输入人的时候 人的身份证号 保证了 非空不重复 符合主键的贴点
但是身份证号 本身是业务字段 可以作为主键使用 叫做自然主键
2.代理主键(常用)
找不到主键(没有业务字段符合主键规则)
搞一个没有任何意义的主键id列 作为主键 就是为了非空不重复 叫做代理主键
3.主键生成策略.
1.identity:主键自增 由数据库来维护主键 录入的时候不需要指定主键
2.increment:主键自增 由hibernate来维护主键 每次插入前会先查询表中 id最大值 +1 作为新主键
效率不高 每次都要查询 如何10个人同时要保存 可能会出现线程问题
3.sequence:Oracle中的主键生成策略 序列
4.hilo:高低位算法(数据库中的主键自增算法原理) 由hibernate来维护主键 hibernate自己算一下(无意义)
5.native:hilo + sequence + identity 自动三选一 检测你是什么数据库
mysql就identity Oracle就sequence 如果没有就 用hilo
但是世界上没有一个 不支持自增和序列的数据库 其实就是 2选1
6.uuid:产生一个随机的字符串作为主键 主键类型必须为string类型
7.assigned:自然主键生成策略 (没策略) hibernate不会管理 由开发人员自己录入
4.HQL的三种状态
1.瞬时态 没有id 没有和session关联
2.持久态 有id 有关联
3.游离态 有id 没有关联
测试:save get 状态的改变
结论:就是将我们想要同步到数据库的数据 所对应的对象转换成持久态
5.事物的隔离级别
READ UNCOMMITTED 脏读 1
READ COMMITTED 幻读 2
REPEATABLE READ 可重复读 4
SERIALIZABLE 串行化(只能一个一个访问) 8
2.设置隔离级别
<property name="hibernate.connection.isolation">4</property>
SQL语句设置
specify a JDBC isolation level hibernate.connection.isolation 1|2|4|8
6.获取当前session 与当前线程绑定的
<property name="hibernate.current_session_context_class">thread</property>
注意:
1.配置配置文件
2.调用getCurrentSession获取session 当事务提交 session会自动关闭 不需要手动关闭
7.HQL查询
查询所有对象
String HQL = "from User"
按条件查询 HQL不会出现表中字段 id 代表的是属性名
String HQL = "from User where id = 1"
查询方法
Query query = session.createQuery(HQL)
返回结果(预见结果)
集合 list()
单个 uniqueResult()
问号?占位符
因为面向对象的查询 这里的id 指的是 实体类中的属性名
String HQL = "from User where id = ? and username = ?"
Query query = session.createQuery(HQL)
赋值参数的时候 参数1 占位符的索引 注意:HQL索引从0开始的
query.setParameter(0, 2)
query.setParameter(1, "www1")
冒号:占位符(注意冒号后面别来空格兄弟)
String HQL = "from User where id = :ww1 and username = :ww2"
分页查询
String HQL = "from User"
Query query = session.createQuery(HQL)
limit ?,? 相同 起始 和 最大显示
query.setFirstResult(2)
query.setMaxResults(2)
Criteria无语句查询(单表查询)
Criteria criteria = session.createCriteria(User.class)
List<User> list = criteria.list()
需要预见结果 使用单对象返回
User user = (User) criteria.uniqueResult()
System.out.println(user)
添加查询条件
criteria.add(Restrictions.eq("id", 2))
User user = (User) criteria.uniqueResult()
System.out.println(user)
HQL运算符 QBC运算符 含义
= Restrictions.eq() 等于
> Restrictions.gt() 大于
>= Restrictions.ge() 大于等于
< Restrictions.lt() 小于
<= Restrictions.le() 小于等于
is null Restrictions.isnull() 等于空值
is not null Restrictions.isNotNull() 非空值
like Restrictions.like() 字符串模式匹配
and Restrictions.and() 逻辑与
or Restrictions.or() 逻辑或
not Restrictions.not() 逻辑非
in(列表) Restrictions.in() 等于列表中的某一个值
between x and y Restrictions.between() 闭区间xy中的任意值
查询总行数
criteria.setProjection(Projections.rowCount())
原生sql查询(复杂的多表查询)
String sql = "select * from user where id=?"
SQLQuery query = session.createSQLQuery(sql)
query.setParameter(0, 3)
预见返回结果集
注意:返回数据的类型
1.创建User.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lanou3g">
<class name="User" table="user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="username" column="username"></property>
<property name="password" column="password"></property>
</class>
</hibernate-mapping>
2.创建hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/a_hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.isolation">4</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/lanou3g/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.创建实体类User
package com.lanou3g;
public class User {
private int id;
private String username;
private String password;
public User() {
super();
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4.创建HibernateUtils.java类
package com.lanou3g;
public class User {
private int id;
private String username;
private String password;
public User() {
super();
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6.创建测试类测试HQL语句
package com.lanou3g;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.jupiter.api.Test;
public class Demo01 {
@Test
public void fun1() {
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
User user = new User();
user.setUsername("wanglong");
user.setPassword("123");
session.save(user);
transaction.commit();
session.close();
}
@Test
public void fun2() {
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
User user = new User();
user.setUsername("wanglong");
user.setPassword("123");
session.save(user);
transaction.commit();
session.close();
}
@Test
public void fun3() {
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
User user1 = session.get(User.class, 1);
User user2 = session.get(User.class, 1);
User user3 = session.get(User.class, 1);
User user4 = session.get(User.class, 1);
User user5 = session.get(User.class, 1);
System.out.println(user2==user2);
transaction.commit();
session.close();
}
@Test
public void fun4() {
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
User user = session.get(User.class, 1);
user.setUsername("haha");
user.setUsername("dp");
session.update(user);
transaction.commit();
session.close();
}
@Test
public void fun5() {
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
User user = new User();
user.setId(1);
session.update(user);
User u = session.get(User.class, 1);
transaction.commit();
session.close();
}
}
package com.lanou3g
import java.util.List
import org.hibernate.Criteria
import org.hibernate.Session
import org.hibernate.Transaction
import org.hibernate.criterion.Projections
import org.hibernate.criterion.Restrictions
import org.hibernate.query.Query
import org.junit.jupiter.api.Test
public class Demo02 {
@Test
public void fun1() {
String hql = "from User"
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
Query query = session.createQuery(hql)
List<User> list = query.list()
//预见一个结果(单数据)
//User user = (User)query.uniqueResult()
System.out.println(list)
transaction.commit()
session.close()
}
@Test
public void fun2() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
//---------------------------------
String hql = "from User where id=?"
Query query = session.createQuery(hql)
query.setParameter(0, 5)
User user = (User) query.uniqueResult()
System.out.println(user)
//-----------------------------------
transaction.commit()
session.close()
}
@Test
public void fun3() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
//---------------------------------
//冒号后面相当于给这个冒号占位符起了个别名
//注意:冒号后面不要搞空格
String hql = "from User where id = :www"
Query query = session.createQuery(hql)
//直接使用别名给占位符赋值
query.setParameter("www", 5)
System.out.println(query.uniqueResult())
//-----------------------------------
transaction.commit()
session.close()
}
@Test
public void fun4() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
//---------------------------------
String hql = "from User"
Query query = session.createQuery(hql)
//设置起始和最大显示
query.setFirstResult(0)
query.setMaxResults(3)
List list = query.list()
System.out.println(list)
//-----------------------------------
transaction.commit()
session.close()
}
@Test
public void fun5() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
//---------------------------------
Criteria criteria = session.createCriteria(User.class)
List list = criteria.list()
System.out.println(list)
//-----------------------------------
transaction.commit()
session.close()
}
@Test
public void fun6() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
//---------------------------------
Criteria criteria = session.createCriteria(User.class)
//添加一个查询条件
criteria.add(Restrictions.eq("id", 3))
User user = (User) criteria.uniqueResult()
System.out.println(user)
//-----------------------------------
transaction.commit()
session.close()
}
@Test
public void fun7() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
//---------------------------------
Criteria criteria = session.createCriteria(User.class)
criteria.setProjection(Projections.rowCount())
//一般返回值 是个数的通常都使用大Long来接受
Long l = (Long) criteria.uniqueResult()
System.out.println(l)
//-----------------------------------
transaction.commit()
session.close()
}
@SuppressWarnings("deprecation")
@Test
public void fun8() {
Session session = HibernateUtils.getSession()
Transaction transaction = session.beginTransaction()
//---------------------------------
String sql = "select * from user where id=?"
Query query = session.createSQLQuery(sql)
query.setParameter(0, 3)
//一条记录就是一个object数组
List list = query.list()
System.out.println(list)
//-----------------------------------
transaction.commit()
session.close()
}
}