1.hibernate介绍
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库
省略mysql-connect包导入以及hibernate 包导入
2.用户类创建
创建一个类User,各种get set方法
package shiyanlou.test.hibernate.entity;
/**
* Created by junfeng on 17/3/5.
*/
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3.实体映射
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 映射对应的 package -->
<hibernate-mapping package="shiyanlou.test.hibernate.entity">
<!-- 实体类和数据库中的表对应(如果没有这个表则新建) -->
<class name="User" table="user_info">
<!-- id主键 和其他属性对应表中相应的字段(这些都是在 User.java 实体类中定义的) -->
<id name="id" column="user_id"/>
<property name="username" column="user_username"></property>
<property name="password" column="user_password"></property>
</class>
</hibernate-mapping>
4.插入数据
package shiyanlou.test.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import shiyanlou.test.hibernate.entity.User;
public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// 获取 Hibernate 配置信息
Configuration configuration = new Configuration().configure();
// 根据 configuration 建立 sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 开启 session(相当于开启 JDBC 的 connection)
Session session = sessionFactory.openSession();
// 创建并开启事务对象
session.beginTransaction();
// 新建对象,并赋值
User user = new User();
user.setId(3);
user.setUsername("user2");
user.setPassword("111111");
// 保存对象
session.save(user);
// 提交事务
session.getTransaction().commit();
// 关闭 session 和 sessionFactory
session.close();
sessionFactory.close();
}
}
5.查询数据
package shiyanlou.test.hibernate.test;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import shiyanlou.test.hibernate.entity.User;
import java.util.List;
/**
* Created by junfeng on 17/3/5.
*/
public class Select {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
// 利用 StringBuilder 来连接查询语句
StringBuilder hq = new StringBuilder();
// 从 User 里面查找(注意 from 后有空格)
// 相当于 "select * from user_info;"
hq.append("from ").append( User.class.getName() );
// 利用 session 建立 query
Query query = session.createQuery( hq.toString() );
// 序列化 query 的结果为一个 list 集合
List<User> users = query.list();
// 打印每一个 User 信息(这里只打印了名字,你也可以打印其他信息)
for (User user : users) {
System.out.println( user.getUsername() );
}
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
6.更新数据
package shiyanlou.test.hibernate.test;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import shiyanlou.test.hibernate.entity.User;
import java.util.List;
/**
* Created by junfeng on 17/3/5.
*/
public class Update {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
StringBuilder hq = new StringBuilder();
// 对比查找的操作来看,因为我们需要修改指定 name 的用户密码,后面需要再添加查询条件
// 注意 from、where 的空格,":name" 表示一个参数
hq.append("from ").append(User.class.getName()).append(" where user_username=:name");
Query query = session.createQuery(hq.toString());
// 这里就设定参数 name 的值为"user1"
query.setString("name", "user1");
List<User> users = query.list();
for (User user : users) {
// 修改 user1 的密码
user.setPassword("123-user");
// 注意这里是 update
session.update(user);
}
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
7.删除数据
package shiyanlou.test.hibernate.test;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import shiyanlou.test.hibernate.entity.User;
import java.util.List;
/**
* Created by junfeng on 17/3/5.
*/
public class Delete {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
StringBuilder hq = new StringBuilder();
// 对比查找时候的操作来看,因为我们需要修改指定 name 的用户密码,后面需要再添加查询条件
// 注意 from、where 的空格,":name" 表示一个参数
hq.append("from ").append(User.class.getName()).append(" where user_username=:name");
Query query = session.createQuery(hq.toString());
// 这里就设定参数 name 的值为"user1"
query.setString("name","user1" );
List<User> users = query.list();
for (User user : users) {
// 注意这里是 delete
session.delete(user);
}
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}