首页 > 代码库 > Hibernate ORM框架——第一章:Hibernate简介与操作基础
Hibernate ORM框架——第一章:Hibernate简介与操作基础
一、相关使用工具的下载与导入(环境配置)
hibernate-release-4.2.21.Final-->旧版本
hibernate-release-5.2.10.Final-->新版本
首先需要解压:hibernate-release-5.2.10.Final(新版本)
-->把解压后的 hibernate-release-5.2.10.Final文件夹里的 lib文件夹里的所有jar包复制到根项目下的lib文件夹里,包括JDBC的ojdbc6.jar包
-->把所有的jar包变成牛奶瓶
解压:hibernate-tutorials文件:可以从这个文件中找到:basic--一直点进去
直到看到java和resources的文件,再依据需求来选择要那个文件里面的xml头文件
-->hibernate.cfg.xml(配置文件的头文件)
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
-->EmpMapping.xml(映射文件的头文件)
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
二、第一个Hibernate添加操作
1)使用PL/SQL数据库
2)第一个类比原生的JDBC实现添加操作:(不需要封装util包)
注意:此代码是必须先在数据库中创建所需要的表
2.1)类比jave原生的JDBC实现添加操作
1.1 util包-->DbUtils.java:连接数据库驱动
package utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DbUtils { private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl"; private static final String username = "myuser"; private static final String password = "123"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } public static void closeConnection(Connection conn) throws SQLException { conn.close(); } }
1.2 写实体对象
package entity; public class TestEntity { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
1.3 写dao包
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import entity.TestEntity; import utils.DbUtils; public class TestDao { public void insert(String name) throws SQLException{ Connection conn = DbUtils.getConnection(); String sql = "insert into mytable values(?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, name); ps.executeUpdate(); } public List<TestEntity> getAll() throws Exception { List<TestEntity> result = new ArrayList<TestEntity>(); Connection conn = DbUtils.getConnection(); String sql = "select * from mytable"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while(rs.next()){ TestEntity entity = new TestEntity(); entity.setName(rs.getString(1)); result.add(entity); } return result; } }
1.4 写test测试包
package yuanMain; import java.util.List; import dao.TestDao; import entity.TestEntity; public class YuanMain { public static void main(String[] args) throws Exception { /*仿照以前JDBC添加和查询数据库的操作*/ /*添加*/ TestDao testdao = new TestDao(); //testdao.insert("xxttt"); /*查询*/ List<TestEntity> all = testdao.getAll(); System.out.println(all.size()); for (TestEntity testEntity : all) { System.out.println(testEntity.getName()); } } }
3)第一个hibernate实现添加操作代码(优化上面的类比java的代码)
3.1 -->先把解压后的 hibernate-release-5.2.10.Final文件夹里的 lib文件夹里的所有jar包复制到根项目下的lib文件夹里,包括JDBC的ojdbc6.jar包
-->把所有的jar包变成牛奶瓶
项目结构:
3.2 写代码:
配置文件
<?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="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">myuser</property> <property name="connection.password">123</property> <!-- 在添加数据时可选hbm2ddl.auto。在查询时,可以不用:避免唯一约束的冲突 --> <!-- 根据schema在PL/SQL中自动创建数据表的工具 --> <!-- <property name="hbm2ddl.auto">create</property> --> <!-- 生成优化的SQL --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 显示hibernate生成的sql语句 --> <property name="show_sql">true</property> <!-- 显示格式化的SQL语句:优化排版 --> <property name="format_sql">true</property> <!-- 数据表映射配置文件 --> <mapping resource="com/nf/Personer.xml"></mapping> </session-factory> </hibernate-configuration>
实体对象:
package com.nf; public class Personer { private long pid; private String pname; public long getPid() { return pid; } public void setPid(long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } }
实体映射文件
<?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-mapping package="com.nf"> <class name="Personer" table="Personers"> <id name="pid" column="id"> <!-- 标识主键:increment/native自动增长、assigned自己输入 --> <generator class="increment"></generator> </id> <property name="pname"></property> </class> </hibernate-mapping>
测试类
package com.nf; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class Main { public static void main(String[] args) { StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure().build(); SessionFactory sf = new MetadataSources(registry) .buildMetadata().buildSessionFactory(); Session s = sf.openSession(); Personer ps = new Personer(); ps.setPname("xxx"); /*ps.setPname("试试");*/ Transaction ts = s.beginTransaction(); s.save(ps); ts.commit(); s.close(); sf.close(); } }
/*以上个人整理笔记,如果有误或者有不懂的地方,欢迎评论与指出*/
Hibernate ORM框架——第一章:Hibernate简介与操作基础