Hibernate基本配置


1.引入jar

2.建立项目

3.创建实体类

package com.sqlserver.domain;
public class Customer {
    long cust_id;
    String cust_name;
    String cust_source;
    String cust_industry;
    String cust_level;
    String cust_phone;
    public Customer() {
    }
    public Customer(long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
            String cust_phone) {
        super();
        this.cust_id = cust_id;
        this.cust_name = cust_name;
        this.cust_source = cust_source;
        this.cust_industry = cust_industry;
        this.cust_level = cust_level;
        this.cust_phone = cust_phone;
    }
    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;
    }
}

4.创建数据库表

USE [hibernate]
GO

/****** Object:  Table [dbo].[customer]    Script Date: 2019/5/28 20:30:22 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[customer](
    ,) NOT NULL,
    ) NULL,
    ) NULL,
    ) NULL,
    ) NULL,
    ) NULL,
 CONSTRAINT [pk_001] PRIMARY KEY CLUSTERED
(
    [cust_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

5.JAVA类与数据库表映射classname.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>
    <class name="com.sqlserver.domain.Customer" table="customer">
        <!-- 主键 -->
        <id name="cust_id" column="cust_id">
        <generator class="native"></generator>
        </id>
        <!-- 普通列关联,name和column一样可以省略column -->
        <property name="cust_name" column="cust_name" />
        <property name="cust_source" column="cust_source"/>
        <property name="cust_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_phone" column="cust_phone"/>
    </class>
</hibernate-mapping>

6.数据库连接配置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>
        <!--配置SQLServer连接属性 -->
        <!-- 第一步配置方言 -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=hibernate</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">123456</property>
        <!--在控制台显示SQL语句 -->
        <property name="show_sqlserver">true</property>
        <!--根据需要自动生成、更新数据表 -->
        <property name="hbm2ddl.auto">update</property>
        <property name="myeclipse.connection.profile">sqlserver</property>
        <!--注册所有ORM映射文件 -->
        <mapping resource="com/sqlserver/domain/Customer.hbm.xml" />
    </session-factory>
</hibernate-configuration>

7.测试

package com.ahabest.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.sqlserver.domain.Customer;
public class HibrenateTest {
    public static void main(String args[]) {
            //加载配置文件
            Configuration configure = new Configuration().configure();
            //创建sessionfactory连接池
            SessionFactory sessionFactory = configure.buildSessionFactory();
            //获取sessionjdbc连接对象
            Session session = sessionFactory.openSession();

            Customer customer = new Customer();
            customer.setCust_name("aa");
            customer.setCust_source("aa");
            customer.setCust_industry("dsd");
            customer.setCust_level("2");
            customer.setCust_phone("132323333");
            // 保存
            session.save(customer);
            //释放资源
            session.close();
            sessionFactory.close();
        }
}

02Hibernate基本配置的更多相关文章

  1. 配置android sdk 环境

    1:下载adnroid sdk安装包 官方下载地址无法打开,没有vpn,使用下面这个地址下载,地址:http://www.android-studio.org/

  2. Android Studio配置 AndroidAnnotations——Hi_博客 Android App 开发笔记

    以前用Eclicps 用习惯了现在 想学学 用Android Studio 两天的钻研终于 在我电脑上装了一个Android Studio 并完成了AndroidAnnotations 的配置. An ...

  3. react-router 组件式配置与对象式配置小区别

    1. react-router 对象式配置 和 组件式配置    组件式配置(Redirect) ----对应---- 对象式配置(onEnter钩子) IndexRedirect -----对应-- ...

  4. 总结:Mac前端开发环境的搭建(配置)

    新年新气象,在2016年的第一天,我入手了人生中第一台自己的电脑(大一时好友赠送的电脑在一次无意中烧坏了主板,此后便不断借用别人的或者网站的).macbook air,身上已无分文...接下来半年的房 ...

  5. Android Studio 多个编译环境配置 多渠道打包 APK输出配置

    看完这篇你学到什么: 熟悉gradle的构建配置 熟悉代码构建环境的目录结构,你知道的不仅仅是只有src/main 开发.生成环境等等环境可以任意切换打包 多渠道打包 APK输出文件配置 需求 一般我 ...

  6. Virtual Box配置CentOS7网络(图文教程)

    之前很多次安装CentOS7虚拟机,每次配置网络在网上找教程,今天总结一下,全图文配置,方便以后查看. Virtual Box可选的网络接入方式包括: NAT 网络地址转换模式(NAT,Network ...

  7. [linux]阿里云主机的免登陆安全SSH配置与思考

    公司服务器使用的第三方云端服务,即阿里云,而本地需要经常去登录到服务器做相应的配置工作,鉴于此,每次登录都要使用密码是比较烦躁的,本着极速思想,我们需要配置我们的免登陆. 一 理论概述 SSH介绍 S ...

  8. nginx配置反向代理或跳转出现400问题处理记录

    午休完上班后,同事说测试站点访问接口出现400 Bad Request  Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...

  9. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

随机推荐

  1. 【HAOI 2008】 硬币购物

    [题目链接] 点击打开链接 [算法] 此题是一道好题! 首先,我们发现 : 付款方法数 = 不受限制的方法数 - 受限制的方法数 那么,我们怎么求呢? 我们用dp求出不受限制的方法数(f[i]表示买i ...

  2. sql 指删除表,改表名,改字段名

    删除表: DECLARE @Table NVARCHAR(30) DECLARE tmpCur CURSOR FOR SELECT name FROM sys.objects WHERE TYPE=' ...

  3. JSP指令--include指令(静态包含)

    转自:https://blog.csdn.net/chentiefeng521/article/details/51802319 include指令         include指令是文件加载指令, ...

  4. 虚拟机安装hadoop

    1.用VMware建立两台虚拟机(Centos6.5)系统,并设立主机名为node1与node2 2.设置虚拟机网络两台都是设置为如图: 3.修改两台虚拟机的/etc/hosts的文件为 (其中192 ...

  5. QT笔记-布局

    1 QT中使用布局器QLayout布局 2自动计算各个空间的大小和位置 采用的既定policy策略来调整子窗口的大小和位置 3QHBoxLayout横向布局  QVBoxLayout纵向布局 QHBo ...

  6. mac上 mysql 突然无法启动的问题

    创建: 2018/02/12 更新: 2018/02/12 补充如何不用sudo 更新: 2018/02/24 补充解决方案1,纠正不用sudo的方案.纠正一些错别字 更新: 2018/08/20 补 ...

  7. [App Store Connect帮助]八、维护您的 App(4.3)回复顾客评论(iOS、macOS 或 watchOS)

    您可以公开回复顾客评论,但在您的 App Store 产品页上每条评论仅会显示一条回复.您可以回复评论.编辑回复,以及删除回复. 在回复和编辑显示在 App Store 上之前(可能需要至多 24 小 ...

  8. C++this详解

    以前对this指针误解挺多的,在这里单独写一篇进行总结,有不对之处,欢迎指正批评! 一.问题 1.一个类中的不同对象在调用自己的成员函数时,其实它们调用的是同一段函数代码,那么成员函数如何知道要访问哪 ...

  9. 大数据技术之_25_手机APP信息统计系统项目_01_APP 数据生成模块 + 数据收集模块 + 数据处理模块框架搭建 + 业务需求处理 + 数据展示模块 +项目总结 + 问题总结

    一 项目概述1.1 角色1.2 业务术语1.3 项目效果展示二 项目需求三 项目概要3.1 项目技术架构3.2 项目目录结构3.3 项目技术选型3.4 项目整体集群规划3.5 创建项目工程四 APP ...

  10. 解决 iphone5 4 inch 屏 app黑边问题

    你需要一张640*1138的预加载图(launch image).在工程>TARGETS 中添加,系统将自动将其重命名为Default-568h@2x.png.