Hibernate4使用Annotation连接访问MySQL的小例子
2014-10-17 15:28
399 查看
1、创建一个Teacher类
[java] view
plaincopy
package com.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
--------Teacher类中使用Annotation
2、设置表结构,创建数据库和数据表
[sql] view
plaincopy
create database hibernate;
use hibernate;
create table teacher(
id int primary key,
name varchar(20),
title varchar(20)
);
3、设置Hibernate配置文件hibernate.cfg.xml,其位置位于src下
[html] view
plaincopy
<?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>
<!-- 连接的数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 连接的数据库的url -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<!-- 连接的数据库的用户名-->
<property name="connection.username">root</property>
<!-- 连接的数据库的密码 -->
<property name="connection.password"></property>
<!-- 配置Hibernate数据库方言 -->
<property name="Dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 输出执行的SQL语句 -->
<property name="show_sql">true</property>
<!-- 启动时撤销并重新创建数据库的模式 -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.model.Teacher"/>
</session-factory>
</hibernate-configuration>
4、建立测试类,测试
[java] view
plaincopy
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.model.Teacher;
public class TeacherTest {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setId(2);
teacher.setName("黎明");
teacher.setTitle("教授");
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
s.save(teacher);
tx.commit();
s.close();
sf.close();
}
}
5、运行测试类,并查看表中是否已添加一条数据记录,且后台输出执行的SQL语句:Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)。
注意:项目建立时需将Hibernate的jar包(位于hibernate-release-4.1.2.Final\lib\required下的所有包)和MySQL的JDBC的包导入到项目中去;
cfg.configure()默认配置文件为hibernate.cfg.xml,即cfg.configure()等价于cfg.configure("hibernate.cfg.xml"),若配置文件不是这个名则必须指定配置文件名。
-------------------------------------------------------------------------------------------------------------------
其中,org.hibernate.cfg.AnnotationConfiguration已经弃用,直接使用org.hibernate.cfg.Configuration即可,创建语句Configuration cfg = new Configuration()而不是AnnotationConfiguration cfg = new AnnotationConfiguration()。
[java] view
plaincopy
package com.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
--------Teacher类中使用Annotation
2、设置表结构,创建数据库和数据表
[sql] view
plaincopy
create database hibernate;
use hibernate;
create table teacher(
id int primary key,
name varchar(20),
title varchar(20)
);
3、设置Hibernate配置文件hibernate.cfg.xml,其位置位于src下
[html] view
plaincopy
<?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>
<!-- 连接的数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 连接的数据库的url -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<!-- 连接的数据库的用户名-->
<property name="connection.username">root</property>
<!-- 连接的数据库的密码 -->
<property name="connection.password"></property>
<!-- 配置Hibernate数据库方言 -->
<property name="Dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 输出执行的SQL语句 -->
<property name="show_sql">true</property>
<!-- 启动时撤销并重新创建数据库的模式 -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.model.Teacher"/>
</session-factory>
</hibernate-configuration>
4、建立测试类,测试
[java] view
plaincopy
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.model.Teacher;
public class TeacherTest {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.setId(2);
teacher.setName("黎明");
teacher.setTitle("教授");
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
s.save(teacher);
tx.commit();
s.close();
sf.close();
}
}
5、运行测试类,并查看表中是否已添加一条数据记录,且后台输出执行的SQL语句:Hibernate: insert into Teacher (name, title, id) values (?, ?, ?)。
注意:项目建立时需将Hibernate的jar包(位于hibernate-release-4.1.2.Final\lib\required下的所有包)和MySQL的JDBC的包导入到项目中去;
cfg.configure()默认配置文件为hibernate.cfg.xml,即cfg.configure()等价于cfg.configure("hibernate.cfg.xml"),若配置文件不是这个名则必须指定配置文件名。
-------------------------------------------------------------------------------------------------------------------
其中,org.hibernate.cfg.AnnotationConfiguration已经弃用,直接使用org.hibernate.cfg.Configuration即可,创建语句Configuration cfg = new Configuration()而不是AnnotationConfiguration cfg = new AnnotationConfiguration()。
相关文章推荐
- Hibernate4使用Annotation连接访问MySQL的小例子
- Hibernate4使用映射文件hbm.cml连接访问MySQL的小例子
- 使用C语言访问MySQL数据 —— 连接和错误处理
- MySQL 使用 SSL 连接(附 Docker 例子)
- .net 使用管道方式访问mySQL的连接字符串
- 使用正确bit数的dll 或 lib(Qt 访问MySQL连接失败的一个原因)
- mysql主主复制+keepalived高可用(使用VIP访问数据库提示不允许连接)
- LINUX下PHP使用PDO访问MYSQL的连接设置注意事项
- jdbc链接mysql的第二个例子。使用properties文件存放连接信息。使用jar文件一个
- 使用Navicat for MySQL访问数据库老是提示ip不能连接mysql问题
- MySQL 使用 SSL 连接(附 Docker 例子)
- Java使用jdbc连接和操作mysql的例子
- python访问mysql 连接mysql - 使用mysql connector
- 使用C语言访问MySQL数据 —— 连接和错误处理
- Delphi2007中使用DbExpress连接MySql的例子
- 使用Connector/C++(VS2015)连接MySQL的完整例子
- mysql的jdbc驱动安装及jsp连接例子
- 一个jdbc连接mysql的小例子
- 使用Robot连接SQL的例子
- Java 实现连接sql server 2000(JDBC数据库访问例子)