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. stdobj to array php

    The lazy one-liner method You can do this in a one liner using the JSON methods if you're willing to ...

  2. 遇到double 数目过大,转String变成科学计数法

    问题: java中,当double数目过大,转出String时,变成了科学记数法的表示. 总结: 1.项目的存储用的是mysql,mysql的类型和java类型之间存在映射关系,以前关注不多.现在总结 ...

  3. TCP三次握手连接与四次握手断开

    http://blog.csdn.net/whuslei/article/details/6667471(三次握手与四次握手) 1. TCP的三次握手最主要是防止已过期的连接再次传到被连接的主机. 如 ...

  4. Sobel算子 (转)

    幻灯片1 Sobel算子 幻灯片2 一.Sobel边缘检测算子 l 在讨论边缘算子之前,首先给出一些术语的定义: l (1)边缘:灰度或结构等信息的突变处,边缘是一个区域的结束,也是另一个区域的开始, ...

  5. Spring MVC 属性文件读取注入到静态字段

    目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用   在项目中,有些参数需要配置到属性文件xxx.pr ...

  6. timus 1022 Genealogical Tree(拓扑排序)

    Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...

  7. timus 1136 Parliament(二叉树)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  8. HDU-1542 Atlantis(离散化+扫描线)

    题目大意:给n个矩形,可能重叠,求面积. 题目分析:线段树维护扫描线. 代码如下: # include<bits/stdc++.h> using namespace std; # defi ...

  9. POJ-1155 TELE (树形DP+分组背包)

    题目大意:给一棵带边权的有根树,每个叶子节点有权.边权表示代价,叶子节点的权值代表可以补偿多少代价.问从根节点最多可以到达多少个叶子,使得付出的总代价不大于0. 题目分析:定义状态dp(u,k)表示从 ...

  10. [POI 2008][BZOJ 1132]Tro

    这题我真是无能为力了 这题的做法还是挺简单的 枚举左下角的点做为原点,把其余点按极角排序    PS.是作为原点,如枚举到 k 时,对于所有 p[i] (包括p[k]) p[i]-=p[k] (此处为 ...