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. [Usaco2017 Dec] A Pie for a Pie

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5140 [算法] 最短路 时间复杂度 : O(N^2) [代码] #include&l ...

  2. asp.net mvc5 使用百度ueditor 本编辑器完整示例(三)在IIS中多个应用程序使用多个ueditor对象

    最近做了一个项目,要求同一类型的多个专业应用程序(网站),但是每个应用程序都需要调用各自当中的ueditor. 步骤: 一.在vs2013中设置每个专业的asp.net mvc 应用程序. 1.配置根 ...

  3. oracle 备份数据

    exp AC_SSO/AC_SSO@HB file=d:\wamp\Golden3C_AuthenticationCenter.dmp owner=AC_SSO full=y用户名/密码@服务 exp ...

  4. Gym 100531B Buffcraft (贪心+暴力+前缀和)

    题意:给定两个加血的方式,一个是直接加多少,另一种是加百分之几,然后你能够你选 k 种,问你选哪 k 种. 析:首先肯定要选加的多的,所以我们先排序,从大到小,然后用前缀和存储一下,再去枚举从第一种和 ...

  5. Luogu P1137 旅行计划 【拓扑排序+Dp】By cellur925

    题目传送门 由于满足游览先后顺序从西到东的性质,我们很自然的想到用拓扑排序处理出一个合理的游览顺序. 然鹅,之后呢? 事实上,拓扑排序常与Dp相结合,解决后效性.我们就可以在每次拓扑入队的时候更新答案 ...

  6. QB学堂济南游记

    七天很快就过去了,今天的测试意味着集训也将结束.回首七天来,与许多dalao同处一室,见识到了各种厉害的老师.厉害的算法.厉害的数据结构. 前两天jzh与yl老师讲课的时候还是全程在线,然而讲到数据结 ...

  7. SpringBoot项目docker化

    前言 有很多种方案构建Docker镜像,包括Dockerfile构建.maven插件构建,这里我使用了最简单的Dockerfile构建的. 一.安装Docker 我的虚拟机系统是CentOS7,需要是 ...

  8. python服务之flask

    前言: 关于python flask 的介绍.指导.案例,网络上比比皆是.这里参考官网:http://www.pythondoc.com/flask/index.html 你可能不知道的flask服务 ...

  9. Qt之界面(自定义标题栏、无边框、可移动、缩放)

    效果 自定义标题栏 titleBar.h #ifndef TITLEBAR_H #define TITLEBAR_H #include <QLabel> #include <QPus ...

  10. 递推DP UVA 590 Always on the run

    题目传送门 题意:题意难懂,就是一个小偷在m天内从城市1飞到城市n最小花费,输入的是每个城市飞到其他城市的航班. 分析:dp[i][j] 表示小偷第i天在城市j的最小花费.状态转移方程:dp[i][j ...