SqlSessionDaoSupport

SqlSessionDaoSupport는 SqlSession을 제공하는 추상클래스이다. getSqlSession()메서드를 호출해서 다음처럼 SQL을 처리하는 마이바티스 메서드를 호출하기 위해 사용할 SqlSessionTemplate을 얻을 수 있다.


public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {

  public User getUser(String userId) {

    return (User) getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);

  }

}

대개 MapperFactoryBean은 추가적인 코드가 필요없기 때문에 이 클래스를 선호한다. 하지만 DAO에서 마이바티스가 필요하지 않고 구현된 클래스가 필요하지 않을때만 유용하다.


SqlSessionDaoSupport는 sqlSessionFactory 와 sqlSessionTemplate 프로퍼티를 셋팅할 필요가 있다. 두개의 프로퍼티를 모두 셋팅하면 sqlSessionFactory는 무시된다.


SqlSessionDaoSupport의 하위클래스인 UserDaoImpl가 있다고 하면 스프링에서는 다음처럼 설정될 수 있다.


<bean id="userMapper" class="org.mybatis.spring.sample.mapper.UserDaoImpl">

  <property name="sqlSessionFactory" ref="sqlSessionFactory" />

</bean>

블로그 이미지

왕왕왕왕

,

  <!-- 인코딩 utf-8 -->

  

  <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>

</filter>

<filter-mapping>

        <filter-name>encodingFilter</filter-name>

        <url-pattern>/*</url-pattern>

</filter-mapping>

    <!-- END 인코딩 utf-8 -->


위에 web.xml에도 맵핑시켜주고

아래 jsp파일은 자신꺼에서 euc-kr을 몽땅 utf-8로 바꾸면 일단 ok




'JAVA > Spring 4.0' 카테고리의 다른 글

Spring4.0 mybatis에 SqlSessionDaoSupport  (0) 2016.07.13
Spring4.0 Mybatis $ , # 차이점  (0) 2015.10.01
Spring4.0 Mybatis 프로젝트 기본구조  (0) 2015.10.01
Spring4.0 Mybatis (pom.xml)  (0) 2015.10.01
Spring4.0 JDBCTemplate  (0) 2015.10.01
블로그 이미지

왕왕왕왕

,

1. #는 쿼리가 수행될 때, 다음과 같이 된다 


SELECT * FROM USER 

WHERE 

col = ?  


parameter : [값]


?에 bind된 값이 들어가게 된다. 



이 쿼리의 컴파일 된 내용을 재사용 할 수 있고, 파라미터에 따라 대입해주므로 효율적이다.

내부적으로 preparedStatement 객체에서 ? 에 들어갈 파라미터의 값을 set 해줌으로써 사용이 된다.

 

* preparedStatement는 한번 수행한 쿼리를 캐싱하는 객체



사용 용도 >>


#일 경우, 값에 사용한다.


myBatis : 컬럼명 = #{값}   

iBatis : 컬럼명 = #값#


* 쿼리에 작은 따옴표가 붙게 된다.



2. $는 쿼리가 수행될 때, 다음과 같이 된다(SQL injection 위험)


SELECT * FROM USER

WHERE 

col = 값



값이 넣어진 쿼리 자체로 수행이 된다.(상수)

즉, 문장 전체가 preparedStatement가 된다.


사용 용도 >>


$일 경우는 컬럼명이 동적으로 바뀌어야 할 때 사용한다. 또는 테이블명.


myBatis : ${컬럼명} = #{값}   

iBatis : $컬럼명$ = #{값}


* 쿼리에 작은따옴표가 붙지 않는다. 

값에 $를 사용하면 스트링의 경우 작은따옴표로 감싸지지 않기 때문에 에러 발생한다.



이렇게 사용하지 않으면 unknown column 이나 There is no readable property named  등등의 에러가 뜨게 된다



 출처 http://marobiana.tistory.com/60

블로그 이미지

왕왕왕왕

,


mybatis.zip





'JAVA > Spring 4.0' 카테고리의 다른 글

Spring4.0 스프링할때 web.xml과 jsp파일 인코딩  (0) 2015.10.07
Spring4.0 Mybatis $ , # 차이점  (0) 2015.10.01
Spring4.0 Mybatis (pom.xml)  (0) 2015.10.01
Spring4.0 JDBCTemplate  (0) 2015.10.01
Spring4.0 메이븐 프로젝트 구조  (0) 2015.09.22
블로그 이미지

왕왕왕왕

,


pom.xml에서 dependencies 탭을 눌러서 add버튼을 눌러서 추가한다.




추가하고 나서 저렇게 나오면 완료


'JAVA > Spring 4.0' 카테고리의 다른 글

Spring4.0 Mybatis $ , # 차이점  (0) 2015.10.01
Spring4.0 Mybatis 프로젝트 기본구조  (0) 2015.10.01
Spring4.0 JDBCTemplate  (0) 2015.10.01
Spring4.0 메이븐 프로젝트 구조  (0) 2015.09.22
Spring4.0 resources 경로이용하기  (0) 2015.09.16
블로그 이미지

왕왕왕왕

,

기본구조 구현된 스프링 프로젝트

JDBC.zip



Servlet-context.xml


<!-- template -->

<beans:bean name="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<beans:property name="driverClassName"                        value="oracle.jdbc.driver.OracleDriver" />

<beans:property name="url"

value="jdbc:oracle:thin:@localhost:1521:orcl" />

<beans:property name="username" value="scott" />

<beans:property name="password" value="tiger" />

</beans:bean>


<beans:bean name="template"

class="org.springframework.jdbc.core.JdbcTemplate">

<beans:property name="dataSource" ref="dataSource" />

</beans:bean>

<!-- END template -->



web.xml


 <resource-ref>

    <description>Connection</description>

    <res-ref-name>jdbc:/OracleDB</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

    <res-auth>Container</res-auth>

  </resource-ref>


pom.xml


<!-- JDBC Template -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>4.1.4.RELEASE</version>

</dependency>

<!-- END JDBC Template -->

블로그 이미지

왕왕왕왕

,



'JAVA > Spring 4.0' 카테고리의 다른 글

Spring4.0 Mybatis (pom.xml)  (0) 2015.10.01
Spring4.0 JDBCTemplate  (0) 2015.10.01
Spring4.0 resources 경로이용하기  (0) 2015.09.16
Spring4.0 ModelAndView 이용한 전달  (0) 2015.09.16
Spring4.0 GET,POST 파라미터 전달  (0) 2015.09.16
블로그 이미지

왕왕왕왕

,


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>


<p>아이디 ${id}  </p>

<img alt="d" src="resources/image/king.jpg">

</body>

</html>


Servlet-context.xml에 resources 매핑이 되있어서

뷰페이지에서는 상대경로를 찾을 필요 없이 바로 resources부터 경로를 써주면된다.




블로그 이미지

왕왕왕왕

,