**
什么是ORM ORM(Object/Relational Mapping):对象/关系映射
是通过使用描述对象和数据库之间映射的数据,将java程序中的对象自动持久化到关系数据库中 ,本质上就是将数据从一种形式转换到另外一种形式**
1, 在resource目录下添加hibernate.cfg.xml(核心配置文件),引入hibernate-configuration-3.0.dtd约束(注意mysql版本一致)
2,添加实体类映射文件,引入hibernate-mapping-3.0.dtd约束
1,对框架核心配置文件进行建模
2,获取sessionFactory工厂
3,获取session会话
4,开启事物(查询不需要事物)
5,session *** 作对象
6,提交事物(查询不需要事物) 7,关闭事物
1.pom.xml中添加依赖
**找到图中两个dtd文件并且外部引用** 4.0.0 mjx_id mjx_hibernatewar 0.0.1-SNAPSHOT mjx_hibernate Maven Webapp http://maven.apache.org UTF-8 1.8 1.8 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} mjx_hibernate
添加hibernate.cfg.xml
root 123456 jdbc:mysql://127.0.0.1:3306/?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true com.mysql.cj.jdbc.Driver org.hibernate.dialect.MySQLDialect thread true true
建实体类
package com.mjx.entity; 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 getUsreName() { return userName; } public void setUsreName(String usreName) { this.userName = usreName; } 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 + ", usreName=" + userName + ", userPwd=" + userPwd + ", realName=" + realName + ", sex=" + sex + ", birthday=" + birthday + ", createDatetime=" + createDatetime + ", remark=" + remark + "]"; } }
建实体类对应的映射文件
//类名对应的表 //属性对应的表的列名
CRUD
//查询
package com.mjx.entity; import java.sql.Date; import java.sql.Timestamp; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Text { public static void main(String[] args) { // 1、对框架核心配置文件进行建模 Configuration configure = new Configuration().configure("/hibernate.cfg.xml"); // 2、获取sessionFactory工厂 SessionFactory sessionFactory = configure.buildSessionFactory(); // 3、获取session会话 Session session = sessionFactory.openSession(); // 4、开启事务(查询不需要事务) Transaction transaction = session.beginTransaction(); // 5、session *** 作对象 //增加 //新建对象 User user = new User(); user.setBirthday(new Date(System.currentTimeMillis()));// 生日 user.setCreateDatetime(new Timestamp(System.currentTimeMillis()));// 日期时间 user.setId(12); user.setRealName("小明"); user.setRemark("城市"); user.setSex("男"); user.setUsreName("小小"); user.setUserPwd("123456"); //增加方法 session.save(user); //修改 //User user = session.get(User.class, 12); //user.setRealName("隔壁老陈"); //删除 //方法一: //User user = session.get(User.class, 12); //session.delete(user); //方法二: //User user=new User(); //user.setId(12); //session.delete(user); // 6、提交事务(查询不需要事务) transaction.commit(); // 7、关闭session session.close(); } }
修改删除增加
package com.mjx.entity; import java.sql.Date; import java.sql.Timestamp; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Text { public static void main(String[] args) { // 1、对框架核心配置文件进行建模 Configuration configure = new Configuration().configure("/hibernate.cfg.xml"); // 2、获取sessionFactory工厂 SessionFactory sessionFactory = configure.buildSessionFactory(); // 3、获取session会话 Session session = sessionFactory.openSession(); // 4、开启事务(查询不需要事务) Transaction transaction = session.beginTransaction(); // 5、session *** 作对象 //增加 //新建对象 User user = new User(); user.setBirthday(new Date(System.currentTimeMillis()));// 生日 user.setCreateDatetime(new Timestamp(System.currentTimeMillis()));// 日期时间 user.setId(12); user.setRealName("小明"); user.setRemark("城市"); user.setSex("男"); user.setUserName("小小"); user.setUserPwd("123456"); //增加方法 session.save(user); //修改 //User user = session.get(User.class, 12); //user.setRealName("隔壁老陈"); //删除 //方法一: //User user = session.get(User.class, 12); //session.delete(user); //方法二: //User user=new User(); //user.setId(12); //session.delete(user); // 6、提交事务(查询不需要事务) transaction.commit(); // 7、关闭session session.close(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)