[JAVA] Initial settings until S2Dao can be used

I didn't find many articles about the initial settings of S2Dao, and I struggled a little, so I'll make a note for myself in the future.

Execution environment


Dao interface using S2Dao is automatically bound and SQL is executed.

DB initial settings

Store data in balance table

mysql> select * from sastrutsdb.balance;
| id | name         | amount | created_at          | updated_at |
|  1 |Taro Tanaka|  10000 | 2019-05-03 19:25:33 | NULL       |
|  2 |Hanako Sato|  20000 | 2019-05-03 23:48:40 | NULL       |
2 rows in set (0.00 sec)

pom & dicon file settings

pom file settings

Download mysql-connector-java-8.0.16.jar and s2-dao-1.0.49.jar



Maven Clean the project.

* Confirm that the target jar exists in WEB-INF / lib Automatically copied by setting copy-dependencies in pom </ font>

dicon file settings

Create dicon file to register dao interface in container The included dao.dicon is a dicon file that exists in s2-dao-1.0.49.jar

Specify the class of dao interface to be registered as a component in addClassPattern.

MySQL.dicon (any file name is OK)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
    <include path="dao.dicon"/>

    <component name="traceInterceptor" class="org.seasar.framework.aop.interceptors.TraceInterceptor"/>

    <component class="org.seasar.framework.container.autoregister.FileSystemComponentAutoRegister">
      <property name="autoNaming">
        <component class="org.seasar.framework.container.autoregister.DefaultAutoNaming"/>
      <initMethod name="addClassPattern">

    <component class="org.seasar.framework.container.autoregister.AspectAutoRegister">
      <property name="interceptor">dao.interceptor</property>
      <initMethod name="addClassPattern">


Specify the dicon file created above in app.dicon


  <include path="MySQL.dicon"/>

mysql settings and S2Dao component registration in jdbc.dicon User name and password are set appropriately


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
<components namespace="jdbc">
	<include path="jta.dicon"/>

    <component name="resultSetFactory" class="org.seasar.dao.pager.PagerResultSetFactoryWrapper">
        <component class="org.seasar.extension.jdbc.impl.BasicResultSetFactory"/>
      <property name="useScrollCursor">true</property>
    <component class="org.seasar.extension.jdbc.impl.ConfigurableStatementFactory">
        <component class="org.seasar.dao.pager.PagerStatementFactory"/>
      <property name="fetchSize">100</property>

	<component name="xaDataSource"
		<property name="driverClassName">
		<property name="URL">
		<property name="user">"sa"</property>
		<property name="password">"password"</property>

	<component name="connectionPool"
		<property name="timeout">600</property>
		<property name="maxPoolSize">10</property>
		<property name="allowLocalTx">true</property>
		<destroyMethod name="close"/>

	<component name="DataSource"


This completes the settings. The rest will be implemented.


Entity creation


@Table(name = "sample")
public class Balance {
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int id;

	@Column(name = "amount", nullable = false, unique = false)
	public long amount;
	@Column(name = "name", nullable = false, unique = false)
	public String name;

	@Column(name = "created_at", nullable = false, unique = false)
	public Date createdAt;

	@Column(name = "updated_at", nullable = true, unique = false)
	public Date updatedAt;

	public String toString() {
		StringBuilder sb = new StringBuilder();
		return sb.toString();

Action creation


public class BalanceAction {

	BalanceService balanceService;

	public String index() {
		List<Balance> balanceList = balanceService.findAll();
		System.out.println("BALANCE_SIZE:" + balanceList.size());
		StringBuilder responseText = new StringBuilder();

		for (Balance balance : balanceList) {
			responseText.append(balance.toString() + "\r\n");
		ResponseUtil.write(responseText.toString(), "text/html", "UTF-8");
		return null;

Service creation

Binding Dao file here


public class BalanceService {

	BalanceDao balanceDao;

	public List<Balance> findAll() {
		return balanceDao.findAll();

Dao interface creation


@S2Dao(bean = Balance.class)
public interface BalanceDao {
	public List<Balance> findAll();


$ curl "localhost:8080/sastruts/balance/"
id:1name:Taro Tanaka created_ad:2019-05-03 19:25:33.0updated_ad:null
id:2name:Hanako Sato created_ad:2019-05-03 23:48:40.0updated_ad:null

I was able to get the data safely.


Introduction to Seasar2 S2Dao S2Dao Reference

Recommended Posts

Initial settings until S2Dao can be used
Until ruby can be used on windows ...
centos7 Initial settings
EC2 initial settings
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
CentOS7 initial settings
Scala String can be used other than java.lang.String method
About the matter that hidden_field can be used insanely
Convenient shortcut keys that can be used in Eclipse
Summary of css selectors that can be used with Nookogiri
Create a page control that can be used with RecyclerView
Firebase-Realtime Database on Android that can be used with copy
[Question] Can nullif be used in the count function in JPQL?
Whether options can be used due to different Java versions