티스토리 뷰
728x90
반응형
개발 환경
java 21.0.4 2024-07-16 LTSJava(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)
Tomcatv 10.1
EclipseVersion: 2024-06 (4.32.0)Build id: 20240606-1231
pom.xml 설정
Spring Web MVC 버전 & Mybatis 버전 두 버전이 서로 맞지 않으면 빌드시 오류가 발생한다.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>21</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Spring dependencies -->
<!--https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.11</version>
</dependency>
<!--https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.11</version>
</dependency>
<!-- JSTL dependencies -->
<!--https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api -->
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.web/jakarta.servlet.jsp.jstl -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
<!-- MyBatis dependencies -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.1.11</version>
</dependency>
</dependencies>
</project>
web.xml 설정
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
id="WebApp_ID" version="5.0">
<display-name>com.thumbnail</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<!-- ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 한글 깨짐 설정(텍스트 인코딩) -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
applicationContext.xml 설정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- MyBatis DataSource -->
<bean id="dataSourcForMyBatis"
class="org.apache.ibatis.datasource.pooled.PooledDataSource">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@localhost:1521/xe" />
<property name="username" value="system" />
<property name="password" value="1234" />
</bean>
<!-- MyBatis SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourcForMyBatis" />
<property name="configLocation" value="classpath:mybatis/mybatisConfig.xml" />
<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mybatis.mapper" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactoryBean" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourcForMyBatis" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
dispatcherServlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- spring MVC annotation(@)을 사용하기 위한 설정 -->
<mvc:annotation-driven />
<!-- controller 를 처리할 view 위치 및 확장자명 설정 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- base-package 속성에 지정된 패키지와 그 하위 패키지에 있는 모든 클래스를 스캔합니다. -->
<context:component-scan base-package="com.mybatis" />
</beans>
mybatisConfig.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>
<typeAliases>
<typeAlias type="com.mybatis.model.MemberVO" alias="memberVO" />
</typeAliases>
</configuration>
MemberMapper.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.mybatis.mapper.MemberMapper">
<resultMap type="memberVO" id="memberResult">
<result property="id" column="id" />
<result property="pwd" column="pwd" />
</resultMap>
<select id="selectAllMemberList" resultMap="memberResult">
select * from t_member
</select>
<select id="getMember" parameterType="memberVO"
resultMap="memberResult">
select * from t_member
<where>
<if test="id != '' and id != null">
id = #{ id }
</if>
</where>
</select>
<update id="updateBalance1">
update cust_account
set balance = balance - 10
where accountNo = '89'
</update>
<update id="updateBalance2">
update cust_account
set balance = balance + 10
where accountNo = dfsfdsf90
</update>
<insert id="insertUser" parameterType="com.example.User">
INSERT INTO users (name, email)
VALUES (#{name}, #{email})
</insert>
</mapper>
MemberMapper.java
package com.mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.mybatis.model.MemberVO;
@Mapper
public interface MemberMapper {
List<MemberVO> selectAllMemberList();
void updateBalance1();
void updateBalance2();
}
MemberRepository.java
package com.mybatis.repository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.mybatis.mapper.MemberMapper;
import com.mybatis.model.MemberVO;
@Repository
public class MemberRepository {
@Autowired
private MemberMapper memberMapper;
public List<MemberVO> memberList() {
return this.memberMapper.selectAllMemberList();
}
public void updateBalance1() {
this.memberMapper.updateBalance1();
}
public void updateBalance2() {
this.memberMapper.updateBalance2();
}
}
Member.java, MemberService.java
package com.mybatis.service;
import java.util.List;
import com.mybatis.model.MemberVO;
public interface Member {
List<MemberVO> memberList();
void transactionTest() throws Exception;
}
package com.mybatis.service;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mybatis.model.MemberVO;
import com.mybatis.repository.MemberRepository;
@Service
public class MemberService implements Member {
@Autowired
MemberRepository memberRepository;
@Autowired
SqlSessionTemplate sqlSessionTemplate;
@Override
public List<MemberVO> memberList() {
return this.memberRepository.memberList();
}
@Override
public void transactionTest() throws Exception {
this.memberRepository.updateBalance1();
this.memberRepository.updateBalance2();
}
}
MemberController.java
package com.mybatis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.mybatis.model.MemberVO;
import com.mybatis.service.MemberService;
@Controller
@RequestMapping("/mybatis/*")
public class MybatisController {
@Autowired
MemberService memberService;
@GetMapping("mybatis")
public ModelAndView mybatis() {
ModelAndView modelAndView = new ModelAndView("/mybatis/mybatis");
modelAndView.addObject("memberList", this.memberService.memberList());
for(MemberVO item: this.memberService.memberList()) {
System.out.println(item.getId());
}
return modelAndView;
}
}
MemberVO.java
package com.mybatis.model;
public class MemberVO {
private String id;
private String pwd;
public String getId() {
return this.id;
}
public MemberVO setId(String id) {
this.id = id;
return this;
}
public String getPwd() {
return this.pwd;
}
public MemberVO setPwd(String pwd) {
this.pwd = pwd;
return this;
}
}
동적 쿼리문
// if
<select ... >
select * from t_member
<where>
<if test="id != '' and id != null">
name = #{ name }
</if>
<if test="pwd != '' and pwd != null">
and pwd = #{ pwd }
</if>
</where>
</select>
// choose
<select ... >
<where>
<choose>
<when test="id != '' and id != null">
</when>
</choose>
</where>
</select>
// foreach
<select ... parameterType="java.util.Map" >
select * form t_member
where name in
<foreach item="item" collection="list" open="(" seperator="," close=")">
#{ item }
</foreach>
</select>
추가 정보
Oracle Database 사용시
스프링 프레임워크(Spring FrameWordk)-Spring Web MVC
728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 메이븐(maven)
- 스프링 시큐리티(spring security)
- 상품 등록
- java-개발 환경 설정하기
- java 키워드 정리
- jsp 오픈 소스
- 람다식(lambda expression)
- jstl(java standard tag library)-core
- nl2br
- MainActor
- system.io
- error-java
- java.sql
- React
- 인텔리제이(intellij)
- java web-mvc
- await
- 표현 언어(expression language)
- In App Purchase
- 문자 자르기
- 스프링 프레임워크(spring framewordk)
- jstl(java standard tag library)
- REST API
- 스프링 시큐리티(spring security)-http basic 인증
- .submit()
- 진수 변환
- 특정 문자를 기준으로 자르기
- System.Diagnostics
- 스프링 프레임워크(spring framework)
- 제품 등록
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함