您的位置:首页 > 编程语言 > Java开发

Hibernate及Struts框架基础学习(一)

2017-01-13 09:11 387 查看
不必相识 2017-01-01 10:21

最近一段时间小编我淡定的学习了一些跟期末考试没有任何关系的javaweb技术,hibernate框架基础和struts2框架基础。

初来乍到,大牛们请勿喷

本人用的环境为myeclipse10,虽然不是最新的版本,但是用着还是挺不错的,就没有换高版本的。当初才学习java web的时候光是这个环境的下载就费了我不少事,话不多说,进入正题。

myeclipse10



Hibernate 是一个对象/关系映射框架,它用来实现应用程序的持久化功能。简单点说hibernate可以让我们更加方便快速的将前台页面和后台的数据库连接起来,这里我用的是MySql数据库,小巧方便,然后我还用了一个数据库的第三方可视化工具Navicat。

数据库



Navicat



navicat
第一个Hibernate程序

1 准备数据库表

对象/关系映射是Hibernate的基础。在Hibernate中,持久化类是Hibernate操作的对象,它与数据库的数据表相对应。因此,首先创建应用程序说需要的数据表。

create table userinfo(

is int(4) not null auto_increment,

username varchar(10) not null,

password varchar(10) not null,

age int(4) default null,

email varchar(50) default null,

primary key(is))

2 定义持久化类

我先手写,其实这些都是可以自动生成的,玩过的人应该都懂

在使用Hibernate之前,我们首先了解一下持久化类的概念。持久化类也叫做实体类,使用来存储要与数据库交互的数据。下面的类就是一个持久化类。

Userinfo.java

package com.Mode;

public class Userinfo implements java.io.Serializable {

private Integer is;

private String username;

private String password;

private Integer age;

private String email;

// Constructors

/** default constructor */

public Userinfo() {

}

/** minimal constructor */

public Userinfo(String username, String password) {

this.username = username;

this.password = password;

}

/** full constructor */

public Userinfo(String username, String password, Integer age, String email) {

this.username = username;

this.password = password;

this.age = age;

this.email = email;

}

// Property accessors

public Integer getIs() {

return this.is;

}

public void setIs(Integer is) {

this.is = is;

}

public String getUsername() {

return this.username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return this.password;

}

public void setPassword(String password) {

this.password = password;

}

public Integer getAge() {

return this.age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getEmail() {

return this.email;

}

public void setEmail(String email) {

this.email = email;

}

}

3 定义映射文件

Hibernate的映射文件定义持久化类与数据表之间的映射关系,如数据库表的主键生成策略,字段类型,实体关联关系等。在Hibernate中映射文件是XML文件,其命名规则是 *.hbm.xml。

Userinfo.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 ">

<!--

Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

<class name="com.Mode.Userinfo" table="userinfo" catalog="access">

<id name="is" type="java.lang.Integer">

<column name="is" />

<generator class="identity" />

</id>

<property name="username" type="java.lang.String">

<column name="username" length="10" not-null="true" />

</property>

<property name="password" type="java.lang.String">

<column name="password" length="10" not-null="true" />

</property>

<property name="age" type="java.lang.Integer">

<column name="age" />

</property>

<property name="email" type="java.lang.String">

<column name="email" length="50" />

</property>

</class>

</hibernate-mapping>

4 编写配置文件

映射文件只是让持久化类和数据库表一一对应,但是还不知道连接哪个数据库,以及数据库的用户名和密码。这些信息对于所有的持久化类都是通用的,称这些信息为Hibernate配置信息,使用配置文件指定。

Hibernate配置文件可以使用XML格式,文件名为hibernate.cfg.xml。

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">

<!-- Generated by MyEclipse Hibernate Tools. -->

<hibernate-configuration>

<session-factory>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="connection.url">

jdbc:mysql://localhost:3306/access

</property>

<property name="connection.username">root</property>

<property name="connection.driver_class">

com.mysql.jdbc.Driver

</property>

<property name="myeclipse.connection.profile">conn</property>

<!--指定映射文件-->

<mapping resource="com/Mode/Userinfo.hbm.xml" />

</session-factory>

</hibernate-configuration>

5编写测试类程序

Mian.java

package com.Mode;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;

public class Main {

public static void main(String []arg){

Configuration configuration=new Configuration().configure();

ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(configuration.getProperties())

.buildServiceRegistry();

SessionFactory factory=configuration.buildSessionFactory(serviceRegistry);

Session session=factory.openSession();

Transaction tx=session.beginTransaction();

Userinfo user=new Userinfo();

//user.setIs(1);数据库已经设置了主键自动增加,所以这里就不需要自己添加了

user.setUsername("wxm");

user.setAge(22);

user.setEmail(null);

user.setPassword("12345");

session.save(user);

System.out.println("插入成功!");

//从数据库中读取上面插入的数据

Userinfo us=(Userinfo)session.get(Userinfo.class, new Integer(17));//这里的new Integer(x)是指在数据库表中的主键为X的元组

System.out.println(us.getUsername()+" "+us.getIs()+" "+us.getAge());

tx.commit();

session.close();

factory.close();

}

}

5 运行结果

前台中显示出信息



数据库中的元组信息



不足

上面的代码虽然能将数据库中的信息显示出来,但是显示的效果不尽如人意,那是因为我们没有专门用jsp或是html这样页面来呈现出我想要的结果,下次我用前台页面和后台数据库,来做一个简单的注册登录系统。

再次注意,以上的代码除了测试部分,其他的基本上可以不用手写而自动生成的

不喜欢打广告,有缘下次再见

本文为头条号作者发布,不代表今日头条立场。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: