使用HQL查询所有记录
预见结果集
多个就用list处理
单个就用 uniqueResult
@Test
public void fun1() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
String hql = "from com.lanou3g.User";
Query<User> query = session.createQuery(hql);
List<User> list = query.list();
System.out.println(list);
transaction.commit();
session.close();
}
@Test
public void fun2() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
String hql = "from com.lanou3g.User where id = 2";
Query<User> query = session.createQuery(hql);
User user = query.uniqueResult();
System.out.println(user);
transaction.commit();
session.close();
}
使用问号占位符替代
?从0开始
@Test
public void fun3() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
String hql = "from com.lanou3g.User where id = ?";
Query<User> query = session.createQuery(hql);
query.setParameter(0, 2);
User user = query.uniqueResult();
System.out.println(user);
transaction.commit();
session.close();
}
使用冒号占位符
冒号后面是占位符的名字
注意 冒号后面不要加空格
替换时 直接使用名字即可
@Test
public void fun4() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
String hql = "from com.lanou3g.User where username = :username and password = :password";
Query<User> query = session.createQuery(hql);
query.setParameter("username", "wanglong");
query.setParameter("password", "123");
User user = query.uniqueResult();
System.out.println(user);
transaction.commit();
session.close();
}
分页
@Test
public void fun5() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
String hql = "from User";
Query<User> query = session.createQuery(hql, User.class);
query.setFirstResult(0);
query.setMaxResults(2);
List<User> list = query.list();
System.out.println(list);
transaction.commit();
session.close();
}
测试1对多
<?xml version="1.0" encoding="UTF-8"?>
<!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.lanou3g">
<class name="Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<!-- 配置一对多关系
name表示实体类对象属性名
column:两张表联系的外键
注意:外键名要一致 否则会多建一个外键
class 与哪个类进行一对多
-->
<!-- 级联操作
作用:简化代码
cascade
sava-update 级联保存和更新
delete 级联删除
all sava-update+delete
建议 最多使用sava-update
-->
<!--inverse反转
默认值是false
不放弃维护外键
作用:提高效率的
一对多的表关系
如果要放弃维护外键关系
只能是一的一方放弃
-->
<set name="linkmans" cascade="save-update" inverse="true">
<key column="lkm_cust_id"></key>
<one-to-many class="Linkman"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!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.lanou3g">
<class name="Linkman" table="cst_linkman">
<id name="lkm_id" column="lkm_id">
<generator class="native"></generator>
</id>
<property name="lkm_name" column="lkm_name"></property>
<property name="lkm_gender" column="lkm_gender"></property>
<property name="lkm_phone" column="lkm_phone"></property>
<property name="lkm_mobile" column="lkm_mobile"></property>
<property name="lkm_email" column="lkm_email"></property>
<property name="lkm_qq" column="lkm_qq"></property>
<property name="lkm_position" column="lkm_position"></property>
<property name="lkm_memo" column="lkm_memo"></property>
<!-- 配置多对一的关系
name表示多对一对象的属性名
class表示的是多对一对象的类
column表示的是外键
注意:上面在配置properties时不要重复配置外键
-->
<many-to-one name="customer" class="Customer" column="lkm_cust_id"></many-to-one>
</class>
</hibernate-mapping>
public class Demo02 {
@Test
public void fun1() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
//创建一个客户两个联系人
//表示客户和联系人的关系
//将以上添加到数据库中
Customer customer = new Customer();
customer.setCust_name("蓝鸥科技");
Linkman l1 = new Linkman();
l1.setLkm_name("欣姐");
Linkman l2 = new Linkman();
l2.setLkm_name("韦姐");
Set<Linkman> linkmans = customer.getLinkmans();
linkmans.add(l1);
linkmans.add(l2);
l1.setCustomer(customer);
l2.setCustomer(customer);
session.save(customer);
//session.save(l1);
//session.save(l2);
transaction.commit();
session.close();
}
//为id为1的客户添加一个联系人
@Test
public void fun2() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
Customer customer = session.get(Customer.class, 1);
Linkman l1 = new Linkman();
l1.setLkm_name("hh");
Set<Linkman> linkmans = customer.getLinkmans();
linkmans.add(l1);
l1.setCustomer(customer);
session.save(l1);
transaction.commit();
session.close();
}
//删除id为1的客户的id为1的联系人
@Test
public void fun3() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
Customer customer = session.get(Customer.class, 1);
Linkman linkman = session.get(Linkman.class, 1);
Set<Linkman> linkmans = customer.getLinkmans();
linkmans.remove(linkman);
linkman.setCustomer(null);
transaction.commit();
session.close();
}
}
测试多对多
<?xml version="1.0" encoding="UTF-8"?>
<!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.lanou3g">
<!--配置表与实体类的关系
name:实体类全类名
table:表名
package:填包名 填上后 底下再写全类名 可以省略包名
-->
<class name="User" table="sys_user">
<id name="user_id" column="user_id">
<generator class="native"></generator>
</id>
<property name="user_code" column="user_code"></property>
<property name="user_name" column="user_name"></property>
<property name="user_password" column="user_password"></property>
<property name="user_state" column="user_state"></property>
<!-- 配置多对多关系
name 容器名字
table 中间表的名字
column 中间表引用我的id
many-to-many class 对应的那个表的类名
column 对应那个表外键
-->
<!-- 多对多时必须要有一方放弃对外键的维护 -->
<set name="roles" table="sys_user_role" inverse="true">
<key column="user_id"></key>
<many-to-many class="Role" column="role_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!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.lanou3g">
<class name="Role" table="sys_role">
<id name="role_id" column="role_id">
<generator class="native"></generator>
</id>
<property name="role_name" column="role_name"></property>
<property name="role_memo" column="role_memo"></property>
<!-- 配置多对多关系
name 容器名字
table 中间表的名字
column 中间表引用我的id
many-to-many class 对应的那个表的类名
column 对应那个表外键
-->
<set name="users" table="sys_user_role">
<key column="role_id"></key>
<many-to-many class="User" column="user_id"></many-to-many>
</set>
</class>
</hibernate-mapping>
@Test
public void fun1() {
//创建两个员工
//创建两个角色 保姆 保镖
//这两员工 都是保姆也是保镖
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
User u1 = new User();
u1.setUser_name("王龙");
User u2 = new User();
u2.setUser_name("王一鸣");
Role r1 = new Role();
r1.setRole_name("保姆");
Role r2 = new Role();
r2.setRole_name("保镖");
Set<Role> roles1 = u1.getRoles();
roles1.add(r1);
roles1.add(r2);
Set<Role> roles2 = u2.getRoles();
roles2.add(r2);
roles2.add(r2);
r1.getUsers().add(u1);
r1.getUsers().add(u2);
r2.getUsers().add(u1);
r2.getUsers().add(u1);
session.save(u1);
session.save(u2);
session.save(r1);
session.save(r2);
transaction.commit();
session.close();
}
//为王龙添加一个新角色
@Test
public void fun2() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
User user = session.get(User.class, 1);
Role role = new Role();
role.setRole_name("讲师");
Set<Role> roles = user.getRoles();
roles.add(role);
role.getUsers().add(user);
session.save(user);
session.save(role);
transaction.commit();
session.close();
}
//为王龙删除一个id为3的角色
@Test
public void fun3() {
Session session = HibernateUtil.getOpensession();
Transaction transaction = session.beginTransaction();
User user = session.get(User.class, 1);
Role role = session.get(Role.class, 3);
user.getRoles().remove(role);
role.getUsers().remove(user);
transaction.commit();
session.close();
}