引入Hibernate相关jar
配置文件 hibernate.cfg.xml和hibernate.properties其中之一
hibernate.cfg.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property>
<!--设置数据库的连接url:jdbc:mysql://localhost/hibernate,其中localhost表示mysql服务器名称,此处为本机, hibernate是数据库名-->
<property name="hibernate.connection.url">jdbc:mysql://localhost/test </property>
<!--连接数据库是用户名-->
<property name="hibernate.connection.username">root</property>
<!--连接数据库是密码-->
<property name="hibernate.connection.password">root</property>
<!--数据库连接池的大小-->
<property name="hibernate.connection.pool.size">20</property>
<!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率-->
<property name="hibernate.show_sql">true </property>
<property name="hibernate.format_sql">true </property>
<property name="hibernate.use_sql_comments">true </property>
<!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->
<property name="jdbc.fetch_size">50 </property>
<!--jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->
<property name="jdbc.batch_size">400 </property>
<!--jdbc.use_scrollable_resultset是否允许Hibernate用JDBC的可滚动的结果集。对分页的结果集。对分页时的设置非常有帮助-->
<property name="jdbc.use_scrollable_resultset">false </property>
<!--connection.useUnicode连接数据库时是否使用Unicode编码-->
<property name="Connection.useUnicode">true </property>
<!--connection.characterEncoding连接数据库时数据的传输字符集编码方式,最好设置为gbk,用gb2312有的字符不全-->
<property name="connection.characterEncoding">utf8</property>
<!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
<!--指定映射文件为“hibernate/ch1/UserInfo.hbm.xml”-->
<mapping resource="com/cor/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<mapping resource="com/cor/entity/User.hbm.xml"/>
要放在所有porperty后面
注意
mapper必须放在所有property 下面
hibernate对象状态
1、瞬时状态 数据库中没有
2、持久状态—save之后
3、游离(脱管状态)状态—-commit之后
HQL
面向对象查询语言,把表变成对象,把字段变成属性
HQL中的参数通过query接口注入,并且有query接口负责查询
hbm.xml
<?xml version="1.0"?>
<!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.coracle.entity">
<class name="User">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="birthday"></property>
</class>
</hibernate-mapping>
获取sessionFactory 和Session
public static Session getSession() {
Session s = null;
Transaction t = null;
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
s = sf.openSession();
return s;
}
查询demo HQL
public static void main(String[] args) {
Session session = HibernateUtils.getSession();
String sql = "from User where name = :name and age=:age";
Query query = session.createQuery(sql);
query.setMaxResults(1);
query.setString("name", "test");
query.setString("age", "12");
Object o= query.uniqueResult();
System.out.println(o);
List<User> li = query.list();
for (User u : li) {
System.out.println(u.getName());
}
session.close();
}
##Criteria
public static void main(String[] args) {
Session s = HibernateUtils.getSession();
Criteria cri = s.createCriteria(User.class);
cri.add(Restrictions.eq("age", "12"));
cri.add(Restrictions.eq("name", "test"));
List<User> us=cri.list();
for(User u : us){
System.out.println(u.getAge());
}
}
分页(HQL相同)
public static void main(String[] args) {
Session s = HibernateUtils.getSession();
Criteria cri = s.createCriteria(User.class);
cri.setFirstResult(0);
cri.setMaxResults(3);
List<User> us=cri.list();
for(User u : us){
System.out.println(u.getAge());
}
}
“`