WEB-INFディレクトリに設定ファイルを作成します。以下の2つのXMLファイルを配置してください。
web.xml設定
<filter>
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter>
struts2
/*
<listener>
org.springframework.web.context.ContextLoaderListener
</listener>
index.jsp
applicationContext.xml設定
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/myssh?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/school/entity/Course.hbm.xml</value>
</list>
</property>
</bean>
<bean id="courseDAO" class="com.school.dao.CourseDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="courseService" class="com.school.service.CourseServiceImpl">
<property name="courseDAO" ref="courseDAO"/>
</bean>
<bean id="courseAction" class="com.school.action.CourseAction">
<property name="courseService" ref="courseService"/>
</bean>
</beans>
Hibernateマッピング定義
<class name="com.school.entity.Course" table="course">
<id name="courseId" type="java.lang.Integer">
<column name="COURSE_ID" precision="22" scale="0"/>
<generator class="identity"/>
</id>
<property name="courseName" type="java.lang.String">
<column name="COURSE_NAME" length="50" not-null="true"/>
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" length="500" not-null="false"/>
</property>
</class>
エンティティクラス定義
package com.school.entity;
public class Course {
private int courseId;
private String courseName;
private String description;
public Course() {}
public Course(int courseId, String courseName, String description) {
this.courseId = courseId;
this.courseName = courseName;
this.description = description;
}
// 以下getter/setter省略
}
コンポーネント依存関係
- CourseDAOImplはsessionFactoryをDI
- CourseServiceImplはcourseDAOをDI
- CourseActionはcourseServiceをDI