目录
一、概念
二、主键生成策略
①程序员自己控制:assigned
② 数据库控制: identity(标识列/自动增长) sequence
③ hibernate控制:increment uuid/uuid.hex
④ 其它:native
三、使用
①在pom.xml中导入Hibernate相关依赖
②配置hibernate.cfg.xml文件
③创建实体类,配置映射文件
④测试
一、概念
Hibernate是持久层的ORM框架,我们可以通过 *** 作java对象来 *** 作我们的数据库数据。
什么是ORM?
Object Relation Mapping(对象关系映射),利用XML文件建立实体类和数据库表之间建立一种映
射关系,实现 *** 作对象控制数据!提高开发人员访问数据层的编程效率。
二、主键生成策略通过 *** 作对象来控制数据,那么在该表中添加数据它的ID(主键)怎么控制呢?
generator元素:表示了一个主键生成器,它用来为持久化类实例生成唯一的标识 。
①程序员自己控制:assigned ② 数据库控制: identity(标识列/自动增长) sequence ③ hibernate控制:increment uuid/uuid.hex ④ 其它:native在实体类与表建立映射关系的XML配置文件中设置!
三、使用 ①在pom.xml中导入Hibernate相关依赖②配置hibernate.cfg.xml文件4.0.0 com.zwf Maven_hibwar 0.0.1-SNAPSHOT Maven_hib Maven Webapp http://maven.apache.org 4.12 4.0.0 5.3.0.Final 8.0.19 junit junit${junit.version} test javax.servlet javax.servlet-api${servlet.version} provided org.hibernate hibernate-core${hibernate.version} mysql mysql-connector-java${mysql.driver.version} Maven_hib
③创建实体类,配置映射文件root root123 jdbc:mysql://127.0.0.1:3306/zwf?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true com.mysql.cj.jdbc.Driver org.hibernate.dialect.MySQLDialect thread true true
name和实体类属性名对应,column和数据库列名对应!!!
实体类:User.java
package com.zwf.po; import java.sql.Date; import java.sql.Timestamp; public class User { private int id; private String userName; private String userPwd; private String realName; private String sex; private Date birthday; private Timestamp createDatetime; private String remark; 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 getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Timestamp getCreateDatetime() { return createDatetime; } public void setCreateDatetime(Timestamp createDatetime) { this.createDatetime = createDatetime; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName + ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark=" + remark + "]"; } }④测试
1、增加
//1.对框架核心配置文件进行建模 Configuration configure = new Configuration().configure("/hibernate.cfg.xml"); //2.获取sessionFactory工厂 SessionFactory buildSessionFactory = configure.buildSessionFactory(); //3.获取session会话 Session openSession = buildSessionFactory.openSession(); //4.开启事务 Transaction beginTransaction = openSession.beginTransaction(); //5.session *** 作对象 User user=new User(); user.setId(18); user.setBirthday(new Date(System.currentTimeMillis())); user.setUserName("张三"); openSession.save(user); // 6.提交事务 beginTransaction.commit(); //7.关闭session openSession.close();
2、删除
//1.对框架核心配置文件进行建模 Configuration configure = new Configuration().configure("/hibernate.cfg.xml"); //2.获取sessionFactory工厂 SessionFactory buildSessionFactory = configure.buildSessionFactory(); //3.获取session会话 Session openSession = buildSessionFactory.openSession(); //4.开启事务 Transaction beginTransaction = openSession.beginTransaction(); //5.session *** 作对象 //方式一 User user = openSession.get(User.class, 12);//查找对应数据 openSession.delete(user); //方式二 //User user = new User(); //user.setId(12); //openSession.delete(user); // 6.提交事务 beginTransaction.commit(); //7.关闭session openSession.close();
通过我们传入的对象(具备id属性)删除对应数据
3、修改
//1.对框架核心配置文件进行建模 Configuration configure = new Configuration().configure("/hibernate.cfg.xml"); //2.获取sessionFactory工厂 SessionFactory buildSessionFactory = configure.buildSessionFactory(); //3.获取session会话 Session openSession = buildSessionFactory.openSession(); //4.开启事务 Transaction beginTransaction = openSession.beginTransaction(); //5.session *** 作对象 User user = openSession.get(User.class, 14); user.setUserName("cc");//修改name openSession.update(user); // 6.提交事务 beginTransaction.commit(); //7.关闭session openSession.close();
获得要修改的对象,然后修改属性提交事务完成对数据的修改!
4、查询(不需要事务)
//1.对框架核心配置文件进行建模 Configuration configure = new Configuration().configure("/hibernate.cfg.xml"); //2.获取sessionFactory工厂 SessionFactory buildSessionFactory = configure.buildSessionFactory(); //3.获取session会话 Session openSession = buildSessionFactory.openSession(); //4.session *** 作对象 List list = openSession.createQuery(" from User ").list(); for (Object object : list) { System.out.println(object); } //7.关闭session openSession.close();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)