public class User {
private int uid;
private String username;
private String password;
//get/set方法 必须要有
}
3、映射文件
4、事务
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
SessionFactory factory = configure.buildSessionFactory();
Session session = factory.openSession();// 获取连接
// 开启事务
Transaction tx = session.beginTransaction();
User user = new User("黄蓉", "123");
session.save(user);
tx.commit();// 提交事务
session.close();// 关闭连接
image.png
sessioin查询api
save、delete、update
get 不支持延迟查询
load 延迟查询:如果使用了对象中非id的属性时才会发送sql语句
saveOrUpdate
瞬时态执行save(),游离态执行update()
merge
两个相同id的对象合并
实体类的三种状态
瞬时态:
无id,与session无关联,与数据库无关联
持久态:
有id,与session有关联,与数据库关联 持久态对象修改数据,会自动修改数据库的数据
游离态(离线):
有id,与session无关联,与数据库有关
image.png
关联关系
多对一
多个Customer对应一个User
image.png
一对多
一个User对应多个Customer
image.png
多对多
image.png
hibernate查询api
oid
就是根据对象的id查询,例如get()、load()
hql Query
// -----------------条件查询-------------------
@Test
public void test1() {
String hql = "from User u where u.uid=1";
Query query = session.createQuery(hql);
User u = query.uniqueResult();
System.out.println(u);
}
@Test
public void test2() {
String hql = "from User u where u.username='lisi' and u.password='123'";
Query query = session.createQuery(hql);
User u = query.uniqueResult();
System.out.println(u);
}
// ---------------占位符条件查询------------------
// 占位符条件查询 ?
@Test
public void test3() {
String hql = "from User u where u.username=? and u.password=?";
Query query = session.createQuery(hql);
query.setParameter(0, "lisi");
query.setParameter(1, "123");
User u = query.uniqueResult();
System.out.println(u);
}
// 命名占位符条件查询1 parameter
@Test
public void test4() {
String hql = "from User where username=:username and password=:password";
Query query = session.createQuery(hql);
query.setParameter("username", "lisi");
query.setParameter("password", "123");
User u = query.uniqueResult();
System.out.println(u);
}
// 命名占位符条件查询2 javabean
@Test
public void test5() {
String hql = "from User where username=:username and password=:password";
Query query = session.createQuery(hql);
User user = new User("lisi", "123");
query.setProperties(user);
User u = query.uniqueResult();
System.out.println(u);
}
// 命名占位符条件查询3 map,可以解决命名占位符与实际参数名不一样的问题
@Test
public void test6() {
String hql = "from User where username=:user and password=:pwd";
Query query = session.createQuery(hql);
Map map = new HashMap<>();
map.put("user", "lisi");
map.put("pwd", "123");
query.setProperties(map);
User u = query.uniqueResult();
System.out.println(u);
}
// 模糊查询
@Test
public void test7() {
String hql = "from User where username like ?";
Query query = session.createQuery(hql);
query.setParameter(0, "%七%");
List list = query.getResultList();
System.out.println(list);
}
// 查询列表,并且排序
@Test
public void test8() {
String hql = "from User order by password desc";
Query query = session.createQuery(hql);
List list = query.getResultList();
System.out.println(list);
}
// 分页查询
@Test
public void test9() {
String hql = "from User";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(3);
List list = query.getResultList();
System.out.println(list);
}
// 动态查询
@Test
public void test10() {
User user = new User();
user.setPassword("123");
String hql = "from User where 1=1";
StringBuffer sb = new StringBuffer(hql);
if (user.getPassword() != null && user.getPassword() != "") {
sb.append(" and password=:password");
}
Query query = session.createQuery(sb.toString());
query.setProperties(user);
List list = query.getResultList();
System.out.println(list);
}
// --------------------连接------------------------
// 内连接,封装成数组,对象数组 查询roleid=1的用户
@Test
public void test11() {
String hql = "from User u inner join u.roleSet ro where ro.roleid=1";
Query
qbc Citira
// 模糊查询
@Test
public void test2() {
Criteria criteria = session.createCriteria(User.class)
.add(Restrictions.like("username", "%七%"));
List list = criteria.list();
System.out.println(list);
}
// 多条件查询
@Test
public void test3() {
Criteria criteria = session.createCriteria(User.class)
.add(Restrictions.eq("username", "李四"))
.add(Restrictions.eq("password", "123"));
List list = criteria.list();
System.out.println(list);
}
//排序
@Test
public void test4() {
Criteria criteria = session.createCriteria(User.class)
.addOrder(Order.desc("password"));//降序
List list = criteria.list();
System.out.println(list);
}
//分页
@Test
public void test5() {
Criteria criteria = session.createCriteria(User.class)
.setFirstResult(3)
.setMaxResults(3);
List list = criteria.list();
System.out.println(list);
}
//---------------------投影查询---------------------
//聚合函数 count()
@Test
public void test6(){
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(Projections.rowCount());//count()
Object result = criteria.uniqueResult();
System.out.println(result);
}
//分组计算
@Test
public void test7(){
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(Projections.projectionList()
.add(Projections.rowCount())
.add(Projections.groupProperty("password")));
List list = criteria.list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
}
//选出部分字段
@Test
public void test8(){
Criteria criteria = session.createCriteria(User.class);
criteria.setProjection(Projections.projectionList()
.add(Projections.property("username"))
.add(Projections.property("password")));
List list = criteria.list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
}
用原型函数(prototype)可以定义一些很方便的自定义函数,实现各种自定义功能。本次主要是实现了Array的去重、获取最大值和最小值。
实现代码如下:
<script type="text/javascript">
Array.prototype.unique = function() {
var a = {};
var le
ORACLE
下面这个效率很低
SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM IPAY_RCD_FS_RETURN order by id desc) A ) WHERE RN <20;
下面这个效率很高
SELECT A.*, ROWNUM RN FROM (SELECT * FROM IPAY_RCD_
Reverse a singly linked list.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
p