-
创建Maven项目
- 1.什么是groupid和artifactId?
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。 - 注意:如果选择的时War,pom.xml会报错。
- 解决方案:在src->webapp下面新建 WEB-INF文件夹,里面粘贴一个web.xml
- 1.什么是groupid和artifactId?
-
在pom.xml里面引用hibernate的依赖
- 百度搜索mvn
- 搜索hibernate
- 找带有core的
-
hibernate的运行需要hibernate.cfg.xml
-
注意名字必须是它,具体里面的配置内容寻找百度
-
放在src/main/resources下面
-
里面包含了要连接数据库的基本信息,例如数据库的账号,密码
重点代码
1
2
3
4
5Configuration cfg = new Configuration().configure();
SessionFactory fac = cfg.buildSessionFactory();//会启动一个新线程
Session session = fac.openSession();//打开一个数据库对话,肯定会建立数据库
session.close();//关闭session
fac.close();//关闭fac
-
创建实体类,配置mapping,保存对象
-
创建实体类
-
例如创建一个entity包,里面放一个User类
-
新建一个mapping文件
-
网上有mapping文件
-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- mapping文件用来描述Entity与数据表的关系 -->
<hibernate-mapping>
<class name="entity.User" table="rethink_user" >
<id name="id" column="rethink_id">
<generator class="idntity"></generator>
</id>
<property name="username" column="rethink_name" type="string" length="16"></property>
<property name="password" column="rethink_password"></property>
<property name="age"></property>
</class>
</hibernate-mapping>对mapping编写完成后需要在hibernate写入映射
1
<mapping resource ="entity/Person.hbm.xml"></mapping>
-
-
接着运行Test.java,可能会出现如下错误
数据库方言Error
1 |
Error executing DDL via JDBC Statement |
-
解决数据库方言BUG
-
1
2<!-- 数据库方言配置 org.hibernate.dialect.MySQLDialect (出现问题就更换) -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> -
保存对象
- 在Test.java里面编写
1 |
package demo; |
-
重点代码
User user = new User();
**user.setAge(18);** **user.setUsername("刘延昊");** **user.setPassword("123456");** **session.save(user);**
删除对象,事务启动,提交,回滚
删除对象
1 |
package test; |
- 注意要启动事务
- 注意要提交事务
保存50个对象,修改对象
TestSave50.java
1 |
package test; |
- session.save(user);
TestUpdate.java
1 |
package test; |
Query 与list —-Query与uniqueResult
TestQuery.java
1 |
package test; |
- **String hql = “from entity.User where age>30”; //也可以加别名
Query<User> query = session.createQuery(hql); List<User> list = query.list(); for(User us:list) { System.out.println(us.getUsername()); } Query<User> query2 = session.createQuery("from entity.User where username='刘3'"); User un = query2.uniqueResult();//如果查出多个数据,会抛异常 System.out.println(un.getId());**
Hibernate使用UUID主键
Book.java
1 |
package entity; |
Book.hbm.xml
1 |
|
在hibernate.cfg.xml里面配置相关的mapping
TestUUID.java
1 |
package test; |
OpenSession与CurrentSession
-
首先在hibernate.cfg.xml里面配置
1
2
3> <!-- 将Session与线程绑定=> 只有配置了该配置,才能使用getCurrentSession -->
> <property name="hibernate.current_session_context_class">thread</property>
>
TestSession.java
1 |
package test; |
OpenSession 打开新的Session
GetCurrentSession 获取最近的Session,我们需要在配置文件中描述何为最近GetCurrentSession()会获取最近的CurrentSession,如果没有就建一个,
OpenSession在查询的时候可以不用事务,用过之后必须关闭
CurrentSession必须使用事务,使用之后不用关闭无论什么Session,无论增删改查,都加事务就肯定没错,
带有参数的HQL语句(问号参数和冒号参数)
TestParams.java
1 |
package test; |
-
String hql = “from User u where u.username = ?”;
-
String hql2 = “from User u where u.username =:bieming”;
投影查询(只查询属性的几个属性)
TestQuery2.java
1 |
package test; |
- **String hql = “select new User(id,username) from User”;
<font color ='purple'>Query<User> query = session.createQuery(hql);</font>**
HQL得到数据数组
1 |
package test; |
- String hql = “select username,age from User”;
近期评论