项目中框架的体系结构
![318c16d774079ea2b4f8d0196925682e.png](https://i-blog.csdnimg.cn/blog_migrate/902e7fec2a74b3da227481c3e943a151.jpeg)
![ad19e263edefaf6fce43926ca8e6ee51.png](https://i-blog.csdnimg.cn/blog_migrate/9d237b563de74cb7b7bf458578238531.jpeg)
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装
hibernate的使用步骤
A.下载hibernate 的jar包,并导入到项目中(http://hibernate.org/orm/)
B.创建数据库和表
C.配置核心配置文件hibernate.cfg.xml【这个文件有连接数据库的配置】
D.编写映射文件hibernate mapping(*.hbm.xml),【声明对象如何关联数据库表字段】
E.调用hibernate的api
hibernate的调用:增删查改
@Test
public void test1(){
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
// Transaction trans = session.getTransaction();
// trans.begin();
// 查询
// User user = (User) session.get(User.class, 2);
session.getTransaction().begin();//开启事务
// 插入
// User user = new User();
// user.setUsername("ccc");
// user.setPassword("234");
// session.save(user);
// trans.commit();
// 删除方法1
// User user = (User) session.get(User.class, 1);
// session.delete(user);
// 删除方法2
// User user = new User();
// user.setUid(2);
// session.delete(user);
// 修改方法1
// User user = (User) session.get(User.class, 1);
// user.setPassword("222");
// 修改方法2
// User user = new User();
// user.setUid(1);
// user.setPassword("222");
// user.setUsername("bbb");
// session.update(user);
session.getTransaction().commit();//事务提交
session.close();
factory.close();
}
hibernate的调用:条件查询,分页查询 HQL语言
@Test
public void test2(){
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
//条件查询
// Query query = session.createQuery("from User where username=? and password=?");
// query.setParameter(0,"bbb");
// query.setParameter(1,"222");
// User user = (User) query.uniqueResult();
// System.out.println(user);
//分页查询
Query query = session.createQuery("from User ");
query.setFirstResult(2);//起始位置
query.setMaxResults(2);//每页条数
List<User> list = query.list();
for (User user : list) {
System.out.println(user);
}
session.close();
factory.close();
}
hibernate的调用:模糊查询,criteria
@Test
public void test3() {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Criteria criteria = session.createCriteria(User.class);
// criteria.add(Restrictions.eq("username","a"));
// criteria.add(Restrictions.eq("password","1"));
// System.out.println(criteria.uniqueResult());
// criteria.add(Restrictions.gt("id",3));
criteria.add(Restrictions.like("username","%b%"));
System.out.println(criteria.list());
}
hibernate的调用:SQL语句查询
@Test
public void test4() {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
SQLQuery query = session.createSQLQuery("SELECT * from t_user");
List<Object[]> list = query.list();
for (Object[] o : list) {
for (Object o1 : o) {
System.out.println(o1);
}
System.out.println("----------");
}
session.close();
}
映射文件配置:User.hbm.xml(放在domain下)
<!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.gyf.hibernate.domain.User" table="t_user">
<!-- 1、主键,如果属性与表字段不一至,要指定column -->
<id name="uid" column="id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- 2、用户字段 -->
<property name="username"></property>
<property name="password"></property>
</class>
</hibernate-mapping>
核心配置文件:hibernate.cfg.xml(放src下)
<!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>
<!-- 1、配置数据库连接的4个参数 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day1</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 是否显示sql语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="format_sql">true</property>
<!-- 是否自动提交事务:针对insert有效,针对delete无效 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 开启与当前线程绑定session的功能
ThreadLocal<Connection>
ThreadLocal<ActionContext>
ThreadLocal<Session>
-->
<property name="hibernate.current_session_context_class">thread</property>
<!-- hibernate.hbm2ddl.auto
配置映射文件与数据库表的关系
update:如果数据库有没表,自动帮你创表【常用】
如果hbm与数据表不一样,会更新
create:每次启动hibernate都帮你创建表
create-drop,每次启动hibernate都帮你创建表,执行完后删除表
validate:检验hbm文件,如果与数据库的字符不一至,就抛出异常【过掉】
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- hiberante.dialect:数据库方言
mysql:分页limit
oracle:分页rownum
-->
<property name="hiberante.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2、配置JavaBean与表的映射文件 -->
<mapping resource="com/gyf/hibernate/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>