hibernate 入门,我就就看了这个例子,才决定用hibernate的!

来源:互联网 发布:centos zh cn.utf 8 编辑:程序博客网 时间:2024/06/02 04:29

POJO类

 

package com.qiujy.domain;

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Account {
@Id
@GeneratedValue
private Long id;
public Long getId() {
 return id;
}
public void setId(Long id) {
 this.id = id;
}
public String getLoginname() {
 return loginname;
}
public void setLoginname(String loginname) {
 this.loginname = loginname;
}
public String getPassword() {
 return password;
}
public void setPassword(String password) {
 this.password = password;
}
public String getEmail() {
 return email;
}
public void setEmail(String email) {
 this.email = email;
}
public Date getRegistrationTime() {
 return registrationTime;
}
public void setRegistrationTime(Date registrationTime) {
 this.registrationTime = registrationTime;
}
private String loginname;
private String password;
private String email;
private Date registrationTime;
public Account(){}

}

 

 

映射文件

<?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对象关系映射文件的根元素 -->
<hibernate-mapping>
 <!-- class元素用来定义一个持久化类及对应的数据库表 -->
 <class name="com.qiujy.domain.Account" table="account">
  <!--
   id元素:指定每个持久化类的唯一标识(即对象标识符OID)到数据库表主键字段的映射 name属性:指定持久化类的OID名
   column属性:指定数据库表主键字段名。此属性的名和映射到数据库表的字段名相同时,可省略
   type属性:指定主键映射时所使用的Hibernate类型名。此属性的类型为基本数据类型和String类型时,可省略
  -->
  <id name="id" column="id" type="long">
   <!--
    generator元素:指定对象标识符的生成器名。 native生成器把对象标识符值的生成工作交由底层数据库来完成
   -->
   <generator class="native" />
  </id>
  <!--
   property元素:指定持久化类的每一个要映射到数据库表的字段的属性的信息。 name属性;指定持久化类中要映射到数据库表字段的属性名。
   column属性:指定对应的表的字段名。此属性的名和映射到数据库表的字段名相同时,可省略
   type属性:指定属性映射所使用的Hibernate类型名。此属性的类型为基本数据类型和String类型时,可省略
   not-null属性:指定此属性映射到数据库表的字段值是否允许为值
  -->
  <property name="loginname" column="loginname" type="string"
   not-null="true" />
  <property name="password" column="password" type="string"
   not-null="true" />
  <property name="email" column="email" type="string" />
  <!-- 属性类型为Date类型的必须要明确指定使用的Hibernate类型名 -->
  <property name="registrationTime" column="registration_time"
   type="timestamp" />
 </class>
</hibernate-mapping>

 

 

测试页面

<%@ page language="java" import="java.util.*,com.main.CreateAccountTest" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
<%
CreateAccountTest test=new CreateAccountTest();
test.InsertAccount();
 %>
  <body>
    This is my JSP page. <br>
  </body>
</html>