hibernate框架简单配置

hibernates是
Hibernate ORM致力于帮助您的应用程序实现持久性。什么是持久性?持久性只是意味着我们希望应用程序的数据比应用程序进程更长久。在Java术语中,我们希望(某些)对象的状态超出JVM的范围,以便稍后可以使用相同的状态

Hibernates核心配置

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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.dialect">org.hibernate.dialect.OracleDialect</property>
         <!--Oracle数据库驱动-->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <!--数据库用户名-->
        <property name="hibernate.connection.username">system</property>
        <!--数据库地址-->
        <property name="hibernate.connection.password">XXX</property>
        <!--数据库连接地址-->
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        
        <!-- 若取值为:create-drop时:
		当我们执行Hibernate程序时,默认会依照实体类结构建立或者删除表结构 --> 
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <!-- 执行Session范围以及上下文由当前线程监控管理 -->
        <property name="hibernate.current_session_context_class">thread</property>
        <!-- 在执行hibernate程序时是否在控制台输出执行的sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 在控制台是否输出格式化的SQL语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 引入映射文件 -->
        <mapping resource="pojo/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

user实体类

package pojo;

public class User {
	/* 用户id */
	private Integer userId;
	/* 用户姓名 */
	private String userName;
	/* 用户密码 */
	private String userPwd;

	/**
	 * @return the userId
	 */
	public Integer getUserId() {
		return userId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the userPwd
	 */
	public String getUserPwd() {
		return userPwd;
	}

	/**
	 * @param userPwd
	 *            the userPwd to set
	 */
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public User(Integer userId, String userName, String userPwd) {
		super();
		this.userId = userId;
		this.userName = userName;
		this.userPwd = userPwd;
	}

	public User(String userName, String userPwd) {
		super();
		this.userName = userName;
		this.userPwd = userPwd;
	}

	public User() {
		super();
	}

}

pojo实体类映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
        SYSTEM
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="pojo">
	<class name="User" table="SHILIAN1">
		<!-- 设置主键的关联映射 -->
		<id name="userId" column="USERID" type="integer">
			<!-- 设置的是主键值生成策略,有如下几种策略 -->
			<!-- native:根据底层数据库的判断自行生成主键值 assigned:主键由我们手动给定 increment:对类型为long,short,int主键以自增长的方式生成主键值 
				identity:对于支持标识列的数据库:比如Sql Server,MySQL等利用标识列生成主键值 sequence:对于支持序列的数据库:比如Oracle,利用序列生成主键值 -->
			<generator class="native" />
		</id>
		<property name="userName" column="USERNAME" type="string"/>
		<property name="userPwd" column="USERPWD" type="string"></property>
	</class>
</hibernate-mapping>

hibernate工具类

package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

//Hibernate工具类,用户管理Session的创建
public class HibernateUtil {
	private static Configuration configuration;
	private static SessionFactory sessionFactory;
	
	static{
		try{
			configuration = new Configuration().configure();
			sessionFactory = configuration.buildSessionFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//获取Session对象的方法
	public static Session currentSession(){
		//getCurrentSession():创建Session对象后。不用手动关闭session
		//openSession():创建Session对象后。需要手动关闭session
		return sessionFactory.getCurrentSession();
	}
}

hibernate的基本架包,好吧应该没什么用
在这里插入图片描述

简单HQL使用
https://blog.csdn.net/qq_37846865/article/details/82938225
请大家多多指教

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值