博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate简单注释(一)
阅读量:7062 次
发布时间:2019-06-28

本文共 7977 字,大约阅读时间需要 26 分钟。

*****************************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>

<!-- Database connection settings -->

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>

<!-- SQL dialect -->

<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</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">create</property>

<!-- Names the annotated entity class -->

<mapping class="com.ij34.dao.New"/>

</session-factory>

</hibernate-configuration>

 

 

 

****************************************************************************************

package com.ij34.dao;

import javax.persistence.*;

@Entity

@Table(name="test01")
public class New {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String title;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

 

*********************************************

package com.ij34.web;

import org.hibernate.Session;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.*;

import com.ij34.dao.New;

public class test01 {

public static void main(String[] args)throws Exception {
//实例化Configuration
Configuration conf=new Configuration().configure();
ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
// 以Configuration实例创建SessionFactory实例
SessionFactory SF=conf.buildSessionFactory(SR);
//create session
Session session=SF.openSession();
//start 事务
Transaction tx=session.beginTransaction();
New n=new New();
n.setTitle("hello");
n.setContent("hello world");
session.save(n);
tx.commit();
session.close();
SF.close();
}
}


附连sqlserver,hibernate5

com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc:sqlserver://localhost:1433;DatabaseName=hibernatedb
sa
123456
20
1
5000
100
3000
2
true
org.hibernate.dialect.SQLServer2008Dialect
true
update
true
true
package com.ij34.dao;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entity(name="news")public class News {        @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer id;    @Column(name="title")    private String title;    @Column(name="content")    private String content;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }    }
package com.ij34.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.service.ServiceRegistry;import com.ij34.dao.News;public class Test {    public static void main(String[] args) {        // TODO Auto-generated method stub       /*        Configuration cfg = new Configuration()                .configure();*/        ServiceRegistry sr = new StandardServiceRegistryBuilder().configure().build();        SessionFactory sessionfactory = new MetadataSources(sr).buildMetadata().buildSessionFactory();        //SessionFactory sessionfactory = cfg.buildSessionFactory(serviceregistry);        Session session = sessionfactory.openSession();        Transaction tx = session.beginTransaction();        News news = new News();        news.setTitle("2018标题");        news.setContent("201820182018内容内容");        session.save(news);        tx.commit();        session.close();        sessionfactory.close();                    }}
4.0.0
com.ij34
HibernateQS
war
0.0.1-SNAPSHOT
HibernateQS Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
org.hibernate
hibernate-core
5.1.0.Final
log4j
log4j
1.2.17
org.slf4j
slf4j-nop
1.6.5
javassist
javassist
3.12.0.GA
com.microsoft.sqlserver
sqljdbc4
4.0
commons-collections
commons-collections
3.1
dom4j
dom4j
1.6.1
org.hibernate
ejb3-persistence
3.3.2.Beta1
org.hibernate.javax.persistence
hibernate-jpa-2.1-api
1.0.2.Final
org.hibernate.common
hibernate-commons-annotations
5.0.1.Final
antlr
antlr
2.7.7
org.hibernate
hibernate-osgi
5.1.0.Final
hibernate
hibernate-entitymanager
3.4.0.GA
pom
HibernateQS
UTF-8

 

 

 

转载地址:http://uzill.baihongyu.com/

你可能感兴趣的文章
使用SwingBench 对Oracle RAC DB性能 压力测试
查看>>
前端学Markdown
查看>>
easyui datagrid 行右键生成 动态获取(toolbar) 按钮
查看>>
Hibernate实体关系映射(OneToMany、ManyToOne双边)——完整实例
查看>>
get方式和set方式提交时乱码
查看>>
REST与SOA两种架构下WCF的异同比较(含源码)
查看>>
遇事处理方式
查看>>
五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
查看>>
linux 如何查看服务和端口
查看>>
Linux中如何让进程(或正在运行的程序)到后台运行?[zz]
查看>>
ZendGuardLoader安装
查看>>
floyd算法&迪杰斯特拉算法
查看>>
[CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
查看>>
加入强调语气,使用<strong>和<em>标签
查看>>
How Spring Boot Autoconfiguration Magic Works--转
查看>>
Android 最简单的SD卡文件遍历程序
查看>>
ArcGIS Engine开发之旅03--ArcGIS Engine中的控件
查看>>
sparkR 跑通的函数
查看>>
jQ效果:jQuery之插件开发短信发送倒计时功能
查看>>
aar
查看>>