Connecting a Spring Starter Project with MyBatis is not complicated. If you want to learn how to create a Spring Starter Project, please refer to my last post linked below.
https://www.agilemeadow.com/entry/Spring-Boot-Basics-of-Spring-Boot-Lombok
To connect MyBatis with Spring Boot, we must create a table.
Create a sql file in webapp/ sql and create a table and sequence.
create table boardtest(
no number primary key,
name varchar2(20),
subject varchar2(50),
content varchar2(1000),
register date
);
create sequence boardtest_seq;
select * from tab;
select * from seq;
Configuration
The biggest difference between Spring and Spring Boot is configuration files. Spring has six configuration files, whereas, Spring Boot has four.
The configuration has to be done in main/resource/application.properties.
# port
server.port=80
# view resolver
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
# oracle
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=spring
spring.datasource.password=spring123
# mybatis
mybatis.config-location=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
After setting the location of the view files, create WEB-INF and views folders.
Create folders for each roles.
In the folders, you will put these classes:
config - database connection class
controller - controller class
dao - DAO class
model - DTO class
service - service class
DataAccessConfig.java
package com.example.demo.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
@PropertySource("classpath:/application.properties")
public class DataAccessConfig {
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
SqlSessionFactoryBean factoryBean=new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*.xml")
);
factoryBean.setTypeAliasesPackage("com.example.demo.model"); // DTO Alias 설정
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
board.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="boardns">
<insert id="insert" parameterType="board">
insert into boardtest values(boardtest_seq.nextval,#{name},#{subject},#{content},sysdate)
</insert>
<select id="list" resultType="board">
select * from boardtest
</select>
</mapper>
Now, it is all set. What you are going to do is to create your project by yourself.
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>
location.href="boardform";
</script>
</body>
</html>
BoardController.java
@RequestMapping("boardform")
public String main() {
System.out.println("controller in");
return "boardform";
}
boardform.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>New Post</title>
</head>
<body>
<form method="post" action="boardInsert">
<table border=1 align=center width=500>
<caption>New Post</caption>
<tr>
<td>Writer</td>
<td><input type=text name="name"></td>
</tr>
<tr>
<td>Title</td>
<td><input type=text name="subject"></td>
</tr>
<tr>
<td>Content</td>
<td><textarea cols="50" rows="5" name="content"></textarea></td>
</tr>
<tr>
<td colspan=2 align="center">
<input type=submit value="Submit">
<input type=reset value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
boardlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Post List</title>
</head>
<body>
<a href="boardform">New Post</a> <br>
<table border=1 align=center width=800>
<caption>Post List</caption>
<tr>
<th>No.</th>
<th>Writer</th>
<th>Title</th>
<th>Date</th>
<th>Content</th>
</tr>
<c:forEach var="b" items="${list }">
<tr>
<td>${b.no}</td>
<td>${b.name}</td>
<td>${b.subject}</td>
<td>
<fmt:formatDate value="${b.register}" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
<td>${b.content}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
'Spring & Spring Boot' 카테고리의 다른 글
Spring Boot) Thymeleaf (0) | 2022.10.19 |
---|---|
Spring Boot) Basics of Spring Boot /Lombok (0) | 2022.10.18 |
Spring) How to start a new Spring Framework Project[2] (0) | 2022.10.14 |
Spring) How to start a new Spring Framework Project[1] (0) | 2022.10.11 |
Spring) Spring with MyBatis / Configuration (0) | 2022.10.08 |