简要的说一下hibernate,它是一个数据持久层的框架,用来让程序员简化问题的考虑方面,不再去考虑数据如何获取和存入,将更多的注意力集中在对数据的操作,前面的博文讲解过模拟hibernate的实现,这篇来讲解如何配置hibernate框架。
首先,在src下应该有一个hibernate.cfg.xml的XML文件,它里面是配置映射关系,用来找到model中的xml,也可以在这个xml里面配置connection,以及选择数据库的方言和显示SQL语句等,代码如下:
<!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> <!-- 通过XML配置,细节是'&'需要转义符,且以';'结尾 <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mec_mysql ? characterEncoding=utf8 & useSSL=false & serverTimezone=Asia/Shanghai & rewriteBatchedStatements=true</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 这四个property是配置数据库连接,也可以写到hibernate.properties文件里 注意这个properties文件的名字必须为hibernate.properties,不然会报错 --> <property name="show_sql">true</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> --> <!--这四个property分别为显示SQL语言,以及采用mysql的方言,但这些配置为非必须参数配置--> <mapping resource ="com/mec/hibernate/model/country.hbm.xml"/> <!--这个映射关系是让找到每个model分别配置的XML--> </session-factory> </hibernate-configuration>这个是model类:
package com.mec.hibernate.model; public class CountryModel { private String number; private String country; public CountryModel() { } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return "CountryModel [number=" + number + ", country=" + country + "]"; } }这个XML就是之前的配置的mapping中的映射XML,它其中配置的是model类与表的的对应,以及类中的成员与表中字段的对应。
<?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 package="com.mec.hibernate.model"> <class name="CountryModel" table="country"> <id name="number" ></id> <property name="country" ></property> </class> </hibernate-mapping>这个是测试类:
package com.mec.hibernate.test; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import com.mec.hibernate.model.CountryModel; public class Test { public static void main(String[] args) { Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session s = sf.openSession(); //除了查询操作,其他事务需要开启业务 Transaction ta = s.beginTransaction(); CountryModel cm = new CountryModel(); cm.setCountry("老挝"); cm.setNumber("21"); s.save(cm); ta.commit(); //一定要提交事务! System.out.println("成功"); @SuppressWarnings("unchecked") Query<CountryModel> query = s.createQuery("FROM com.mec.hibernate.model.CountryModel"); //注意这里的FROM之后必须是带包名的类名,而不是表名 List<CountryModel> countrys = query.getResultList(); for (CountryModel country : countrys) { System.out.println(country); } s.close(); //最后一定要关闭session }下面是测试结果: