Hibernate入门
来源:互联网 发布:北交大知行pt网址 编辑:程序博客网 时间:2024/05/29 06:45
1。导包:
2。实体类:Person.java文件:
package com.itheima.domain;import java.io.Serializable;/*create database day22;use day22;create table PERSONS( ID int primary key, NAME varchar(255)); */public class Person implements Serializable { private Integer id; private String name; public Person() { super(); } public Person(Integer id, String name) { super(); this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
3。映射文件:Person.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 package="com.itheima.domain"> <class name="Person" table="PERSONS"> <id name="id" column="ID"> <generator class="native"></generator> </id> <property name="name" column="NAME"></property> </class></hibernate-mapping>
4。Hibernate配置文件,hibernate.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> <!-- 配置Hibernate:属性配置参考 Hibernate发型包\project\etc\hibernate.properties --> <!-- JDBC的基本链接 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">admin</property> <property name="connection.url">jdbc:mysql://localhost:3306/day22</property> <!-- 配置数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 根据映射产生表结构的类型: create-drop:木有表结构创建,下次启动时删除重新创建。适合学习阶段 create:只做创建 update:探测表结构够的变化,对于数据库没有的,进行更新操作。适合学习阶段 validate:对比与数据库的结构 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 自动开启事务 --><!-- <property name="hibernate.connection.autocommit">true</property> --> <!-- 显示sql语句及格式:开发调试阶段非常有用 --> <property name="hibernate.show_sql">true</property> <!-- 告知映射文件 --> <mapping resource="com/itheima/domain/Person.hbm.xml"/> </session-factory></hibernate-configuration>
5。获取session对象的工具类,HibernateUtil.java文件:
package com.itheima.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil { private static SessionFactory sessionFactory; static { Configuration cfg = new Configuration().configure(); sessionFactory = cfg.buildSessionFactory(); } public static Session getSession(){ return sessionFactory.openSession(); }}
6。测试类,CRUDDemo.java文件:
package com.itheima.test;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import com.itheima.domain.Person;import com.itheima.util.HibernateUtil;public class CRUDDemo { @Test public void testAdd(){ Person p = new Person(); p.setId(1); p.setName("杨洋"); Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); s.save(p); tx.commit(); s.close(); } @Test public void testFind(){ Session s = HibernateUtil.getSession(); Person p = (Person) s.get(Person.class, 1); System.out.println(p); s.close(); } @Test public void testUpdate(){ Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); Person p = (Person) s.get(Person.class, 1); p.setName("范青霞"); s.update(p); tx.commit(); s.close(); } @Test public void testDelete(){ Session s = HibernateUtil.getSession(); Transaction tx = s.beginTransaction(); Person p = (Person) s.get(Person.class, 1); s.delete(p); tx.commit(); s.close(); }}
7。测试结果:
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate: insert into PERSONS (NAME) values (?)
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate: select person0_.ID as ID0_0_, person0_.NAME as NAME0_0_ from PERSONS person0_ where person0_.ID=?com.itheima.domain.Person@16c9b8c
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate: select person0_.ID as ID0_0_, person0_.NAME as NAME0_0_ from PERSONS person0_ where person0_.ID=?Hibernate: update PERSONS set NAME=? where ID=?
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.Hibernate: select person0_.ID as ID0_0_, person0_.NAME as NAME0_0_ from PERSONS person0_ where person0_.ID=?Hibernate: delete from PERSONS where ID=?
0 0
- Hibernate 入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate 入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- Hibernate入门
- hibernate 入门
- Hibernate入门
- hibernate入门
- 从零开始学习SLAM
- HDU 1004
- 课后作业代码参考
- 数据结构总结
- 重装系统后,桌面只有孤零零的回收箱,该怎么解决?
- Hibernate入门
- 管程的特点
- Mac 批量删除 .svn 文件
- MySQL server has gone away 问题的解决方法
- HDU 1005
- const的用法
- android_01_电话拨号器
- BZOJ 3450 Tyvj1952 Easy
- 51nod-1478 括号序列的最长合法子段