hibernate 原生sql
交叉连接-笛卡儿积(避免使用)
select * from A,B
内连接
|-隐式内连接
select * form A,B where a.id = b.aid
|-显示内连接
select * from A inner join B on a.id = b.aid
外连接
|-左外连接
select * from A left inner join B on a.id = b.aid
|-右外连接
select * from A right inner join B on a.id = b.aid
【基本查询】
String sql = "select * from cst_customer"
SQLQuery query = session.createSQLQuery(sql)
List<Object[]> list = query.list() 如果要想写成List<Customer> list = query.list()这样的形式,那么之前要加上query.addEntiry(Customer.class)
【条件查询 问号占位符】
String hql = “ select * from cst_customer where cust_id = ?"
SQLQuery query = session.createSQLQuery(sql)
query.setParameter(0,1l) //占位符是从0开始
query.addEntity(Customer.class)
Customer customer = query.uniqueResult()
【分页查询】
String hql = “ select * from cst_customer limit ?,?"
SQLQuery query = session.createSQLQuery(sql)
query.setParameter(0,0) //占位符是从0开始
query.setParameter(1,1) //设置每页显示条数
query.addEntity(Customer.class)
List<Customer> lsit= query.list()