分享
 
 
 

read AppFuse 11-实践-业务信息person的建立

王朝java/jsp·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

read AppFuse 11-实践-业务信息person的建立

● 按照AppFuse帮助,进行AppFuse开发实践

(1) 创建POJO

package org.dudu.model;

/**

* @hibernate.class table="person"

*/

public class Person extends BaseObject

{

private Long id;

private String firstName;

private String lastName;

/**

* @return Returns the id

* @hibernate.id column="id"

* generator-class="increment" unsaved-value="null"

*/

public long getId() {

return id;

}

/**

* @return Returns thd first name

* @struts.validator type="required"

* @hibernate.property column="first_name" length="50" not-null="true"

*/

public String getFirstName() {

return firstName;

}

/**

* @return Returns the last name

* @struts.validator type="required"

* @hibernate.property column="last_name" length="50" not-null="true"

*/

public String getLastName() {

return lastName;

}

/**

* @param l

*/

public void setId(long l) {

id = l;

}

/**

* @param string

*/

public void setFirstName(String string) {

firstName = string;

}

/**

* @param string

*/

public void setLastName(String string) {

lastName = string;

}

/* (非 Javadoc)

* @see java.lang.Object#equals(java.lang.Object)

*/

public boolean equals(Object o) {

// TODO 自动生成方法存根

return false;

}

/* (非 Javadoc)

* @see java.lang.Object#hashCode()

*/

public int hashCode() {

return 0;

}

public String toString() {

return null;

}

}

Ø Hibernate是一个对象/关系(O/R) 框架,它允许你将一个Java对象同一个表关联。

Ø 要确保对象的id属性的getter是当前类的第一个方法,否则生成的JSP的页面可能会有一个脚本错误

Ø 注意:所有的 @hibernate.* tag必须放在你的POJO的getter的Javadoc中

Ø 如果你想更改列的长度, 更改@hibernate.property标记的length属性。如果你希望它是一个必填的字段(NOT NULL),那么你要添加not-null="true"。

Ø 添加column属性的唯一原因是:表的列名和对象的属性名字不一致。如果他们一致,就不需要添加column属性。[引用]

(2) 使用Ant根据Person对象创建一个person新表和名为Person.hbm.xml的映射文件。

E:\dudu>ant setup-db

Buildfile: build.xml

init:

db-create:

[echo] Detected MySQL, creating database...

[echo] Creating database with: mysql-create.sql

[echo] URL: jdbc:mysql://localhost/mysql

[sql] Executing file: E:\dudu\metadata\sql\mysql-create.sql

[sql] 3 of 3 SQL statements executed successfully

clean:

[echo] Cleaning build and distribution directories

[delete] Deleting directory E:\dudu\build

[delete] Deleting directory E:\dudu\dist

[delete] Deleting: E:\dudu\database.properties

build.properties.missing:

prepare:

[echo] Preparing target directory 'E:\dudu/build/dudu'

[mkdir] Created dir: E:\dudu\build\dudu

[mkdir] Created dir: E:\dudu\build\dudu\WEB-INF

[mkdir] Created dir: E:\dudu\dist\webapps

hibernatedoclet:

[hibernatedoclet] (XDocletMain.start 47 ) Running <hibernate/

>

[hibernatedoclet] Generating mapping file for org.dudu.model.User.

[hibernatedoclet] org.dudu.model.User

[hibernatedoclet] Generating mapping file for org.dudu.model.Person.

[hibernatedoclet] org.dudu.model.Person

[hibernatedoclet] Generating mapping file for org.dudu.model.Role.

[hibernatedoclet] org.dudu.model.Role

[hibernatedoclet] Generating mapping file for org.dudu.model.UserCookie.

[hibernatedoclet] org.dudu.model.UserCookie

compile-dao:

compile-module:

[echo] Compiling dao...

[mkdir] Created dir: E:\dudu\build\dao\classes

[mkdir] Created dir: E:\dudu\build\test\dao\classes

[javac] Compiling 16 source files to E:\dudu\build\dao\classes

[javac] Note: E:\dudu\src\dao\org\dudu\model\User.java uses unchecked or uns

afe operations.

[javac] Note: Recompile with -Xlint:unchecked for details.

package-dao:

[copy] Copying 1 file to E:\dudu\build\dao\gen

[jar] Building jar: E:\dudu\dist\dudu-dao.jar

db-prepare:

generate.database.properties:

[echo] generating database.properties from build.properties

[propertyfile] Creating new property file: E:\dudu\database.properties

[schemaexport] (cfg.Environment 478 ) Hibernate 2.1.7

[schemaexport] (cfg.Environment 507 ) hibernate.properties n

ot found

[schemaexport] (cfg.Environment 538 ) using CGLIB reflection

optimizer

[schemaexport] (cfg.Environment 567 ) using JDK 1.4 java.sql

.Timestamp handling

