Hibernate框架的基本搭建(一个小的java project的测试向数据库中插入和查询数据的功能)
Hibernate介绍:Hibernate是一种“对象-关系型数据映射组件”,它使用映射文件将对象(object)与关系型数据(Relational)相关联,在Hibernate中映射文件通常以".hbm.xml"作为后缀。
包:com.cn.beans
与数据库表t_user(id主键,name)对应的工程中POJO代码(Javabean):Tuser.Java
package com.cn.beans;
import java.io.Serializable; import javax.annotation.Generated; public class Tuser implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L; private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
POJO与数据库表对应的映射文件:Tuser.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.cn.beans">
<class name="com.cn.beans.Tuser" table="t_user">
<id name="id" column="id" type="int">
</id>
<property name="name" column="name" type="string">
</property>
</class>
</hibernate-mapping>
包:com.cn.hibernate.test:HibernateBaseTest.java代码:
package com.cn.hibernate.test; import java.util.List; import junit.framework.Assert;
import junit.framework.TestCase; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import com.cn.beans.Tuser; public class HibernateBaseTest{
Session session=null; //初始化hibernate的Session
protected void setUp(){
try{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory=config.buildSessionFactory();
session = sessionFactory.openSession();
System.out.println("连接数据库成功!");
}catch(HibernateException e){
e.printStackTrace();
System.out.println("连接数据库失败!");
}
} public static void main(String[] args) {
HibernateBaseTest j = new HibernateBaseTest();
j.setUp();
//j.testInsert();
j.testSelect();
}
//teardown()方法用于关闭用setUp打开的hibernate中的Session;
protected void teardown(){
try{
session.close();
System.out.println("关闭数据库");
}catch(HibernateException e){
e.printStackTrace();
}
} public void testInsert(){
Transaction tran=null;
try{
tran=session.beginTransaction();
Tuser user=new Tuser();
user.setId(8);
user.setName("Emma11");
session.save(user);
session.flush();
tran.commit();
session.close();
}catch(HibernateException e){
e.printStackTrace();
if(tran!=null){
try{
tran.rollback();
}catch(HibernateException e1){
e1.printStackTrace();
}
}
}
} //对象读取(select)测试,请保证运行之前数据库中已经存在name='Emma11'的记录 public void testSelect(){
//setUp();
String hql="from Tuser where name='Emma11'";
try{
List userList=session.createQuery(hql).list();
Tuser user=(Tuser)userList.get(0);
//Assert.assertEquals(user.getName(), "Emma11");
System.out.println("用户为:"+user.getId()+" "+user.getName());
}catch(HibernateException e){
e.printStackTrace();
//Assert.fail(e.getMessage());
}
teardown();
}
}
测试类:JunitTest.java package com.cn.hibernate.test;
import org.junit.Test; public class JunitTest { @Test
public void testInsert() {
} @Test
public void testselect() {
} }
该包对应的文件:
1.hibernate.cfg.xml(与sql server2008数据库进行连接的配置文件)
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--显示执行的SQL语句-->
<property name="hibernate.show_sql">true</property>
<!--连接字符串-->
<property name="hibernate.connection.url">
jdbc:sqlserver://192.168.254.133:1433;DatabaseName=sample
</property>
<!--连接数据库的用户名-->
<property name="hibernate.connection.username">sa</property>
<!--数据库用户密码-->
<property name="hibernate.connection.password">123456</property>
<!--数据库连接池的大小-->
<property name="hibernate.connection.pool.size">20 </property>
<!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->
<property name="jdbc.fetch_size">50</property>
<!--数据库驱动-->
<property name="hibernate.connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<!--选择使用的方言-->
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<!-- <property name="connection.pool_size">0</property> -->
<property name="c3p0.max_size">2000</property>
<property name="c3p0.min_size">200</property>
<property name="c3p0.timeout">5000</property>
<property name="c3p0.validate">false</property>
<!--
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
-->
<!-- Enable Hibernate's automatic session context management -->
<!--<property name="current_session_context_class">thread</property>
--><property name="connection.autocommit">true</property>
<!--在启动时删除并重新创建数据库-->
<mapping resource="com/cn/beans/Tuser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
结果:插入记录
查询记录:
Hibernate框架的基本搭建(一个小的java project的测试向数据库中插入和查询数据的功能)的更多相关文章
- Spring框架——事务管理方式搭建一个小的项目
学习Spring框架,通过事务管理的方式搭建一个小的项目,该项目可以查询对数据库中的图书库存数量进行修改. 首先,使用MVC分层的设计模式思想搭建项目目录结构. 此部分代码源码之中都有相关注释,所以尽 ...
- 搭建一个完整的Java开发环境
搭建一个完整的Java开发环境 作为一个Java程序员,配置一个java开发环境是必备的技能,今天给广大菜鸟初学者补上一课.环境的配置,大概就分三个1,JDK 2,Tomcat(或者其他的)3,ecl ...
- 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)
分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...
- hibernate 4.3 在使用获取数据获取不到数据库中最新变更的数据问题解决
hibernate 4.3 在使用获取数据获取不到数据库中最新变更的数据问题解决,应该是因为缓存问题 问题过程和现象: 查询一个数据列表=>数据库中手动update了数据=>刷新页面,数据 ...
- (转)分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)
分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...
- 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】
问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...
- 以使用QSqlQuery向数据库中插入数据为例,做一个小结
背景: 最近在使用Qt+SQLite写一个本地数据库管理程序(使用者不懂SQL),在写向数据库中插入数据的 相关的函数时,我遇到了几个问题(暂时就这些): 1.向指定字段插入指定数据时,读取到的数据都 ...
- MyBatis框架的insert节点-向数据库中插入数据
需求:使用mybatis框架中的insert元素节点向数据库中插入数据 UserMapper.xml UserMapper.java 编写测试方法: @Test public void testAdd ...
- 2.0、Hibernate框架的简单搭建
一.Hibernate:是一个开放源代码的对象关系映射框架,对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句 ...
随机推荐
- Linux高级编程--08.线程概述
线程 有的时候,我们需要在一个基础中同时运行多个控制流程.例如:一个图形界面的下载软件,在处理下载任务的同时,还必须响应界面的对任务的停止,删除等控制操作.这个时候就需要用到线程来实现并发操作. 和信 ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- Gradle学习系列之十——自定义Plugin(本系列完)
在本系列的上篇文章中,我们讲到了如何自定义Task类型,在本篇文章中,我们将讲到如何自定义Plugin. 请通过以下方式下载本系列文章的Github示例代码: git clone https://gi ...
- error LNK1281: 无法生成 SAFESEH 映像 LNK2026 模块对于 SAFESEH 映像是不安全的 VS2015 /win10
平台 VS2015 /win10 错误 LNK1281 无法生成 SAFESEH 映像. 错误 LNK2026 模块对于 SAFESEH 映像是不安全的. 将 release 改成 debug
- MySQL主从复制与读写分离 --非原创
原文出处:http://www.cnblogs.com/luckcs/articles/2543607.html MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 ...
- Netty学习之服务器端创建
一.服务器端开发时序图 图片来源:Netty权威指南(第2版) 二.Netty服务器端开发步骤 使用Netty进行服务器端开发主要有以下几个步骤: 1.创建ServerBootstrap实例 Serv ...
- PHP图像处理类库及演示分享
简单写了一个PHP的图像处理类库,虽然功能比较少,但是目前也没用到太高级的,以后用到了再填吧,或者哪位给点建议加上什么功能,或者有什么需求可以跟我说,我有时间加上,如果哪位对这个类库进行了扩展的话,还 ...
- vs2008不能创建C#项目的解决方法
解决方法:1.先关闭 Visual Studio 2008 ;2.在运行中输入命令"devenv.exe /setup"3.运行 Visual Studio 2008 ,一切搞定. ...
- POSTMAN and HTTPie to test APIs
http://blog.mashape.com/postman-httpie-test-apis/ We love working with APIs at Mashape, and we love ...
- python 的import机制2
http://blog.csdn.net/sirodeng/article/details/17095591 python 的import机制,以备忘: python中,每个py文件被称之为模块, ...