地址:http://blog.sina.com.cn/s/blog_81496ec00101i5f8.html
Hibernate的基础配置:
hibernate.cfg.xml
<?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">admin</property>
<!-- 驱动类 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库URL -->
<property name="connection.url">jdbc:mysql://localhost:3306/newStrutsDemo</property>
<!-- 数据库方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否在SessionFactory创建后,自动创建数据库表,none/create/create-drop/update-->
<property name="hbm2ddl.auto">none</property>
<!-- 是否把执行的SQL语句输出到控制台 -->
<property name="show_sql">true</property>
<!-- 是否把控制台SQL语句格式化 -->
<property name="format_sql">true</property>
<!-- 映射文件配置,配置文件名必须包含其相对于根的全路径 -->
<mapping resource="com/oracle/hibernetdemo/pojos/Users.hbm.xml" />
</session-factory>
</hibernate-configuration>
POJO类,User.java
package com.oracle.hibernetdemo.pojos;
public class Users {
private int userid;
private String username;
private String nickName;
private String password;
private String specialAttention;
private String sex;
private String favoriate;
private String introduction;
private String head;
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 getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSpecialAttention() {
return specialAttention;
}
public void setSpecialAttention(String specialAttention) {
this.specialAttention = specialAttention;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getFavoriate() {
return favoriate;
}
public void setFavoriate(String favoriate) {
this.favoriate = favoriate;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
public Users() {
super();
}
public Users(int userid, String username, String nickName, String password,
String specialAttention, String sex, String favoriate,
String introduction, String head) {
super();
this.userid = userid;
this.username = username;
this.nickName = nickName;
this.password = password;
this.specialAttention = specialAttention;
this.sex = sex;
this.favoriate = favoriate;
this.introduction = introduction;
this.head = head;
}
@Override
public String toString() {
return "Users [userid=" + userid + ", username=" + username
+ ", nickName=" + nickName + ", password=" + password
+ ", specialAttention=" + specialAttention + ", sex=" + sex
+ ", favoriate=" + favoriate + ", introduction=" + introduction
+ ", head=" + head + "]";
}
}
User.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>
<class name="com.oracle.hibernetdemo.pojos.Users" table="users">
<id name="userid" type="java.lang.Integer">
<column name="userid"></column>
<generator class="native" />
</id>
<property name="username" type="java.lang.String" column="username"></property>
<property name="nickName" type="java.lang.String" column="nickName"></property>
<property name="password" type="java.lang.String" column="password"></property>
<property name="specialAttention" type="java.lang.String" column="specialAttention"></property>
<property name="sex" type="java.lang.String" column="sex"></property>
<property name="favoriate" type="java.lang.String" column="favoriate"></property>
<property name="introduction" type="java.lang.String" column="introduction"></property>
<property name="head" type="java.lang.String" column="head"></property>
</class>
</hibernate-mapping>
测试类:
TestHibernate.java
package com.oracle.hibernetdemo.pojos;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestHibernate {
public static void main(String[] args) {
Users user=new Users(0,"Avil","艾薇儿","Avirl","摇滚","女","唱歌","Cray women","images.gif");
Configuration conf=new Configuration().configure();
SessionFactory factory=conf.buildSessionFactory();
Session session=factory.openSession();
Transaction tr=session.beginTransaction();
session.save(user);
tr.commit();
session.close();
factory.close();
}
}
相关推荐
bookAdmin.rar是一个人做的图书的增删改差(带分页)的程序。
论坛转帖工具 .......... 纯HTML
一键转帖 一键8经验签到. 绿易贴吧工具3.7 11.11版.rar
J2ME全方位开发讲解基础汇总[转帖] 一、J2ME中需要的Java基础知识 现在有大部分人,都是从零开始学J2ME的,学习J2ME的时候,总是从Java基础开始学习,而且现在讲Java基础的书籍中都是以J2SE来讲基础,这就给学习造成...
编辑人员转帖去水印工具,很好用,直接选中水印区域后运行伪装即可!
图形系统开发基础,我是转帖的,感觉不错,属于入门级别。 1:搭建Linux下的图形系统开发环境 图形输出在嵌入式开发中,多数是向FrameBuffer中写数据,然后会显示在LCD中,在Linux下做图形开发的时候,需要模拟各种...
UBB论坛转帖圣手.exeUBB论坛转帖圣手.exe
转帖性能测试
discuz X2转帖工具、采集工具,discuz论坛批量发帖,批量转帖工具
这是一个世界编程大赛第一名写的程序,很好玩的,大家试试看。
1.修改自Convert X转帖工具 2.新增批量替换关键词(原来是单个词语替换,可以利用这个功能删除一些网站的防转帖代码) 3.批量随机新增文字(新增内容可自定义,从而实现伪原创) 4.cookie记录替换和新增关键词(避免每次...
转帖图片提取工具可以对论坛图片附件信息进行清除,只保留图片代码,操作很简单,推荐有需要转帖图片工具的朋友下载 转帖图片提取工具使用方法: 将IP138上处理过的东西复制到上方的编辑框内,点击只要图片,下面...
Html2UBBMaxcj_Softii论坛专用转帖工具
gl8雨刮维修以及调试转帖.doc
转帖PLCDCSFCS三大控制系统的特点和差异.doc
关于工作与生活的转帖 一篇论述精辟的文章,值得一看。
用PHP批量生成图片缩略图——活跃论坛转帖
轻松转帖之突破网页复制限制宣贯.pdf
股票T+0的操作(转帖).pdf