[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

goal

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

pom.xml


        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.16</version>
        </dependency>
        <dependency>
          <groupId>org.seasar.dao</groupId>
          <artifactId>s2-dao-tiger</artifactId>
          <version>1.0.49</version>
        </dependency>

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"
"http://www.seasar.org/dtd/components24.dtd">
<components>
    <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"/>
      </property>
      <initMethod name="addClassPattern">
        <arg>"org.seasar.sastruts.example.dao"</arg>
        <arg>".*Dao"</arg>
      </initMethod>
    </component>

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

</components>

Specify the dicon file created above in app.dicon

app.dicon


  <include path="MySQL.dicon"/>

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

jdbc.dicon


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

    <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 class="org.seasar.extension.jdbc.impl.ConfigurableStatementFactory">
      <arg>
        <component class="org.seasar.dao.pager.PagerStatementFactory"/>
      </arg>
      <property name="fetchSize">100</property>
    </component>

	<component name="xaDataSource"
		class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
		<property name="driverClassName">
			"com.mysql.cj.jdbc.Driver"
		</property>
		<property name="URL">
			"jdbc:mysql://localhost:3306/sastrutsdb?characterEncoding=UTF-8&amp;characterSetResults=UTF-8"
		</property>
		<property name="user">"sa"</property>
		<property name="password">"password"</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>

This completes the settings. The rest will be implemented.

Implementation

Entity creation

Balance


@Entity
@Table(name = "sample")
public class Balance {
	@Id
	@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)
	@Temporal(TemporalType.DATE)
	public Date createdAt;

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

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("id:").append(id);
		sb.append("name:").append(name);
		sb.append("created_ad:").append(createdAt);
		sb.append("updated_ad:").append(updatedAt);
		return sb.toString();
	}
}

Action creation

BalanceAction


public class BalanceAction {

	@Binding
	BalanceService balanceService;

	@Execute(validator=false)
	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

BalanceService


public class BalanceService {

	@Binding
	BalanceDao balanceDao;

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

Dao interface creation

BalanceDao


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

Run

$ 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.

reference

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