Hibernate使用mysql例子:

1) 新建一个bean: User.java

package com.my.bean;

import java.util.Date;

public class User {

    private int userid;
private String username;
private String password;
private char status;
private int isBuild;
private Date createTime;
private Date lastUpdateTime; public int getUserID() {
return userid;
}
public void setUserID(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public char getStatus() {
return status;
}
public void setStatus(char status) {
this.status = status;
}
public int getIsBuild() {
return isBuild;
}
public void setIsBuild(int isBuild) {
this.isBuild = isBuild;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
} }

2) 新建一个hibernate mapping xml file: User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="com.my.bean.User" table="sys_user">
<id name="UserID" type="int">
<column name="UserID" />
<generator class="assigned" />
</id>
<property name="Username" type="java.lang.String" length="50">
<column name="Username" />
</property>
<property name="Password" type="java.lang.String" length="50">
<column name="Password" />
</property>
<property name="Status" type="char" length="1">
<column name="Status" />
</property>
<property name="IsBuild" type="int">
<column name="IsBuild" />
</property>
<property name="CreateTime" type="java.util.Date">
<column name="CreateTime" />
</property>
<property name="LastUpdateTime" type="java.util.Date">
<column name="LastUpdateTime" />
</property>
</class> </hibernate-mapping>

3)新建一个hibernate config file: 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://127.0.0.1/obs</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.MySQL5Dialect</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/my/hbm/User.hbm.xml"/> </session-factory> </hibernate-configuration>

4)新建一个类:HibernateUtil.java

package com.my.dao.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
return configuration.configure().buildSessionFactory(
new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
} public static SessionFactory getSessionFactory() {
return sessionFactory;
} }

这是hibernate 4的sessionFactory方法。

5)测试:

package com.my.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session; import com.my.bean.User;
import com.my.dao.util.HibernateUtil; public class ConsoleTest { @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String hqlSelect = "from User where UserID=:UserID";
Query query = session.createQuery(hqlSelect);
query.setParameter("UserID", 1);
List<User> users = query.list();
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close(); for (User user : users) {
System.out.println("User name: " + user.getUsername());
}
} }

[Hibernate] - mysql的更多相关文章

  1. Spring MVC Hibernate MySQL Integration(集成) CRUD Example Tutorial【摘】

    Spring MVC Hibernate MySQL Integration(集成) CRUD Example Tutorial We learned how to integrate Spring ...

  2. JSP+Spring+SpringMVC+Hibernate+Mysql实现的校园失物招领网站

    项目简介 项目来源于:https://github.com/wenlongup/LostAndFound 因原github仓库无数据库文件,经过本人修改,现将该仓库重新上传至个人gitee仓库. ht ...

  3. hibernate+mysql的连接池配置

    1:连接池的必知概念    首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...

  4. hibernate+mysql 自动生成数据库问题

    Hibernate Entity类 表名注解大写时,在windows下mysql自动生成的表都为小写(不区分大小写),在linux下mysql自动生成区分大小写.导致数据库问题. 原因(window下 ...

  5. hibernate mysql写入中文乱码 解决

    启动hibernate项目,自动创建表,插入数据之后发现写入表里的数据里的中文是乱码.按如下方法解决了: 修改数据库的字符集为UTF-8,这个可以通过mysql的客户端软件里右键要修改的数据库的属性更 ...

  6. MyEclipse+Struts+Hibernate+Mysql开发环境配置

    软件: jdk-6u22-windows-x64.exe apache-tomcat-6.0.29.exe mysql-5.1.51-winx64.exe myeclipse-8.6.0-win32. ...

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

    想使用Hibernate框架,在网上看了一个Hibernate学习视频,试着做了一个小小的Java连接数据库的操作,Java初学者一个,大家多多包涵 开发环境: 1.安装MySql, 2.安装了Ecl ...

  8. Hibernate + MySQL中文乱码问题

    如果持久化的类中有包括了汉字的String对象,那么对应到数据库中汉字的部分就会是乱码.这主要是由于MySQL数据表的字符集与我们当前使用的本地字符集不相同造成的. 如果是windows系统,那么系统 ...

  9. Hibernate MySQL 数据库 使用别名 报 Column * Not Found

    使用Hibernate 查询MySQL数据表的时候报 Column Not Found ,原因是MySQL的驱动不支持别名, 解决方案如下,在连接参数中加上 useOldAliasMetadataBe ...

随机推荐

  1. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  2. Mysql时间戳函数

    1.转换为时间戳 select unix_timestamp('2013-07-15 10-06-07') 如果参数为空,则为当前时间 2.转换为时间 select from_unixtime(tim ...

  3. Wamp安装使用+Git for Windows

    相信很多朋友都曾在windows上做过web开发,我们常用的Web应用程序平台是:Apache+Mysql+Perl/PHP/Python,在windows下集成称为WAMP.web开发新手有时候由于 ...

  4. kinect在ros上的初步测试---17

    摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.在使用本贴前必须先按照我的上一个博文正确在ubuntu上安装kinect驱动:http:// ...

  5. 3-4 rpm包查询

    概述:yum不能查询已经安装好的rpm包, 就算采用了yum来进行安装,查询方法还是依赖rpm包的查询, 因此rpm包的查询十分常用和重要 1.查询是否安装 <1>rpm -q 包名(不是 ...

  6. php归获取当前目录下的二级目录数 和文件数

    <?php        header('Content-Type: text/html; charset=gb2312');        // $baseDir = "/www/u ...

  7. HDU-5785 Interesting(Manacher算法+区间处理)

    题目大意:给一个字符串,求所有相邻两回文子串的外侧下标之积的和 题目分析:另L[i]为所有以 i 为右端点的回文字串的左端点之和,同理,另R[i]表示所有以 i 为左端点的回文子串的右端点之和.显然, ...

  8. linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)

    原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html 驱动常用头文件介绍 #include <linux/***.h> 是 ...

  9. InputStreamReader和OutputStreamWriter

    public class ClientSocket {   public static void main(String args[]) {         String host = "1 ...

  10. RedHat Linux下利用sersync进行实时同步数据

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://linux5588.blog.51cto.com/65280/772054 拓扑图 ...