1、Hibernate 单表配置
1、实体类(Student.java)
public class Student{
private Integer id;
private String username;
private String password;
}
2、映射文件(Student.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>
<class name="com.qf.entity.Students" table="student">
<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>
3、核心配置文件(hibernate.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!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:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</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.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="cn/itcast/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、工具类(HibernateUtils.Java)
public class HibernateUtils {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<>();
private static Configuration cfg = new Configuration();
private static SessionFactory factory;
private static Session session;
static {
cfg.configure();
factory = cfg.buildSessionFactory();
}
private CRUDUtil() {}
public static Session getSession() {
Session session = threadLocal.get();
if (session == null || !session.isOpen()) {
if (session == null) {
rebuildSessionFactory();
}
session = factory.openSession();
}
return session;
}
public static void rebuildSessionFactory() {
cfg.configure();
factory = cfg.buildSessionFactory();
}
public static void closeSession(Closeable... sessions) {
for (Closeable session : sessions) {
if (session != null) {
session.close();
}
}
}
}
2、Hibernate CRUD操作
1、添加
Student student=new Student();
student.setId(2);
student.setUsername("张三");
student.setPassword("1234");
session.save(student);
session.saveOrUpdate(student);
2、删除(根据Id删除)
Student student=session.get(Student.class,2);
session.delete(student);
3、修改(根据Id的修改)
Student student=session.get(Student.class,3);
student.setUsername("李四");
student.setPassword("123");
session.update(student);
session.saveOrUpdate(student);
Student student=new Student();
student.setId(2);
student.setUsername("李四");
student.setPassword("123");
session.saveOrUpdate(student);
4、查询
(1)、OID查询(根据Id查询 - - - - - - 对象 )
Student studernt=session.get(Student.class,1)
(2)、HQL查询
String hql="from Student";
Query query=session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(3);
String hql="from Student where username=? and password=?";
Query query=session.createuery(hql);
query.setParameter(0,"王%_");
query.setParameter(1,"1234");
String hql="from Student order by id asc";
Query query=session.createQuery(hql);
List<Student> list=query.list();
String hql="select count(1) from student [where username=? and password=?]";
Query query=session.createQuery(hql);
Object obj=query.uniqueResult();
Long lobj=(Long)obj;
int count=lobj.intValue();
(3)、QBC查询
Criteria criteria=session.createCriteria(Student.class);
criteria.setFirstResult(0);
criteria.setMaxResults(3);
Criteria criteria=session.createCriteria(Student.class);
criteria.add(Restrictions.eq("username","张三"));
criteria.add(Restrictions.eq("password","1234"))
Criteria criteria=session.createCriteria(Student.class);
criteria.addOrder(Order.asc("id"));
List<Student> list=criteria.list();
Criteria criteria=session.createCriteria(Student.class);
criteria.add(Restrictions.eq("username","张三"));
criteria.add(Restrictions.eq("password","1234"))
Object obj=criteria.uniqueResult();
Long lobj=(Long)obj;
int count=lobj.intValue();
(4)、SQL查询(了解)
String sql="select * from Student";
String sql="select * from Student limit ?,?";
SQLQuery query=session.createSQLQuery(sql);
query.setParameter(0,0);
query.setParameter(0,3);
String hql="select * from Student where username=? and password=?";
SQLQuery query=session.createSQLQuery(sql);
query.setParameter(0,"王%_");
query.setParameter(1,"1234");
String hql="select * from Student order by id asc";
SQLQuery query=session.createSQLQuery(sql);
query.addEntity(Student.class);
List<Student> list=query.list();
String hql="select count(1) from student [where username=? and password=?]";
SQLQuery query=session.createSQLQuery(sql);
Object obj=query.uniqueResult();
Long lobj=(Long)obj;
int count=lobj.intValue();
3、Hibernate 一对多配置
1、实体类
(1)、客户类(一对多)Customer.java
public class Customer {
private Integer cid;
private String custName;
private String custLevel;
private String custSource;
private String custPhone;
private String custMobile;
private Set<LinkMan> setLinkMan = new HashSet<LinkMan>();
}
(2)、联系人类(多对一)LinkMan.java
public class LinkMan {
private Integer lkm_id;
private String lkm_name;
private String lkm_gender;
private String lkm_phone;
private Customer customer;
}
2、映射文件
(1)、客户(一对多)Customer.hbm.xml
<hibernate-mapping>
<class name="cn.itcast.entity.Customer" table="t_customer">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="custName" column="custName"></property>
<property name="custLevel" column="custLevel"></property>
<property name="custSource" column="custSource"></property>
<property name="custPhone" column="custPhone"></property>
<property name="custMobile" column="custMobile"></property>
<set name="setLinkMan" inverse="true">
<key column="clid"></key>
<one-to-many class="cn.itcast.entity.LinkMan"/>
</set>
</class>
</hibernate-mapping>
(2)、联系人(多对一)LinkMan.hbm.xml
<hibernate-mapping>
<class name="cn.itcast.entity.LinkMan" table="t_linkman">
<id name="lkm_id" column="lkm_id">
<generator class="native"></generator>
</id>
<property name="lkm_name" column="lkm_name"></property>
<property name="lkm_gender" column="lkm_gender"></property>
<property name="lkm_phone" column="lkm_phone"></property>
<many-to-one name="customer" class="cn.itcast.entity.Customer" column="clid"></many-to-one>
</class>
</hibernate-mapping>
3、核心配置文件(hibernate.cfg.xml)
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day03</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping resource="cn/itcast/entity/Customer.hbm.xml"/>
<mapping resource="cn/itcast/entity/LinkMan.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、Hibernate 一对多CRUD
1、添加
1、第一步(给Customer配置文件)
<set name="setLinkMan" cascade="save-update"></set>
2、第二步
Customer customer = new Customer();
customer.setCustName("百度");
.....
LinkMan linkman = new LinkMan();
linkman.setLkm_name("小宏");
......
customer.getSetLinkMan().add(linkman);
session.save(customer);
2、删除(根据Id修改)
1、第一步(给Customer配置文件)
<set name="setLinkMan" cascade="save-update,delete"></set>
2、第二步(根据Id添加)
Customer customer=session.get(Customer.class,2);
session.delete(customer);
3、Hibernate 多对多配置
1、实体类
(1)、教师类(多对多)Teacher.java
public class Teacher {
private Integer tea_id;
private String tea_name;
private String tea_phone;
private Set<Student> student= new HashSet<Student>();
}
(2)、学生类(多对多)Student.java
public class Student{
private Integer stu_id;
private String stu_name;
private String stu_gender;
private String stu_phone;
private Set<Teacher> student= new HashSet<Teacher>();
}
2、映射文件
(1)、教师(多对多)Teacher.hbm.xml
<hibernate-mapping>
<class name="cn.itcast.entity.Teacher" table="t_teacher">
<id name="tea_id" column="tea_id">
<generator class="native"></generator>
</id>
<property name="tea_name" column="tea_name"></property>
<property name="tea_phone" column="tea_phone"></property>
<set name="setLinkMan" table="three_tables">
<key column="clid"></key>
<many-to-many class="cn.itcast.entity.Student" column="user_id"/>
</set>
</class>
</hibernate-mapping>
(2)、学生人(多对多)Student.hbm.xml
<hibernate-mapping>
<class name="cn.itcast.entity.Student" table="t_student">
<id name="stu_id" column="stu_id">
<generator class="native"></generator>
</id>
<property name="stu_name" column="stu_name"></property>
<property name="stu_gender" column="stu_gender"></property>
<property name="stu_phone" column="stu_phone"></property>
<set name="setLinkMan" table="three_tables">
<key column="clid"></key>
<many-to-many class="cn.itcast.entity.Teacher" column="user_id"/>
</set>
</class>
</hibernate-mapping>
3、核心配置文件(hibernate.cfg.xml)
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day03</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping resource="cn/itcast/entity/Teacher.hbm.xml"/>
<mapping resource="cn/itcast/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、Hibernate 一对多CRUD
1、添加
1、第一步(给Teacher配置文件)
<set name="setLinkMan" table="three_tables" cascade="save-update"></set>
2、第二步
Teacher teacher = new Teacher();
teacher.setTeaName("百度");
.....
Student student = new Student();
student.setStuName("小宏");
......
Teacher.getSetTeacher().add(student);
Student.getSetStudent().add(teacher);
session.save(Teacher);