调整java单行格式化时注释不换行:
Window --> Preferences --> Java --> Code Style --> Formatter --> New(自已新建一个格式) --> Edit --> Comments --> Maximum line width for comments:xxx(拉到最底下,调整右边框内数字)

HQL查询
1,简单查询
2,带上过滤条件的(可以使用别名):Where
3,带上排序条件的:Orace by
4,指定select子句(不可以使用select *)
可以使用new语法,指定把查询出的部分属性封装到对象中
5,执行查询,获得结果(list、uniqueResult、分页)
6,方法链


// 准备数据
@Test
public void testSave() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// ------------------------------------

// 保存一些部门
for (int x = 1; x <= 10; x++) {
Department department = new Department();
department.setName("开发部_" + x);
// 保存
session.save(department);
}

// 保存一些员工
for (int x = 1; x <= 20; x++) {
Employee employee = new Employee();
employee.setName("李xx_" + x);
// 保存
session.save(employee);
}
// ------------------------------------
tx.commit();
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
session.close();
}
}

// 使用HQL查询
// HQL:Hibernate Query Language
// 特点
// >> 1,与SQL相似,SQL中的语法基本上都可以直接使用。
// >> 2,SQL查询的是表和表中的列,HQL查询的是对象与对象中的属性
// >> 3,HQL的关键字不区分大小写,类名与属性名区分大小写的。
// >> 4,SELECT省略
@Test
public void testHql() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// ------------------------------------
String hql = null;//

// 1,简单查询
// hql = "from Employee";
// hql = "from Employee as e";// 使用别名
// hql = "from Employee e";// 使用别名,as关键字可以省略

// 2,带上过滤条件的(可以使用别名):Where
// hql = "from Employee e where id<10";
// hql = "from Employee e where e.id<10 and e.id>5";

// 3,带上排序条件的:Orace by
// hql = "from Employee e where e.id<10 order by e.name";
// hql = "from Employee e where e.id<10 order by e.name desc";
// hql = "from Employee e where e.id<10 order by e.name desc,id asc";//

// 4,指定select子句(不可以使用select *)
// hql = "select e from Employee e";// 相当于"from Employee e"
// hql = "select e.name from Employee e";// 只查询一个指定的列,返回的集合的元素类型就是这个属性的类型
// hql = "select e.id,e.name from Employee e";// 只查询多个指定的列,返回的集合的元素类型是Object数组

// >> 可以使用new语法,指定把查询出的部分属性封装到对象中
// hql = "select new Employee(e.id,e.name) from Employee e";// 只查询多个指定的列,返回的集合的元素类型是Object数组

// 5,执行查询,获得结果(list、uniqueResult、分页)
// Query query = session.createQuery("from Employee e where e.id=300");
// query.setFirstResult(0);
// query.setMaxResults(10);
// List<Object> list = query.list();// 查询的结果是一个List集合
// System.out.println(list.get(0));
// Employee employee = (Employee) query.uniqueResult();// 查询的结果是唯一的一个结果,当结果有多个,就会抛异常。
// System.out.println(employee);

// 6,方法链
List<Object> list = session.createQuery(//
"from Employee e")//
.setFirstResult(0)//
.setMaxResults(10)//
.list();

// ------------------------------------

// // ----- 执行查询
// List<Object> list = session.createQuery(hql).list();
// ----- 显示结果
for (Object obj : list) {
if (obj.getClass().isArray()) {// 是数组
System.out.println(Arrays.toString((Object[]) obj));
} else {
System.out.println(obj);
}
}

tx.commit();
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
session.close();
}
}