分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

hibernate5小案例讲解

发布时间:2023-09-06 01:10责任编辑:傅花花关键词:暂无标签

Student类

package com.wangning.bean;public class Student {private Integer id;private String name;private int age;private double score;@Overridepublic String toString() { ???return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" ???????????+ score + "]";}public Student() { ???super();}public Student(String name, int age, double score) { ???super(); ???this.name = name; ???this.age = age; ???this.score = score;}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;}public int getAge() { ???return age;}public void setAge(int age) { ???this.age = age;}public double getScore() { ???return score;}public void setScore(double score) { ???this.score = score;}}
View Code

Student.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.wangning.bean"> ???<!-- 映射文件的作用: ???????????1、完成类到表的映射 2、属性到字段的映射 --> ???<class name="Student" table="t_student"> ???????<id name="id" column="tid"> ???????????<generator class="native"></generator> ???????</id> ???????<property name="name" column="tname"></property> ???????<property name="age" column="tage"></property> ???????<property name="score" column="tscore"></property> ???</class></hibernate-mapping>

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> ???????<!-- DB链接4要素 --> ???????<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> ???????<property name="hibernate.connection.url">jdbc:mysql:///world</property> ???????<property name="hibernate.connection.username">root</property> ???????<property name="hibernate.connection.password">huadiyatou1314</property> ???????<!-- 方言 --> ???????<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> ???????<!-- 数据源 :数据连接池--> ???????<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> ???????<!-- 当前session上下文:保证统一线程获取到的session是同一个--> ???????<property name="hibernate.current_session_context_class">thread</property> ???????<!-- 自动建表 --> ???????<property name="hibernate.hbm2ddl.auto">update</property> ???????????????<!-- 显示SQL --> ???????<property name="hibernate.show_sql">true</property> ???????<!-- 格式化SQL --> ???????<property name="hibernate.format_sql">true</property> ???????????????<mapping resource="com/wangning/bean/Student.hbm.xml"/> ???</session-factory></hibernate-configuration>

分析:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>

DTD----->这段代码可以从        

hibernate-core-5.1.9.Final.jar/org/hibernate/hibernate-configuration-3.0.dtd

路径下copy。同理映射文件中的dtd也相应加入。

package test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.wangning.bean.Student;public class Test1 {@Test ???public ?void mytest() { ???????//加载主配置文件 ???????Configuration config=new Configuration().configure(); ???????//创建Session工厂 ???????SessionFactory sessionFactory=config.buildSessionFactory(); ???????//获取Session ???????Session session=sessionFactory.getCurrentSession(); ???????session.getTransaction().begin(); ???????//执行操作 ???????????????Student student =new Student("王五",21,100); ???????session.save(student); ???????session.getTransaction().commit(); ???}}


1、默认名字:hibernate.cfg.xml 是因为 new Configuration().configure()方法。如下定义:

 ???public static final String DEFAULT_CFG_RESOURCE_NAME = "hibernate.cfg.xml"; ???public Configuration configure() throws HibernateException { ???????return configure( StandardServiceRegistryBuilder.DEFAULT_CFG_RESOURCE_NAME ); ???}

我们也可以更换名字为 wangwu.cfg.xml。只需在方法调用时 变更为

//加载主配置文件 ???????Configuration config=new Configuration().configure("wangwu.cfg.xml");

hibernate5小案例讲解

原文地址:http://www.cnblogs.com/huadiyatou/p/7512811.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved