Use Hibernate core API
For Hibernate configuration, We can use hibernate.cfg.xml file to configure:
<?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> <!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> </session-factory> </hibernate-configuration>
You configure Hibernate's SessionFactory, SessionFactory is a glob factory responsible for a particular database.
If you have several databases, for easier startup you shuould use several <session-factory> configurations in your several configration files.
(in java code use configure("xml file name") to chose configuration which is use for.)
the first four property elements is used for configure jdbc connection.
<property name="current_session_context_class">thread</property>
In Hibernate 4.x use currentSession. So This element provide the context class.
the value thread is use for tomcat and others which server is not suport several databases.
The value can be also "jta",Jta can be used for server which is suport several databases like JBOSS.
<mapping class="com.hibernate.model.Teacher"/> This element mapping to The java calss you created.
The class must use annotation like tihs:
@Entity
@Table(name="_teacher")
//If Object name is not same as table's name, use this annotate
public class Teacher {
@Id
@GeneratedValue
//if you want to automate id. Just use @GeneratedValue
//by default all database can automate id
//When use Database which suport identity. just use @GeneratedValue(strategy = GenerationType.IDENTITY)
//When use database which suport sequence. just use @GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} @Column(name="_name")
//If Object name is not same as column use this annotate
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Transient
//When you don't need to save this object to DB, just use @Transient
public String getYourWifeName() {
return yourWifeName;
}
public void setYourWifeName(String yourWifeName) {
this.yourWifeName = yourWifeName;
} @Temporal(TemporalType.DATE)
//just save date and type integer database is Date
public Date getBrithDate() {
return brithDate;
}
public void setBrithDate(Date brithDate) {
this.brithDate = brithDate;
}
@Enumerated(EnumType.STRING)
//use STRING will save a string in database witch you wrote in enumerate;
//use ORDINAL will save a integer type in database order by enumerate elements;
public Zhicheng getZhicheng() {
return zhicheng;
}
public void setZhicheng(Zhicheng zhicheng) {
this.zhicheng = zhicheng;
} private int id;
private String name;
private String title;
private String yourWifeName;
private Date brithDate;
private Zhicheng zhicheng; }
the annotation @Entity means this class can mapping for confguration.
@Id before Getter method, that means this proprety is the primary key for table in database.
If you want save data to database. Just do like this:
At the first, you must build the SessionFactory. The SessionFactory build by configuration file (hibernat.cfg.xml). So code:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Then, you should get Session from SessionFactory. Because SessionFactory comes from the configration.
Session session = getSessionFactory().getCurrentSession();
Use getCurrentSession() method to get session. Maybe, The Session is created aready. Because getSessionFactory() get the session from context. You can use this session until you commit it.
session.beginTransaction();
When you need to transact data, Just beginTransaction. Then you can do something for database.
session.save(d);
session.getTransaction().commit();
When you commit the session, CurrentSession will be destoryed. The datas' there stats:
@Test
public void testGetProgramer() {
ProgramerPK pp = new ProgramerPK();
pp.setId(1);
pp.setSid("123");
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Programer p = (Programer)session.get(Programer.class, pp);
session.getTransaction().commit();
System.out.println(p.getName());
}
@Test
public void testLoadProgramer() {
ProgramerPK pp = new ProgramerPK();
pp.setId(1);
pp.setSid("123");
Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Programer p = (Programer)session.load(Programer.class, pp);
System.out.println(p.getName());
session.getTransaction().commit();
}
Use Hibernate core API的更多相关文章
- AspNet Core Api Restful 实现微服务之旅 (一)
(一)了解微服务(二)搭建VS项目框架 (三)创建AspNet Core Api VS2017 安装包 链接:https://pan.baidu.com/s/1hsjGuJq 密码:ug59 创 ...
- 【从零开始搭建自己的.NET Core Api框架】(七)授权认证进阶篇
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- .net core api +swagger(一个简单的入门demo 使用codefirst+mysql)
前言: 自从.net core问世之后,就一直想了解.但是由于比较懒惰只是断断续续了解一点.近段时间工作不是太忙碌,所以偷闲写下自己学习过程.慢慢了解.net core 等这些基础方面学会之后再用.n ...
- 【从零开始搭建自己的.NET Core Api框架】(一)创建项目并集成swagger:1.1 创建
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- 【从零开始搭建自己的.NET Core Api框架】(四)实战!带你半个小时实现接口的JWT授权验证
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- 详解ASP.NET Core API 的Get和Post请求使用方式
上一篇文章帮助大家解决问题不彻底导致博友使用的时候还是遇到一些问题,欢迎一起讨论.所以下面重点详细讲解我们常用的Get和Post请求( 以.net core2.2的Http[Verb]为方向 ,推荐该 ...
- ASP.NET Core API 接收参数去掉烦人的 [FromBody]
在测试ASP.NET Core API 项目的时候,发现后台接口参数为类型对象,对于PostMan和Ajax的Post方法传Json数据都获取不到相应的值,后来在类型参数前面加了一个[FromBody ...
- Ubuntu server 运行.net core api 心得
1.安装.net core sdk 在微软.net core 安装页面找到linux 安装,按照步骤安装好 2.安装mysql 参考 Ubuntu安装mysql 3.配置mysql 1.需要将mysq ...
- ASP.NET CORE API Swagger+IdentityServer4授权验证
简介 本来不想写这篇博文,但在网上找到的文章博客都没有完整配置信息,所以这里记录下. 不了解IdentityServer4的可以看看我之前写的入门博文 Swagger 官方演示地址 源码地址 配置Id ...
随机推荐
- 一份不错的vue.js基础笔记!!!!
第一章 Vue.js是什么? Vue(法语)同view(英语) Vue.js是一套构建用户界面(view)的MVVM框架.Vue.js的核心库只关注视图层,并且非常容易学习,非常容易与其他库或已有的项 ...
- WCF权限控制
前面写了 WCF账户密码认证, 实现了帐号密码认证, 接下来看看如何对方法的细粒度控制, 本文很大程度参考了 WCF安全之基于自定义声明授权策略, 这篇文章对原理讲得比较清楚, 而我这篇文章呢, ...
- 选择流程—— switch if else结构
一.switch switch(表达式){ case 常量1: 语句; break; case 常量2: 语句; break; … default; 语句; } 例题:运用switch结构实现购物管理 ...
- Mongodb无法访问28107的问题
解压mongodb文件后,放到指定文件,最好别有空格.汉字之类的文件中 此时在mongodb文件夹下,建立一个 db 文件夹,此时执行启动命令,默认27017端口号可以打开,但是28017端口无法打开 ...
- Python切片
切片是啥, 可以吃么 切片肿么用哈 辣么长,记不住 切片是啥, 可以吃么 嘛,所谓切片故名思意就有选取的意思啦, 跟java里面的subString()意思差不多, 从原始的字符串中按规则提取出新的字 ...
- [python]自动化将markdown文件转成html文件
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Spring知识点总结大全(2)
3.Spring的AOP 一.AOP(Aspect-oriented programming,面向切面编程): 什么是AOP? 定义:将程序中的交叉业务逻辑提取出来,称之为切面.将这些切面动态织入到目 ...
- 如何用VB.Net创建一个三层的数据库应用程序
[b]1.[/b][b]概论:[/b] 本文将介绍如何创建一个三层应用程序,并且将介绍如何创建一个Web Service服务. ADO.NET创建Windows三层结构应用程序的体系架构如下图所示: ...
- page、pageContext、servletContext的区别
ServletContext是容器上下文,指当前的一个web应用的上下文 JSP网页本身,page对象是当前页面转换后的Servlet类的实例.从转换后的Servlet类的代码中,可以看到这种关系:O ...
- js基础知识点总结
如何在一个网站或者一个页面,去书写你的js代码:1.js的分层(功能):jquery(tool) 组件(ui) 应用(app),mvc(backboneJs)2.js的规划():避免全局变量和方法(命 ...