Javas2dao Demo (原创)

作为初学者本人根据转载的那篇稿子摸索了一段时间才打了一个简短的Demo,发觉S2dao难到不麻烦,对于新家的话,主要是布,如果会配置成功,基本上就对S2dao有只中心了解,能够做还深入之学了。

 

以下是本人个人的Demo制作过程。

1.S2dao 文书下载,请以Google 搜索自行下载。

2.下载S2dao文件后,在Eclipse里团结新建一个Java project : S2daoH2Test

3.数据库采用H2,如果想使用其他的数据库,请参见官方的jdbc.dicon。

 

工程文件发出下载的,不过Lib文件太死,不达标传了。请自行到关方网站下载。

(1)文件结构

Java 1 

 

 (2)库文件,我拿来自文件库也引用了,实际中无需引用源文件库

Java 2 

 

(3)建立java bean

 

Java 3Java 4代码

package db.entity;

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的操作

 

Java 5Java 6代码

package db.dao;
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

             

Java 7Java 8代码

<?xml version=”1.0″ encoding=”Shift_JIS”?>
<!DOCTYPE components PUBLIC “-//SEASAR//DTD S2Container//EN”
“http://www.seasar.org/dtd/components.dtd"&gt;
<components>
    <include path=”dao.dicon”/>
    <component class=”db.dao.UserDao”>
        <aspect>dao.interceptor</aspect>
    </component>
</components>

   2> j2ee.dicon

 

Java 9Java 10代码

<?xml version=”1.0″ encoding=”Shift_JIS”?>
<!DOCTYPE components PUBLIC “-//SEASAR2.1//DTD S2Container//EN”
    “http://www.seasar.org/dtd/components21.dtd"&gt;
<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)数据库准备,网上广大课程都尚未写就同步,数据库都并未,怎么能访问数据库也

  

Java 11Java 12代码

package Prepare;

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)测试

 

Java 13Java 14代码

package db.test;

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


类型文件下充斥:

 

 带库文件的类文件,文件较深8.5M左右

 

 匪牵动库文件的型来文件,比较小。

 

 

相关文章