공부/예제 실습

Spring (Mybatis)

726582776982 2022. 5. 10. 14:25

설정 (Root-context)

 

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
32
33
34
35
36
37
<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">   
    <!-- ** Mybatis 설정  ** 
   => DBConnection (MySql) 
      - allowPublicKeyRetrieval=true : local DB open 하지 않아s도 connection 허용
   => SqlSessionFactory 
      - MyBatis 와 연동시 DB 연결과 SQL 실행의 핵심 클래스
      - SqlSessionFactoryBean 에 의해 생성됨  
   => base-package 등록 : DAO 필요 없이 interface ~Mapper.java 사용을 위한 경로 맞춰주는 설정    
   &amp;allowPublicKeyRetrieval=true-->
   <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/mydb?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true" />
      <property name="username" value="root" />
      <property name="password" value="mysql" />
   </bean>
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
      <!-- 
      <property name="mapperLocations" value="classpath:/mappers/**/*Mapper.xml" />
      interface 방식으로 적용하면 필요없음 -->
   </bean>
   <mybatis-spring:scan base-package="mapperInterface"/>
    
    <!-- Root Context: defines shared resources visible to all other web components -->
        <!-- JUnit Spring Test -->
<!--    <bean class="util_DB.BoardDAO" lazy-init="true" /> -->
<!--    <bean class="vo.BoardVO" lazy-init="true" /> -->
    
</beans>
 
cs

Web.xml

 

1
2
3
4
5
6
7
8
9
10
<!-- 한글처리 필터 -->
   <filter>
      <filter-name>characterEncoding</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>
   </filter>
cs

boardMapper.xml

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
32
33
34
<?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="mapperInterface.MemberMapper">
      <select id="selectList" resultType="vo.MemberVO">
          select id,password,name,
          concat(lev,if(lev = 'a','관리자',if(lev ='b','나무',if(lev = 'c','잎새',if(lev='d','새싹','일반'))))) as 'lev',
          birthd,point,weight 
          from member 
          order by id
      </select>
      <select id="selectOne" resultType="vo.MemberVO">
          select id,password,name,
        concat(lev,if(lev = 'a','관리자',if(lev ='b','나무',if(lev = 'c','잎새',if(lev='d','새싹','일반'))))) as 'lev',
        birthd,point,weight 
        from member
        where id =#{id}
      </select>
      <!-- #{id} : parameter 로 전달된 vo 의 id 라는 컬럼의 value 가 전달됨 -->
      <insert id="insert">
          insert into member values(#{id},#{password},#{name},#{lev},#{birthd},#{point},#{weight})
      </insert>
      <update id="update">
          update member set password = #{password}, name = #{name}, lev = #{lev}, birthd = #{birthd}, point = #{point}, weight =#{weight} where id = #{id}
      </update>
      <delete id="delete">
          delete from member where id=#{id}
      </delete>
  </mapper>
  
  <!--  
** interface 를 통해 ~Mapper 접근
=> interface ~Mapper.java 의 패키지명과 화일명과  
   ~Mapper.xml 의 패키지명, 화일명, namespace 값은 같아야함. -->
cs

BoardMapper.java (interface)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package mapperInterface;
 
import java.util.List;
 
import vo.BoardVO;
 
public interface BoardMapper {
    List<BoardVO> SelectList();
 
    BoardVO SelectOne(BoardVO vo);
 
    int countUp(BoardVO vo);
 
    int goodupdate(BoardVO vo);
 
    int badupdate(BoardVO vo);
 
    int delete(BoardVO vo);
 
    int insert(BoardVO vo);
 
    int boardupdate(BoardVO vo);
}
 
cs