Java 测试Hibernate+Mysql简单的数据存储

想使用Hibernate框架,在网上看了一个Hibernate学习视频,试着做了一个小小的Java连接数据库的操作,Java初学者一个,大家多多包涵

开发环境:

  1.安装MySql,

  2.安装了Eclipse+插件hibernatetools-Update-4.1.2插件

  3.java包导入1.hibernate-core  2.Junit4.0  3.mysql

思路:

  1.配置Hibernate配置文件

  

<?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>
<property name="connection.username">root</property> <!-- 数据库用户名 -->
<property name="connection.password"></property> <!-- 数据库密码 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;charactertEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <!-- SQL输出到控制台 -->
<property name="format_sql" >true</property> <!-- 格式化SQL -->
<property name="hbm2ddl.auto">update</property> <!-- create|update|create-drop|validate:检查数据表结构是否相同 --> <mapping resource="Students.hbm.xml" /> <!-- 学生配置文件 -->
</session-factory>
</hibernate-configuration>

  2.定义持久话学生类

//学生类
public class Students {
// 1.公有的类
// 2.提供公有的不带参数的默认的构造方法
// 3.属性私有
// 4.属性setter/getter封装 private int sid; private String sname; private String gender; private String address; public Students() { } public Students(int sid, String sname, String gender, String address) {
// super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.address = address;
} public int getSid() {
return sid;
} public void setSid(int sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", address=" + address + "]";
} }

3.配置Students.hbm.xml文件,这个是由Hibernate插件工具自动完成的,Src右键-Other-Hibernate-hbm.xml配置文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-6-25 19:45:24 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>

4.定义一个StudentsTest测试类

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 org.junit.After;
import org.junit.Before;
import org.junit.Test; //测试类
//@SuppressWarnings("deprecation")
public class StudentsTest { private SessionFactory sessionFactory; //会话工厂
private Session session; //会话
private Transaction transaction; @Before //之前
public void init() //初始化
{
//创建配置对象
Configuration config=new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory=config.buildSessionFactory(serviceRegistry);
//会话对象
session=sessionFactory.openSession();
//开启事务
transaction=session.beginTransaction();
} @After //之后
public void destory()
{
transaction.commit(); //提交事务
session.close(); //关闭会话
sessionFactory.close(); //关闭会话工厂
} @Test //测试
public void testSaveStudents()
{
Students s=new Students(3,"张三丰","男","武当山");
session.save(s);
Students s1=new Students(4,"shexunyu","男","咸宁");
session.save(s1); //保存对象进入数据库
} }

5.到这就算完成了,在把工程的目录结构截图给大家

Java 测试Hibernate+Mysql简单的数据存储

Java 测试Hibernate+Mysql简单的数据存储

6.大家可以右键StudentsTest右键Runas-JUnit Test进行测试吧,好啦,代码我上传了,点这里下载

http://files.cnblogs.com/files/shexunyu/Java-Hibernate_002.zip

User:shexunyu Date:2015-06-26  Contact:

上一篇:同时运行多个scrapy爬虫的几种方法(自定义scrapy项目命令)


下一篇:解决CentOS7虚拟机无法上网并设置CentOS7虚拟机使用静态IP上网