学习hibernate的一个Demo,使用hibernate对Customer类进行单表增删改查,hibernate是ORM对象关系映射技术,可以对JDBC数据库底层操作进行封装,简化开发。
1.环境搭建
官网上下载hibernate的依赖包,hibernate可以再java环境下也可以在web环境下进行开发,我们使用java环境。
2.进行项目配置
2.1建一个customer表,什么表都可以,这里建一个customer表做演示,表中添加一些属性。
2.2根据customer表新建java类。
2.3根据java类,新建对象映射文件,xxx.hbm.xml,配置两者映射关系。
2.4配置核心文件,文件主要配置连接数据库信息和映射表。
2.5.编写测试类,进行测试
3.代码展示
package hibernate;/** * 客户管理的实体类 * @author jt *CREATE TABLE `cst_customer` ( ?`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘, ?`cust_name` varchar(32) NOT NULL COMMENT ‘客户名称(公司名称)‘, ?`cust_source` varchar(32) DEFAULT NULL COMMENT ‘客户信息来源‘, ?`cust_industry` varchar(32) DEFAULT NULL COMMENT ‘客户所属行业‘, ?`cust_level` varchar(32) DEFAULT NULL COMMENT ‘客户级别‘, ?`cust_phone` varchar(64) DEFAULT NULL COMMENT ‘固定电话‘, ?`cust_mobile` varchar(16) DEFAULT NULL COMMENT ‘移动电话‘, ?PRIMARY KEY (`cust_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; */public class Customer { ???private Long cust_id; ???private String cust_name; ???private String cust_source; ???private String cust_industry; ???private String cust_level; ???private String cust_phone; ???private String cust_mobile; ???public Long getCust_id() { ???????return cust_id; ???} ???public void setCust_id(Long cust_id) { ???????this.cust_id = cust_id; ???} ???public String getCust_name() { ???????return cust_name; ???} ???public void setCust_name(String cust_name) { ???????this.cust_name = cust_name; ???} ???public String getCust_source() { ???????return cust_source; ???} ???public void setCust_source(String cust_source) { ???????this.cust_source = cust_source; ???} ???public String getCust_industry() { ???????return cust_industry; ???} ???public void setCust_industry(String cust_industry) { ???????this.cust_industry = cust_industry; ???} ???public String getCust_level() { ???????return cust_level; ???} ???public void setCust_level(String cust_level) { ???????this.cust_level = cust_level; ???} ???public String getCust_phone() { ???????return cust_phone; ???} ???public void setCust_phone(String cust_phone) { ???????this.cust_phone = cust_phone; ???} ???public String getCust_mobile() { ???????return cust_mobile; ???} ???public void setCust_mobile(String cust_mobile) { ???????this.cust_mobile = cust_mobile; ???} ???@Override ???public String toString() { ???????return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source ???????????????+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone ???????????????+ ", cust_mobile=" + cust_mobile + "]"; ???} ???}<?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="hibernate"> ??????<class name="Customer" table="cst_customer"> ???????????<id name="cust_id" column="cust_id"> ????????????????<generator class="native"/> ???????????</id> ???????????<property name="cust_name" column="cust_name"></property> ???????????<property name="cust_source" column="cust_source"></property> ???????????<property name="cust_industry" column="cust_industry"></property> ???????????<property name="cust_level" column="cust_level"></property> ???????????<property name="cust_phone" column="cust_phone"></property> ???????????<property name="cust_mobile" column="cust_mobile"></property> ??????</class> ???</hibernate-mapping>
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ?????????<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/hibernate_day01</property> ?????????<property name="hibernate.connection.username">root</property> ?????????<property name="hibernate.connection.password">root</property> ?????????<property name="hibernate.show_sql">true</property> ?????????<property name="hibernate.format_sql">true</property> ?????????<!-- 自动建表 --> ?????????<property name="hibernate.hbm2ddl.auto">update</property> ?????????<!-- 配置方言 --> ?????????<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> ?????????<!-- 配置映射 --> ?????????<mapping resource="hibernate/customer.hbm.xml"></mapping> ?????</session-factory> ???</hibernate-configuration>
package hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class hibernateDemo1 { ????public static void main(String[] args) { ?????????//1.加载核心配置文件 ????????Configuration configuration = new Configuration().configure(); ????????//2.创建sessionFactory对象 ????????SessionFactory sessionFactory = configuration.buildSessionFactory(); ????????//3.通过sesstionFactory获取session对象 ????????Session session = sessionFactory.openSession(); ????????//4.手动开启事务 ????????Transaction transaction = session.beginTransaction(); ????????//5.编写测试代码 ????????Customer customer = new Customer(); ????????customer.setCust_name("h"); ????????session.save(customer); ????????//6.事务提交 ????????transaction.commit(); ????????//7.资源释放 ????????session.close(); ????????System.out.println("成功了"); ???} ???}4.主要对象
4.1Configuation对象,加载配置文件
4.2SessionFactory对象,封装了一个数据库连接池,二级缓存,线程安全。
4.3Session对象,数据库操作对象,对数据库进行增删改查操作,线程不安全。
4.4Transaction对象,数据库事务操作对象。
5.数据库操作方法
5.1插入操作save
5.2查找操作get(Object.class,id)和load,get立即操作,load延迟操作,get返回具体对象。
5.3删除操作delete。
5.4批量查询Query,可以使用hql和sql两种方式,creatQuery和creatSQLQuery。
5.5saveOrUpdate
6.注意事项
实体类对象的hbm.xml文件的class的类名name可能会包解析文件错误,类名需要在mapping中设置包名,不要在class中的name中带上包名。
hibernate.cfg.xml中的mapping配置的映射名resource用/分开。
7.优化处理
可以封装一个工具类简化开发。
package hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class hibernateUtils { ???????public static final Configuration cfg; ???????public static final SessionFactory sf; ???????????????static { ???????????cfg=new Configuration().configure(); ???????????sf=cfg.buildSessionFactory(); ???????} ???????public static Session openSession() { ???????????return sf.openSession(); ???????}}可以添加log4j.properties文件打印log信息
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c\:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ #### error warn info debug tracelog4j.rootLogger= info, stdouthibernate入门
原文地址:https://www.cnblogs.com/yanqingguo/p/9741396.html