반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자료구조
- java
- 자바
- 그리디알고리즘
- 코딩
- spring
- 스텍
- NestJS
- 프로그래머스
- js
- 코딩테스트
- 백준
- 삼성
- SWEA
- stack
- 정렬
- 알고리즘
- 코테준비
- 중간 평균값 구하기
- 자바스크립트
- array
- AtoZ0403
- 콜백지옥
- 코테
- 삼성소프트웨어아카데미
- mybatis
- javascript
- 인프런
- 카카오
- 배열
Archives
- Today
- Total
개발에 AtoZ까지
[Spring] Mybatis 와 JPA 환경설정 차이 본문
반응형
1. 환경
- spring Legacy project (mvc)
- spring security
- java 1.8
- Orcle DB
- tomcat 8.5.59
- maven
2. POM.xml
- Mybatis 코드
<!-- dbcp , mybatis -->
<!-- Common DBCP-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- spring-mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
- JPA 코드
<!-- 스프링 ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- JPA, Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.11.Final</version>
</dependency>
<!-- Common DBCP(Database Connection Pool) -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
3. WEB.XML
-> model, web,security등의 xml을 나눠서 관리하여 가시성을 높임
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
<!-- DispatcherServlet 로딩에 앞서 Filter기반 Security 설정과 spring-model ( 로그인 관련
db연동을 통한 인증,인가 처리를 위해) 설정을 ServletContextListener를 이용해 로딩한다 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml,/WEB-INF/spring-model.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet은 ServletConfig를 이용해 spring web 관련 설정을 로딩한다 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodeFilter</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>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<description>스프링 파일업로드 필터 등록-스프링시큐리티설정전에 위치해야 한다</description>
<filter-name>MultipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MultipartFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter>
<description>스프링 시큐리티 필터 등록</description>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
4. web.xml에 연결한 spring-model.xml
- JPA 설정
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<context:annotation-config />
<!-- 트랜잭션 관리자 활성화 -->
<tx:annotation-driven />
<!-- 트랜잭션 관리자 등록 -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- JPA 예외를 스프링 예외로 변환 -->
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<!-- JPA 설정 ( 엔티티 매니저 팩토리 등록 ) -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- @Entity 탐색 범위 -->
<property name="packagesToScan"
value="com.victolee.guestbook.domain" />
<!-- 하이버네이트 구현체 사용 -->
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<!-- 하이버네이트 상세 설정 -->
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <!-- 방언 -->
<prop key="hibernate.show_sql">true</prop> <!-- SQL 보기 -->
<prop key="hibernate.format_sql">true</prop> <!-- SQL 정렬해서 보기 -->
<prop key="hibernate.use_sql_comments">true</prop> <!-- SQL 주석 보기 -->
<prop key="hibernate.id.new_generator_mappings">true</prop> <!-- JPA 표준에 맞게 새로운 키 생성 전략을 사용 -->
</props>
</property>
</bean>
<!-- Connection Pool DataSource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/jpadb" />
<property name="username" value="id" />
<property name="password" value="password" />
</bean>
<context:component-scan
base-package="com.victolee.guestbook.service, com.victolee.guestbook.repository">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Component" />
</context:component-scan>
</beans>
- Mybatis 설정
<?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:aop="http://www.springframework.org/schema/aop"
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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- Spring Security를 위해 ServletContextListener 를 이용해 로딩하므로 컨트롤러측은 제외하고
dbcp mybatis model 쪽만 빈을 생성하도록 설정한다 -->
<context:component-scan base-package="org.kosta">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
<!-- dataSource -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:xe</value>
</property>
<property name="username">
<value>watflix</value>
</property>
<property name="password">
<value>dev6m</value>
</property>
<property name="maxActive">
<value>30</value>
</property>
</bean>
<!-- MyBatis 연동 설정 -->
<mybatis-spring:scan
base-package="org.kosta.watflix.model.mapper" />
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage"
value="org.kosta.watflix.model" />
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true" />
</bean>
</property>
</bean>
<!-- transation 설정 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven
transaction-manager="transactionManager" proxy-target-class="true" />
</beans>
반응형
'백엔드 > Spring' 카테고리의 다른 글
[에러]Database "mem:testdb" not found, either pre-create it or allow remote database creation (0) | 2021.07.19 |
---|---|
[Spring Boot] Gradle로 Build하는 방법 (0) | 2021.06.13 |
[에러] Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 39; 예기치 않은 파일의 끝입니다. (0) | 2021.05.26 |
[Spring Boot] 전송방식에 따른 Parameter 받는 방법 (1) | 2021.04.30 |
[Spring] 파일 단일/다중 업로드(Multipart) (0) | 2021.01.23 |
H2, JPA, MyBatis 특징 및 차이 (0) | 2021.01.17 |
Comments