最近看了满江红的seam文档,照着写了个例子。
首先,使用seam setup进行配置:
E:\sxb\seam\seam>seam setup
Buildfile: E:\sxb\seam\seam\seam-gen\build.xml
init:
setup:
[echo] Welcome to seam-gen :-)
[input] Enter your Java project workspace (the directory that contains your Seam projects) [d:/
workspace] [d:/workspace]
[input] Enter your JBoss home directory [e:/sxb/seam/jboss] [e:/sxb/seam/jboss]
[input] Enter the project name [seam_crud] [seam_crud]
[echo] Accepted project name as: seam_crud
[input] Do you want to use ICEfaces instead of RichFaces [n] (y, [n])
[input] skipping input as property icefaces.home.new has already been set.
[input] Select a RichFaces skin [blueSky] ([blueSky], classic, ruby, wine, deepMarine, emeraldT
own, japanCherry, DEFAULT)
[input] Is this project deployed as an EAR (with EJB components) or a WAR (with no EJB support)
[ear] ([ear], war)
[input] Enter the Java package name for your session beans [xiaobin.shi.session] [xiaobin.shi.s
ession]
[input] Enter the Java package name for your entity beans [xiaobin.shi.entity] [xiaobin.shi.ent
ity]
[input] Enter the Java package name for your test cases [xiaobin.shi.test] [xiaobin.shi.test]
[input] What kind of database are you using? [mysql] (hsql, [mysql], oracle, postgres, mssql, d
b2, sybase, enterprisedb, h2)
[input] Enter the Hibernate dialect for your database [org.hibernate.dialect.MySQLDialect] [org
.hibernate.dialect.MySQLDialect]
[input] Enter the filesystem path to the JDBC driver jar [e:\sxb\seam\mysql-connector-java-5.1.
6-bin.jar] [e:\sxb\seam\mysql-connector-java-5.1.6-bin.jar]
[input] Enter JDBC driver class for your database [com.mysql.jdbc.Driver] [com.mysql.jdbc.Drive
r]
[input] Enter the JDBC URL for your database [jdbc:mysql:///mysql] [jdbc:mysql:///mysql]
这里我使用mysql数据库
[input] Enter database username [root] [root]
[input] Enter database password [root] [root]
[input] skipping input as property hibernate.default_schema.new has already been set.
[input] Enter the database catalog name (it is OK to leave this blank) [mysql] [mysql]
[input] Are you working with tables that already exist in the database? [y] ([y], n)
[input] Do you want to drop and recreate the database tables and data in import.sql each time y
ou deploy? [n] (y, [n])
[delete] Deleting: E:\sxb\seam\seam\seam-gen\build.properties
[propertyfile] Creating new property file: E:\sxb\seam\seam\seam-gen\build.properties
[echo] Installing JDBC driver jar to JBoss server
[echo] Type 'seam create-project' to create the new project
BUILD SUCCESSFUL
Total time: 35 seconds
第二步:创建工程。使用 seam create-project
第三步:新建entity.
seam new-entity
输入类名后,会创建几个文件。我输入的是MyUser,所以生成了MyUser.java,MyUserHome.java,MyUserList.java,myUser.xhtml , myUserList.xhtml
第四步:在eclpise中打开这个工程,根据数据库的表结构修改实体BEAN MyUser.java
例如,我对应的表是my_user
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| user_id | varchar(20) | NO | PRI | NULL | |
| user_name | varchar(20) | YES | | NULL | |
| password | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
那么MyUser.java中就必须指明实题BEAN对应的表:
@Table(name="my_user")
第四步:修改MyUserHome.java
@RequestParameter
String myUserId;
我的表的主键是字符型的。
第五步:修改myUser.xhtml , myUserList.xhtml
因为自动生成的页面可能和你的实体BEAN中的字段不同,需要自己手动修改一下。
发布到jboss后,就可以看到效果了。不需要你写什么代码,就能实现my_user的CRUD,感觉挺方便的。