HIbernate CURD操作
添加:save()
更新:update()
删除:delete()
通过id查询:get(*.class,id)
查询所有:session.createQuery(“from User”).list()
分页:
query.setFirstResult()开始索引号
query.setMaxResult()每页显示个数
创建数据库表
导入所需的jar包
创建对应的类
package cn.hibernate.bean;
public class User {
private Integer userId;//类型必须,整形,名称不建议使用id
private String userName;
private String userPassword;
public User() {
super();
}
public User(Integer userId, String userName, String userPassword) {
super();
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userPassword=" + userPassword + "]";
}
}
对应的映射文件User.hbm.xml
<?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 >
<class name="cn.hibernate.bean.User" table="user">
<!-- 主键 -->
<id name="userId">
<!-- 固定值 -->
<generator class="native"></generator>
</id>
<property name="userName" ></property>
<property name="userPassword"></property>
</class>
</hibernate-mapping>
创建核心配置文件hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/minemysql
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="cn/hibernate/bean/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
创建获取session的工厂
package cn.hibernate.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class H3Utils {
//方式1
//private static SessionFactory factory2 = new Configuration().configure().buildSessionFactory();
//方式2
//创建一个工厂
private static Configuration configuration;
private static SessionFactory factory;
static{
configuration = new Configuration().configure();
factory = configuration.buildSessionFactory();
}
public static Session openSession(){
return factory.openSession();
}
}
编写测试类
package cn.hibernate.test;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import cn.hibernate.bean.User;
public class UserCURD {
@Test
public void demo1(){
//更新
User user = new User();
user.setUserId(1);
user.setUserName("吊炸天");
user.setUserPassword("就是这么叼");
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
session.update(user);
transaction.commit(); //生成sql语句:update user set userName=?, userPassword=? where userId=?
session.close();
factory.close();
}
@Test
public void demo2(){
//删除
User user = new User();
user.setUserId(2);
//4步 获取配置对象 、session工厂、获取session、开启事物
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
//操作
session.delete(user);
//3步 提交 、关闭
transaction.commit();
session.close();
factory.close();
}
@Test
public void demo3(){
//通过id查询
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
User user =(User)session.get(User.class, 1);//必须强制类型转换,get()返回Object类型
System.out.println(user);
transaction.commit();
session.close();
}
@Test
public void demo4(){
//查询所有
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
//需要通过HQL语句查询所有,HQL:表示hibernate查询语句,面向对象的查询语句,类似sql语句。注意:必须使用对象
//session.createQuery("from cn.hibernate.bean.User")
Query query = session.createQuery("from User");
List<User> user = query.list();
for (User user2 : user) {
System.out.println(user2);
}
transaction.commit();
session.close();
}
@Test
public void demo5(){
//分页
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from User");
//确定分页数据 回顾mysql分页:select ... from User limit 0,0
//第一页
//query.setFitstResult(0)
//query.setMaxResults(2)
//第二页
query.setFirstResult(2);
query.setMaxResults(2);
List<User> user = query.list();
for (User user2 : user) {
System.out.println(user2);
}
transaction.commit();
session.close();
}
}