[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\Person.hbm.xml

[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud

u.model.Person -> person

[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\Role.hbm.xml

[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud

u.model.Role -> role

[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\User.hbm.xml

[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud

u.model.User -> app_user

[schemaexport] (cfg.Binder 572 ) Mapping collection: or

g.dudu.model.User.roles -> user_role

[schemaexport] (cfg.Configuration 169 ) Mapping file: E:\dudubuild\dao\gen\org\dudu\model\UserCookie.hbm.xml

[schemaexport] (cfg.Binder 230 ) Mapping class: org.dud

u.model.UserCookie -> user_cookie

[schemaexport] (dialect.Dialect 86 ) Using dialect: net.sf.

hibernate.dialect.MySQLDialect

[schemaexport] (cfg.Configuration 632 ) processing one-to-many

association mappings

[schemaexport] (cfg.Configuration 641 ) processing one-to-one

association property references

[schemaexport] (cfg.Configuration 666 ) processing foreign key

constraints

[schemaexport] (cfg.Configuration 632 ) processing one-to-many

association mappings

[schemaexport] (cfg.Configuration 641 ) processing one-to-one

association property references

[schemaexport] (cfg.Configuration 666 ) processing foreign key

constraints

[schemaexport] (hbm2ddl.SchemaExport 98 ) Running hbm2ddl schema

export

[schemaexport] (hbm2ddl.SchemaExport 112 ) writing generated sche

ma to file: create-tables.sql

[schemaexport] (hbm2ddl.SchemaExport 117 ) exporting generated sc

hema to database

[schemaexport] (connection.DriverManagerConnectionProvider 42 ) Using Hibernate

built-in connection pool (not for production use!)

[schemaexport] (connection.DriverManagerConnectionProvider 43 ) Hibernate conne

ction pool size: 20

[schemaexport] (connection.DriverManagerConnectionProvider 77 ) using driver: c

om.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/dudu?autoReconnect=true&useU

nicode=true&characterEncoding=utf-8

[schemaexport] (connection.DriverManagerConnectionProvider 78 ) connection prop

erties: {user=test, password=test, show_sql=true}

[schemaexport] alter table user_role drop foreign key FK143BF46A14048CB4;

[schemaexport] alter table user_role drop foreign key FK143BF46AF02988D6;

[schemaexport] drop table if exists user_role;

[schemaexport] drop table if exists app_user;

[schemaexport] drop table if exists person;

[schemaexport] drop table if exists user_cookie;

[schemaexport] drop table if exists role;

[schemaexport] create table user_role (

[schemaexport] username varchar(20) not null,

[schemaexport] role_name varchar(20) not null,

[schemaexport] primary key (username, role_name)

[schemaexport] );

[schemaexport] create table app_user (

[schemaexport] username varchar(20) not null,

[schemaexport] version integer not null,

[schemaexport] password varchar(255) not null,

[schemaexport] first_name varchar(50) not null,

[schemaexport] last_name varchar(50) not null,

[schemaexport] address varchar(150),

[schemaexport] city varchar(50) not null,

[schemaexport] province varchar(100),

[schemaexport] country varchar(100),

[schemaexport] postal_code varchar(15) not null,

[schemaexport] email varchar(255) not null unique,

[schemaexport] phone_number varchar(255),

[schemaexport] website varchar(255),

[schemaexport] password_hint varchar(255),

[schemaexport] primary key (username)

[schemaexport] );

[schemaexport] create table person (

[schemaexport] id bigint not null,

[schemaexport] first_name varchar(50) not null,

[schemaexport] last_name varchar(50) not null,

[schemaexport] primary key (id)

[schemaexport] );

[schemaexport] create table user_cookie (

[schemaexport] id bigint not null,

[schemaexport] username varchar(30) not null,

[schemaexport] cookie_id varchar(100) not null,

[schemaexport] date_created datetime not null,

[schemaexport] primary key (id)

[schemaexport] );

[schemaexport] create table role (

[schemaexport] name varchar(20) not null,

[schemaexport] version integer not null,

[schemaexport] description varchar(255),

[schemaexport] primary key (name)

[schemaexport] );

[schemaexport] alter table user_role add index FK143BF46A14048CB4 (role_name), a

dd constraint FK143BF46A14048CB4 foreign key (role_name) references role (name);

[schemaexport] alter table user_role add index FK143BF46AF02988D6 (username), ad

d constraint FK143BF46AF02988D6 foreign key (username) references app_user (user

name);

[schemaexport] create index user_cookie_username_cookie_id on user_cookie (usern

ame, cookie_id);

[schemaexport] (hbm2ddl.SchemaExport 160 ) schema export complete

[schemaexport] (connection.DriverManagerConnectionProvider 143 ) cleaning up con

nection pool: jdbc:mysql://localhost/dudu?autoReconnect=true&useUnicode=true&cha

racterEncoding=utf-8

db-load:

(connection.DriverManagerConnectionProvider 143 ) cleaning up connection pool: j

dbc:mysql://localhost/dudu?autoReconnect=true&useUnicode=true&characterEncoding=

utf-8

[dbunit] Executing operation: CLEAN_INSERT

[dbunit] on file: E:\dudu\metadata\sql\sample-data.xml

[dbunit] with format: xml

setup-db:

BUILD SUCCESSFUL

Total time: 1 minute 6 seconds

Ø 生成的person表

mysql> desc person;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| id | bigint(20) | | PRI | 0 | |

| first_name | varchar(50) | | | | |

| last_name | varchar(50) | | | | |

+------------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

Ø 生成的Person..hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class

name="org.dudu.model.Person"

table="person"

dynamic-update="false"

dynamic-insert="false"

select-before-update="false"

optimistic-lock="version"

>

<id

name="id"

column="id"

type="java.lang.Long"

unsaved-value="null"

>

<generator class="increment">

<!--

To add non XDoclet generator parameters, create a file named

hibernate-generator-params-Person.xml

containing the additional parameters and place it in your merge dir.

-->

</generator>

</id>

<property

name="firstName"

type="java.lang.String"

update="true"

insert="true"

access="property"

column="first_name"

length="50"

not-null="true"

/>

<property

name="lastName"

type="java.lang.String"

update="true"

insert="true"

access="property"

column="last_name"

length="50"

not-null="true"

/>

<!--

To add non XDoclet property mappings, create a file named

hibernate-properties-Person.xml

containing the additional properties and place it in your merge dir.

-->

</class>

</hibernate-mapping>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有