Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的JavaEE架构中取代CMP,完成数据持久化的重任。
导入hibernate包
项目结构如下:
右击src选择新建,选择其他,输入hibernate,点击带有cfg.xml的文件,点击完成
hibenate.cfg.xml代码如下:
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">root</property> <!-- jdbc:mysql://localhost:端口号/数据库名 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mycsdb</property> <property name="hibernate.connection.username">root</property> <!-- MySQL5InnoDB为数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="pojo/Authors.hbm.xml"/> <mapping class="pojo.Books"/> </session-factory> </hibernate-configuration>
创建两个java类:
Authors.java类:
package pojo; import java.util.ArrayList; import java.util.List; public class Authors { //write //pojo中需要写属性,getter和setter toString()都可以自己生成 private int authorid; private String authorname; private List<Books> list = new ArrayList(); public Authors() {} //auto public Authors(int authorid, String authorname, List<Books> list) { super(); this.authorid = authorid; this.authorname = authorname; this.list = list; } public List<Books> getList() { return list; } public void setList(List<Books> list) { this.list = list; } public int getAuthorid() { return authorid; } public void setAuthorid(int authorid) { this.authorid = authorid; } public String getAuthorname() { return authorname; } public void setAuthorname(String authorname) { this.authorname = authorname; } @Override public String toString() { return "Authors [authorid=" + authorid + ", authorname=" + authorname + ", list=" + list + "]"; } }
Books.java类:
package pojo; public class Books { private int bookid; private String bookname; public Books() {}; public Books(int bookid,String boookame){ super(); this.bookid = bookid; this.bookname = bookname; } public int getBookid() { return bookid; } public void setBookid(int bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } @Override public String toString() { return "Books [bookid=" + bookid + ", bookname=" + bookname + "]"; } }
在src中创建test包,在包中创建TestList类:
package test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import pojo.Authors; import pojo.Books; import util.HibernateUtil; public class TestList { @Test public void testOne() { //1.获取会话Session Session session = HibernateUtil.getSessionFactory().openSession(); // Session session1 = HibernateUtil.getSession(); //2.开启事务 Transaction tx = session.beginTransaction(); //3.添加数据 Books book1 = new Books();// book1.setBookname("白夜行"); book1.setBookid(1); Books book2 = new Books(); book2.setBookname("解忧杂货店"); book2.setBookid(2); Authors author = new Authors();//一 author.setAuthorname("东野圭吾"); //4.关联 author.getList().add(book1); author.getList().add(book2); //5.保存数据 session.save(author); session.save(book1); session.save(book2); //6.事务提交 tx.commit(); //7.关闭资源 session.close(); } @Test public void testQuery() { Session session = HibernateUtil.getSessionFactory().openSession(); Query query = session.createQuery("From pojo.Authors"); List list = query.list(); for(Iterator itr = list.iterator();itr.hasNext();) { Authors a = (Authors) itr.next(); //借助pojo中的toString()方法打印内容 System.out.print(a.toString()); } } }
在src中创建util类,创建HibernateUtil工具类:
package util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { static SessionFactory sf; static { sf = new Configuration().configure() .buildSessionFactory(); } public static SessionFactory getSessionFactory() { return sf; } public static Session getSession() { return sf.openSession(); } }
将Authors和Books合并:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2021-9-22 21:47:19 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="pojo.Authors" table="AUTHORS"> <id name="authorid" type="int"> <column name="AUTHORID" /> <generator class="assigned" /> </id> <property name="authorname" type="java.lang.String"> <column name="AUTHORNAME" /> </property> <list name="list" cascade="all"> <key column="AUTHORID"/> <list-index column="BOOKID"></list-index> <one-to-many class="pojo.Books" /> </list> </class> <class name ="pojo.Books" table= "BOOKS"> <<id name="bookid" type ="int"> <column name ="BOOKID"/> <generator class="assigned"/> </id> <property name="bookname" type="java.lang.String"> <column name="BOOKNAME"></column> </property> </class> </hibernate-mapping>
打开Navicat查看表:
以上是Hibernate自动向数据库插入数据,如有错误请改正。