1. MyBatis
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
2. MyBatis Spring
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
3. root-context.xml 내용 추가
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml" />
<property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
4. src/main/resources 파일 추가 (mybatis-config.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org/DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
5. src/main/resources 폴더와 파일 추가 (Mapper.xml)
ex)
1) mappers -> com -> ex -> exercise -> board -> mapper 순으로 폴더 생성
2) Mapper.xml 생성
BoardMapper.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="com.ex.exercise.board.mapper">
<insert id = "insertBoard">
INSERT INTO board
(
id
, title
, contents
)
VALUES
(
#{id}
, #{title}
, #{contents}
)
</insert>
</mapper>
'Spring' 카테고리의 다른 글
[Spring] enum(이늄) 사용자 레벨 관리 기능 (0) | 2022.06.15 |
---|---|
[Spring] @Controller, @RestController의 차이 (0) | 2022.06.14 |
[Spring] StringUtils 예제 (0) | 2022.06.08 |
[Spring] mariadb spring datasource 적용 (0) | 2022.06.07 |
[Spring] 로그, 한글 filter 적용, RequestMapping 관련, ajax (0) | 2022.06.03 |