完整的简单的Annotation版本的hibernate
2017-03-28 14:54
239 查看
1、所需要的jar包:
2、建表语句:
CREATE TABLE teacher(
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20),
age int
)
3、java类:Teacher.java
package com.gao.hibernate.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teacher {
private int id;
private String name;
private int age;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4、配置文件:hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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://127.0.0.1:3306/hibernate?characterEncoding=utf8&useSSL=true</property>
<property name="connection.username">root</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.MySQLDialect</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="com/huxing/hibernate/model/Student.hbm.xml"/> -->
<mapping class="com.gao.hibernate.entity.Teacher"/>
</session-factory>
</hibernate-configuration>
6、测试类:TeacherTestMain.java
package com.gao.hibernate.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class TeacherTestMain {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(1);
t.setName("aa");
t.setAge(22);
Configuration cfg = new AnnotationConfiguration();
SessionFactory cf = cfg.configure().buildSessionFactory();
Session session = cf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
cf.close();
}
}
2、建表语句:
CREATE TABLE teacher(
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20),
age int
)
3、java类:Teacher.java
package com.gao.hibernate.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teacher {
private int id;
private String name;
private int age;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4、配置文件:hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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://127.0.0.1:3306/hibernate?characterEncoding=utf8&useSSL=true</property>
<property name="connection.username">root</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.MySQLDialect</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="com/huxing/hibernate/model/Student.hbm.xml"/> -->
<mapping class="com.gao.hibernate.entity.Teacher"/>
</session-factory>
</hibernate-configuration>
6、测试类:TeacherTestMain.java
package com.gao.hibernate.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class TeacherTestMain {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(1);
t.setName("aa");
t.setAge(22);
Configuration cfg = new AnnotationConfiguration();
SessionFactory cf = cfg.configure().buildSessionFactory();
Session session = cf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
cf.close();
}
}
相关文章推荐
- hibernate与annotation版本配合问题
- Annotation版本的hibernate
- 为大家提供一个完整的Hibernate Annotation项目_源码下载
- hibernate Annotation简单学习
- 关于hibernate4.3.8final版本(annotation)注解问题 相当的bug啊 求大神指点迷津
- spring2.5 + hibernate3.2 标注(annotation)开发的简单示例
- hibernate 建立annotation版本的helloworld
- Hibernate的关系映射总结annotation版本
- 笔记——Annotation 注解(hibernate3版本)
- 1.3 hibernate annotation环境搭建及简单的例子
- Hibernate 3.2.5 annotation介绍及其简单例子
- Hibernate的Annotation版本的HelloWorld
- java annotation简单模拟hibernate
- spring + hibernate + annotation 简单整合
- hibernate源码下载地址及对应hibernate-annotation、slf4j版本
- 注解(Annotation)源码解析--简单实现(Hibernate,EJB,JPA)
- Hibernate 简单的annotation
- 老版本hibernate-core和hibernate-annotation版本的对应关系,Hibernate Compatibility Matrix
- IOS游戏源码下载之简易版雷电(2.2.3版本)源码完整下载和简单开发教程
- linux下安装部署redis及使用jedis访问redis server简单的完整流程(简洁版本)