项目结构:
Hibernate 配置文件
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>
<property name="myeclipse.connection.profile">mysqlTest</property>
<property name="connection.url">
jdbc:mysql://10.2.11.6:3306/yyj <!-- ?characterEncoding=utf-8 处理编码 -->
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/yns/pojo/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
hibernate映射文件
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.yns.pojo.Student" table="student">
<!-- 属性名 和表的字段一样可以不写 -->
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="pass"></property>
</class>
</hibernate-mapping>
TestMain.java
package com.yns.pojo;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class TestMain {
public static void main(String[] args) {
// 主意Configuration()选hibernate下的 加载hibernate 配置文件
Configuration configure = new Configuration().configure();// 可以写configure(hibernate.cfg.xml);
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// Hql 操作
// 查询
/*
* Query query = session.createQuery("from Student where name=?");
* query.setParameter(0, "aa"); List<Student> list =
* query.list();//只要不是主键 都可能是多条数据 for(Student stu:list){
* System.out.println(stu.toString()); }
*/
/*
* Query query = session.createQuery("select id,name from Student");
* List<Object[]> list = query.list();// 分开写 返回值 是一个Object[]
* for(Object[] o:list){ System.out.println(o[0]+","+o[1]); }
*/
// 没有Student(id,name)构造方法 报 org.hibernate.hql.ast.QuerySyntaxException:
// Unable to locate appropriate constructor on class
/*
* Query query =
* session.createQuery("select new Student(id,name) from Student");
* List<Student> list = query.list();// 分开写 返回值 是一个Student for(Student
* stu:list){ System.out.println(stu.toString()); }
*/
// 聚合函数也可以使用 count avg max
/*----------count---------*/
/*
* Query query = session.createQuery("select count(*) from Student");
* Long l = (Long) query.uniqueResult();//此时只有一条结果
* System.out.println(l);
*/
/*----------max---------*/
/*
* Query query = session.createQuery("select max(id) from Student"); int
* i = (Integer) query.uniqueResult(); System.out.println(i+"");
*/
/*----------max---------*/
/*
* Query query = session.createQuery("select avg(id) from Student");
* Double d= (Double) query.uniqueResult(); System.out.println(d);
*/
// 子查询 ----大于平均id的人的信息
/*
* Query query =
* session.createQuery("from Student where id>(select avg(id) from Student)"
* ); List<Student> list = query.list(); for(Student stu:list){
* System.out.println(stu); }
*/
// in--- id 在1 2 3 中
/*
* Query query = session.createQuery("from Student where id in(1,2,3)");
* List<Student> list = query.list(); for(Student stu:list){
* System.out.println(stu); }
*/
// 也可以执行sql语句
SQLQuery query = session
.createSQLQuery("select *from student where id in(1,2,3)");// 写表名
List<Object[]> list = query.list();
for (Object[] o : list) {
System.out.println(o[0] + "," + o[1]);
}
transaction.commit();// 提交事务
}
}
ps:点我下载源代码