Hibernate框架使用步骤

    xiaoxiao2021-03-25  135

    Hibernate是一个开源的ORM(Object Relation Mapping,对象关系映射)持久层(dao层)框架。

    框架使用步骤:

    1.导入使用Hibernate框架需要用到的常用包,如下图,最下面mysql-connector-java-5.1.18-bin.jar包不属于Hibernate框架中的包,是属于另引入的jar包,用于连接数据库执行sql语句的jar包。

    2.编写po(持久化对象)

    (1)编写实体类(这里以Users为例)如下:

    package com.study.po; public class Users { private Integer id; private String username; private String password; public Users() { // TODO Auto-generated constructor stub } public Users(Integer id,String username,String password) { this.id = id; this.username = username; this.password = password; } public Integer getId() { return id; } public void setId(Integer 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; } }(2)配置映射文件Users.hbm.xml(.hbm.xml文件需要在下载的Hibernate框架中去查找,任意一个以.hbm.xml为后缀的都可以),配置如下:

    <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.study.po"> <!-- name表示对于实体类的地址,table表示生成数据库表的表名 --> <class name="com.study.po.Users" table="users"> <!-- 主键 --> <id name="id" column="id"> <!-- 生成主键的方式:自动生成,自动增长 --> <generator class="native"> </generator> </id> <!-- property中name表示实体类中对应属性,column表示数据库表中对应字段 --> <property name="username" column="usernames" type="string" not-null="true"/> <property name="password" column="passwords" type="string" not-null="true"/> </class> </hibernate-mapping>3.配置hibernate.cfg.xml注册文件,配置如下:

    <!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="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> jdbc:mysql://localhost:3306/twodb </property> <property name="connection.username"> root </property> <property name="connection.password"> root </property> <!-- 在控制台显示sql语句 --> <property name="show_sql">true</property> <!-- 自动生成或更新数据库表 --> <property name="hbm2ddl.auto">update</property> <!-- 注册映射文件 --> <mapping resource="com/study/po/Users.hbm.xml" /> </session-factory> </hibernate-configuration>4.创建Configuration对象,加载注册文件,创建SessionFactory从而打开session,通过session操作数据库最终实现dao层的功能,测试代码如下:

    package com.study.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import com.study.po.Users; public class Test { public static void main(String[] args) { Users user = new Users(1, "zhangsan", "zs123"); //创建Configuration对象 Configuration cfg = new Configuration(); //加载注册文件 cfg.configure("hibernate.cfg.xml"); //创建SessionFactory StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); builder.applySettings(cfg.getProperties()); SessionFactory factory = cfg.buildSessionFactory(builder.build()); //打开session Session session = factory.openSession(); //开始事务 Transaction trans = session.beginTransaction(); //操作 session.save(user);//执行插入数据 trans.commit(); session.close(); } }

    转载请注明原文地址: https://ju.6miu.com/read-3514.html

    最新回复(0)