首页 技术 正文
技术 2022年11月15日
0 收藏 376 点赞 4,406 浏览 2770 个字

接下来这几章节学习的是Hibernate,Hibernate的主要作用就是用来和数据库进行连接,简化了JDBC的操作。

首先,我们创建项目,然后把Hibernate的jar包和sqlserver的驱动导入进去。

接下来,我们须要写一个实体类:User

package cn.itcast.hibernate.domain;import java.util.Date;public class User {
private int id;
//private String name;
private String name;
private Date birthday; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

提供了三个属性:id、name、birthday

然后,我们在User所在的包下,配置User实体类的映射文件:User.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">
<hibernate-mapping
package="cn.itcast.hibernate.domain"><class name="User" table="uuser">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
<property name="birthday" />
</class>
</hibernate-mapping>

能够看到,我们配置了一个<class>,name属性是这个类的雷鸣,table是要映射的数据库的表

<id><property><property>这三个标签是定义了表中的属性列,当中为id设置了<generator>标签,规定了主键生成的策略(后边会介绍)

接下来,我们就要写配置文件了,在src文件夹下创建: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.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;DatabaseName=testhibernate
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property><property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.password">123456</property>
<property name="hbm2ddl.auto">create</property><property name="show_sql">true</property><mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" /></session-factory>
</hibernate-configuration>

在这上边配置了数据库驱动、数据库地址及数据库、用户名、密码、sqlserver数据库方言,还有建表方式、是否显示sql语句,最后一个<mapping>是把user.hbm.xml这个映射文件引入进来。

然后,我们写一个測试类实现一下:

package cn.itcast.hibernate;import java.util.Date;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import cn.itcast.hibernate.domain.User;public class Base {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();Session s = sf.openSession();
Transaction tx = s.beginTransaction();
User u = new User();
u.setBirthday(new Date());
u.setName("name1");s.save(u);
tx.commit();
s.close();
System.out.println("end");
}
}

在这段代码中,首先载入配置文件,然后打开session,再利用session打开事务,然后把user对象保存在session中,提交事务,关闭session。

在Hibernate中,默认是不会插入数据的,仅仅有打开事务才可以插入。

我们执行之后,就能够发现数据库中插入了一条数据。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,956
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,480
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,327
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,110
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,741
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,776