hibernate 开发步骤

    xiaoxiao2021-03-25  94

    一、创建hibernate配置文件

          hibernate.cfg.xml

         基本都是这样   直接复制拿去用就好了

    <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--链接数据库的基本信息--> <property name="connection.url">jdbc:mysql://localhost:3306/pipi</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 配置hibernate的基本信息 --> <!-- 配置数据库方言dialect --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 操作时是否在控制台打印sql语句 --> <property name="show_sql">true</property> <!-- 是否对sql进行格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- DB schema will be updated if needed --> <!-- <property name="hbm2ddl.auto">update</property> --> </session-factory> </hibernate-configuration>

    二、创建持久化类persistent object

    public class Namelist { private String id; private String name; public Namelist(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Namelist namelist = (Namelist) o; if (id != null ? !id.equals(namelist.id) : namelist.id != null) return false; if (name != null ? !name.equals(namelist.name) : namelist.name != null) return false; return true; } @Override public int hashCode() { int result = id != null ? id.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); return result; } }   

    三、创建对象-关系映射文件  *.hbm.xml

    <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.pipi.hibernateDemo.Namelist" table="namelist" schema="pipi"> <id name="id"> <column name="id" sql-type="varchar(30)" length="30"/> </id> <property name="name"> <column name="name" sql-type="varchar(30)" length="30" not-null="true"/> </property> </class> </hibernate-mapping>

    四、通过hibernate api编写访问数据库的代码

    写一个测试类

    import com.pipi.hibernateDemo.Namelist; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.junit.Test; /** * Created by 1 on 2017/3/22. */ public class HibernateTest { @Test public void test(){ //1、创建SessionFactory对象 SessionFactory sessionFactory=null; //创建Configuration对象:对应hibernate的基本配置信息和对象映射关系 Configuration configuration = new Configuration().configure(); //创建一个ServiceRegistry对象:4.x以后新添加的对象 // hibernate的任何服务和配置都需要在该对象注册后生效 StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build(); // 这个已经不用了 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); //创建sessionFactory sessionFactory =configuration.buildSessionFactory(standardRegistry); //2、创建一个session对象 Session session = sessionFactory.openSession(); //3、开启事务 Transaction transaction = session.beginTransaction(); //4、执行保存操作 Namelist namelist=new Namelist("0","pipi"); session.save(namelist); //5、提交事务 transaction.commit(); //6、关闭session session.close(); //7、关闭SessionFactory对象 sessionFactory.close(); } }

    如果是利用IntelliJ idea的话,连接数据库以后,二和三都是自动生成的,只需稍微改动即可。

    本皮皮实在太懒咯就不上图了

     

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

    最新回复(0)