Spring Framework - Basic CRUD Implementation Part 1
Setting Up the Project
A new Spring 5.0.7 web project is created in Eclipse, after which the following dependencies are added:
- Spring TestContext Framework
- Spring JDBC
- Spring Transaction
- HikariCP
- MyBatis
- MyBatis Spring
- Log4Jdbc
- Lombok
- MySQL Connector
Configuation
Configure Log4Jdbc and and the root-context file. MyBatis should target the mapper file in the project.
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
<property name="jdbcUrl" value="jdbc:log4jdbc:oracle:thin:@localhost:(DB)"></property>
<property name="username" value=""></property>
<property name="password" value=""></property>
</bean>
<!-- HikariCP configuration -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig"></constructor-arg>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
Add the encoding filter if necessary in web.xml.
Database Setup
Create a table using standard SQL language:
create table table(
no not null,
post vachar2(100),
etc.
)
Create Data Class and Interfaces
Create a VO for the table in the main package. In the mapper package, create an interface for the mapper and the associated XML. The interface class contains the data objects. The XML should contain the SQL statement and targets:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ict.mapper.BoardMapper">
<select id="getList" resultType="com.ict.persistent.VO">
<![CDATA[
select * from tbl WHERE no<4
]]>
</select>
</mapper>
To be continued…
-gonkgonk