初识hibernate小案例
使用hibernate前需要导入相关JAR包。
1.它可以接受词文法语言描述,并能产生识别这些语言的语句的程序
2.是一个Java的XML API,类似于jdom,用来读写XML文件的
3.支持注解配置
4.核心配置
5.jpa注解是hibernate的核心
6.编辑和创建Java字节码的类库
7.使用一个注解处理器来实现你的带注释的接口的具体类
8. 解释不清。。
9.单元测试
10.mysql驱动jar
1.src下创建hibernate.cfg.xml文件,提供数据库信息
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/demo</property> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--显示sql语句--> <property name="show_sql">true</property> <!--格式化sql语句--> <property name="format_sql">true</property> <!--数据库中创建表结构--> <property name="hbm2ddl.auto">update</property> <!--映射表--> <mapping resource="Student.hbm.xml"/> </session-factory> </hibernate-configuration>
2.创建实体类Student.java,遵循javabean规范。
package com.test; import java.sql.Blob; import java.util.Date; /** * Created by Wdnncey on 2016/10/14. */ public class Student { private int sid; private String sname; private String sex; private String address; private Date birthday; private Blob picture; public Student(int sid, String sname, String sex, String address, Date birthday, Blob picture) { this.sid = sid; this.sname = sname; this.sex = sex; this.address = address; this.birthday = birthday; this.picture = picture; } @Override public String toString() { return "Student{" + "sid=" + sid + ", sname='" + sname + '\'' + ", sex='" + sex + '\'' + ", address='" + address + '\'' + ", birthday=" + birthday + ", picture=" + picture + '}'; } public Student(int sid) { this.sid = sid; } public Blob getPicture() { return picture; } public void setPicture(Blob picture) { this.picture = picture; } public Student() { } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
3.通过实体类映射xml文件,创建Student.hbm.xml,这个xml文件是对应数据库表的关系配置,里面的属性对应着相应字段!
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.test.Student" table="student"> <id name="sid" type="int"> <column name="SID"/> <generator class="assigned"/> </id> <property name="sname" type="java.lang.String"> <column name="SNAME"/> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS"/> </property> <property name="sex" type="java.lang.String"> <column name="SEX"/> </property> <property name="birthday" type="java.util.Date"> <column name="BIRTHDAY"/> </property> <property name="picture" type="java.sql.Blob"> <column name="PICTURE" /> </property> </class> </hibernate-mapping>
4.将Student.hbm.xml文件映射,添加到Hibernate.cfg.xml中!(上面的配置中已经存在)
<mapping resource="Student.hbm.xml"/>
5.创建Test类(在执行测试方法前,需要手动创建数据库,表会由Hibernate自动创建)
public class StudentTtest { //初始化 private SessionFactory sessionFactory; private Session session; private Transaction transaction; //在执行Test之前执行(Before)的注解。 @Before public void init(){ //创建配置对象 Configuration configuration = new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); //创建会话工厂对象 sessionFactory = configuration.buildSessionFactory(serviceRegistry); //会话对象 session =sessionFactory.openSession(); //开启事务对象 transaction = session.beginTransaction(); } @Test public void testStudent(){ Student student = new Student(); student.setSname("Demo"); student.setAddress("china"); student.setBirthday(new Date()); student.setSex("男"); //通过sessin执行保存方法! session.save(student); } //在执行完Test注解后之情(After)注解。 @After public void destory(){ transaction.commit();//提交事务 session.close();//关闭会话 sessionFactory.close();//关闭会话工厂 }
成功信息:
查询记录:
//get、load,都可以用来查询数据使用。 @Test public void testGetStudent(){ //查询主键记录为“1”的数据 Student student = (Student) session.get(Student.class,1); System.out.println(student); } @Test public void testLoadStduent(){ //查询主键记录为“1”的数据 Student student = (Student) session.load(Student.class,1); System.out.println(student); }
成功信息:
修改记录:
@Test public void testUpdateStudent(){ //查询主键记录为“1”的数据,并重新设置名称 Student student = (Student) session.get(Student.class,1); student.setSname("Update后"); }
成功信息:
删除记录:
@Test public void testDeleteStudent(){ //查询主键记录为“1”的数据,并且删除 Student student = (Student) session.get(Student.class,1); session.delete(student); }
成功信息:
以上就是hibernate的CRUD操作。
get和load都可以查询数据,他们的根本区别,一句话,hibernate对于load方法认为该数据在数据库中一定存在,可以放心的使用代理来延迟加载,如果在使用过程中发现了问题,只能抛异常;而对于get方法,hibernate一定要获取到真实的数据,否则返回null。
你配不上自己的野心 也辜负了所受的苦难