作为初学者本人根据转载的那篇文章摸索了一段时间才制作了一个简单的Demo,发觉S2dao难到不难,对于初学者来说,主要是配置,如果能够配置成功,基本上就对S2dao有个基本了解,能够做更深入的学习了。
以下是我个人的Demo制作过程。
1.S2dao 文件下载,请在Google 搜索自行下载。
2.下载S2dao文件后,在Eclipse里自己新建一个Java project : S2daoH2Test
3.数据库采用H2,如果想采用其它的数据库,请参考官方的jdbc.dicon。
工程文件有下载的,不过Lib文件太大,不上传了。请自行到关方网站下载。
(1)文件结构
(2)库文件,我把源文件库也引用了,实际中不需要引用源文件库
(3)建立java bean
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import org.seasar.dao.annotation.tiger.Bean;
@Bean(table = " user " )
public class UserEntity {
// public static final String TABLE = "user";
private String id;
private String name;
public String getId()
{
return this .id;
}
public void setId(String id)
{
this .id = id;
}
public String getName()
{
return this .name;
}
public void setName(String name)
{
this .name = name;
}
}
(4) S2dao文件,这个文件主要是对javabean的操作
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import java.util.List;
import org.seasar.dao.annotation.tiger.Arguments;
import org.seasar.dao.annotation.tiger.Sql;
import org.seasar.dao.annotation.tiger.Query;
import org.seasar.dao.annotation.tiger.S2Dao;
import db.entity.UserEntity;
@S2Dao(bean = UserEntity. class )
public interface UserDao {
int insert(UserEntity userEntity);
@Query( " name = /*name*/ " )
@Arguments({ " name " })
UserEntity getByName(String name);
@Query( " id = ? " )
UserEntity getById(String id);
int update(UserEntity userEntity);
int delete(UserEntity userEntity);
@Query( " id = /*id*/ " )
@Arguments({ " id " })
int deleteById(String id);
List < UserEntity > getAll();
}
(5)配置文件 ,请注意参考图片的文件夹格式配置。不然会出错误. j2ee.dicon在UserDao.dicon的上层目录。
1> UserDao.dicon
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<! DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd" >
< components >
< include path ="dao.dicon" />
< component class ="db.dao.UserDao" >
< aspect > dao.interceptor </ aspect >
</ component >
</ components >
2> j2ee.dicon
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<! DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN"
"http://www.seasar.org/dtd/components21.dtd" >
< components namespace ="j2ee" >
< component name ="transactionManager"
class ="org.seasar.extension.jta.TransactionManagerImpl" />
< component name ="requiredTx"
class ="org.seasar.extension.tx.RequiredInterceptor" />
< component name ="requiresNewTx"
class ="org.seasar.extension.tx.RequiresNewInterceptor" />
< component name ="mandatoryTx"
class ="org.seasar.extension.tx.MandatoryInterceptor" />
< component name ="notSupportedTx"
class ="org.seasar.extension.tx.NotSupportedInterceptor" />
< component
name ="resultSetFactory"
class ="org.seasar.dao.pager.PagerResultSetFactoryWrapper" >
< arg >
< component class ="org.seasar.extension.jdbc.impl.BasicResultSetFactory" />
</ arg >
< property name ="useScrollCursor" > true </ property >
</ component >
<!--
<component
name="resultSetFactory"
class="org.seasar.dao.pager.PagerResultSetFactoryLimitOffsetWrapper">
<arg>
<component class="org.seasar.extension.jdbc.impl.BasicResultSetFactory"/>
</arg>
<arg>dataSource</arg>
</component>
-->
< component class ="org.seasar.extension.jdbc.impl.ConfigurableStatementFactory" >
< arg >
< component class ="org.seasar.dao.pager.PagerStatementFactory" />
</ arg >
< property name ="fetchSize" > 100 </ property >
<!--
<property name="maxRows">100</property>
-->
</ component >
<!--
<component class="org.seasar.extension.jdbc.impl.BasicResultSetFactory"/>
<component class="org.seasar.extension.jdbc.impl.ConfigurableStatementFactory">
<arg>
<component class="org.seasar.extension.jdbc.impl.BasicStatementFactory"/>
</arg>
<property name="fetchSize">100</property>
<! - -
<property name="maxRows">100</property>
- - >
</component>
<component class="org.seasar.extension.jdbc.impl.OracleResultSetFactory"/>
<component class="org.seasar.extension.jdbc.impl.BooleanToIntStatementFactory"/>
-->
< component name ="xaDataSource"
class ="org.seasar.extension.dbcp.impl.XADataSourceImpl" >
< property name ="driverClassName" >
"org.hsqldb.jdbcDriver"
</ property >
< property name ="URL" >
<!--
"jdbc:hsqldb:hsql://localhost:9001"
-->
<!--
"jdbc:hsqldb:file:D:/Eclipse/WorkspaceS2DaoTest/S2DaoH2Test/java/examples/dao/demo"
-->
"jdbc:h2:c:/s2db/test"
</ property >
< property name ="user" > "sa" </ property >
< property name ="password" > "sa" </ property >
</ component >
< component name ="connectionPool"
class ="org.seasar.extension.dbcp.impl.ConnectionPoolImpl" >
< property name ="timeout" > 600 </ property >
< property name ="maxPoolSize" > 10 </ property >
< property name ="allowLocalTx" > true </ property >
< destroyMethod name ="close" />
</ component >
< component name ="dataSource"
class ="org.seasar.extension.dbcp.impl.DataSourceImpl" />
</ components >
3> log4j.properties配置, 显示运行日志信息
log4j.category.org.seasar=DEBUG, C
log4j.additivity.org.seasar=false
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p %d [%t] %m%n
(6)数据库准备,网上很多教程都没写这一步,数据库都没有,怎么能够访问数据库呢。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.tools.DeleteDbFiles;
public class DbInit {
public static final String PATHSEPERATECHAR = " / " ;
public static final String DATABASEDIR = " c:/s2db " ;
public static final String DRIVER = " jdbc:h2: " ;
public static final String DATABASENAME = " test " ;
public static final String DATABASE = DRIVER + DATABASEDIR + " / " + DATABASENAME;
public static final String USER = " sa " ;
public static final String PWD = " sa " ;
public static void main(String[] a) throws Exception {
DeleteDbFiles.execute(DATABASEDIR, DATABASENAME, true );
Class.forName( " org.h2.Driver " );
Connection conn = DriverManager.getConnection(DATABASE, USER, PWD);
Statement stat = conn.createStatement();
String sql = " CREATE TABLE user(id VARCHAR(8) NOT NULL PRIMARY KEY,name VARCHAR(16)) " ;
stat.execute(sql);
conn.close();
}
}
(7)测试
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import java.util.List;
import java.util.ArrayList;
import db.dao.UserDao;
import db.entity.UserEntity;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
public class UserTest {
public static final String CONFIG_PATH = " dicon/UserDao.dicon " ;
public void test(){
UserDao userDao = getComponent(UserDao. class );
UserEntity user = new UserEntity();
// test insert
user.setId( " 1 " );
user.setName( " MyName " );
userDao.insert(user);
// test get
UserEntity newUser = new UserEntity();
// newUser = userDao.getById("1");
newUser = userDao.getByName( " MyName " );
System.out.println(newUser.getId() + " - " + newUser.getName());
// test delete
userDao.deleteById( " 1 " );
// test getAll
user.setId( " 1 " );
user.setName( " MyName " );
userDao.insert(user);
user.setId( " 2 " );
user.setName( " MyName2 " );
userDao.insert(user);
List < UserEntity > users = new ArrayList < UserEntity > () ;
users = userDao.getAll();
for (UserEntity userEntity : users) {
System.out.println( " getall: " + userEntity.getId() + " - " + userEntity.getName());
}
userDao.deleteById( " 1 " );
userDao.deleteById( " 2 " );
}
public static void main(String[] a){
UserTest userTest = new UserTest();
userTest.test();
}
public static < T > T getComponent( final Class < T > clazz) {
SingletonS2ContainerFactory.setConfigPath(CONFIG_PATH);
SingletonS2ContainerFactory.init();
final S2Container container = SingletonS2ContainerFactory.getContainer();
final Object o = container.getComponent(clazz);
final T bean = clazz.cast(o);
return bean;
}
}
测试结果如下
DEBUG 2010-06-22 16:47:08,093 [main] Physical connection got
DEBUG 2010-06-22 16:47:08,093 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,093 [main] Logical connection closed
DEBUG 2010-06-22 16:47:08,093 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,156 [main] Logical connection closed
DEBUG 2010-06-22 16:47:08,171 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,171 [main] INSERT INTO user (id, name) VALUES ('1', 'MyName')
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,187 [main] SELECT user.id, user.name FROM user WHERE name = 'MyName'
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
1 - MyName
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,187 [main] DELETE FROM user WHERE id = '1'
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,187 [main] INSERT INTO user (id, name) VALUES ('1', 'MyName')
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,187 [main] INSERT INTO user (id, name) VALUES ('2', 'MyName2')
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,187 [main] SELECT user.id, user.name FROM user
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
getall:1 - MyName
getall:2 - MyName2
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,187 [main] DELETE FROM user WHERE id = '1'
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection got
DEBUG 2010-06-22 16:47:08,187 [main] DELETE FROM user WHERE id = '2'
DEBUG 2010-06-22 16:47:08,187 [main] Logical connection closed
-----------------------------
项目文件下载: