1. pom.xml ํ์ผ ์์
<!-- ์ถ๊ฐ ์์กด ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <!-- MyBatis ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!-- Spring JDBC ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- ํ์ผ์ ๋ก๋ ์ฒ๋ฆฌ๋ฅผ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (SmartEditor ์์๋ ํ์ํจ)--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- json, xml ์๋ต์ ํธํ๊ฒ ํ ์ ์๋๋ก ๋์ ์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.0</version> </dependency> <!-- Aop ์ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.0</version> </dependency> <!-- Spring Security ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- ํธ๋ ์ ์ ์ฒ๋ฆฌ๋ฅผ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!-- ์ค๋ผํด ๋ผ์ด๋ธ๋ฌ๋ฆฌ --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>21.1.0.0</version> </dependency>
2. web.xml ํ์ผ ์์
<!-- post ๋ฐฉ์ ์ ์ก๋ ๋ ํ๊ธ ๊นจ์ง์ง ์๋๋ก Spring ์ธ์ฝ๋ฉ ํํฐ ์ ์ํ๊ธฐ --> <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> <!-- Spring ์ธ์ฝ๋ฉ ํํฐ ๋งตํํ๊ธฐ --> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3. HomeController.java ํ์ผ ์์
4. web.xml ํ์ผ ์์
- Controller์์ /home.do๋ก๋ง ์์ฒญ ๋งตํํ๊ธฐ ๋๋ฌธ์ ๋ฃจํธ ๊ฒฝ๋ก์ ์ ๊ทผํด๋ ์๋ฌด๋ฐ ํ์ด์ง๊ฐ ๋จ์ง ์๋๋ค.
- web.xml ํ์ผ์ welcome-file-list๋ฅผ ์ถ๊ฐํ๋ค.
MyBatis
- ํน์ ์ฝ๋๋ฅผ ์๋ํํ์ฌ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ฃผ๋ ๋ชจ๋
MyBatis ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ
1. MyBatis ์์ xml ์ผ๋ถ ๋ณต์ฌ<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
2. com.ํจํค์ง๋ช .ํ๋ก์ ํธ ํด๋ ๋ด๋ถ์ ํด๋ ์ถ๊ฐ
- com.ํจํค์ง๋ช .ํ๋ก์ ํธ.mybatis ํด๋ ์์ฑ
3. Configuration.xml ํ์ผ ์์ฑ
- ํด๋ ์ฐํด๋ฆญ โ new โ others โ xml โ ํ์ผ ์์ฑํ๊ธฐ
4. Configuration.xml ํ์ผ ์์ <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
ํด๋น xml ๋ถ์ฌ๋ฃ๊ธฐ
1. servlet-context.xml ํ์ผ ์์
sessionFactory ๊ฐ์ฒด ์์ฑ ์, ์ค์ ์ ๋ณด๋ฅผ ๋ณ๊ฒฝํด์ผ ํ๋ค.<!-- JNDI ๋ฐ์ดํฐ ์์ค ๊ฐ์ฒด ์ป์ด์ค๋ ์ค์ Servers/context.xml ์ ์ค์ ๋ oracle ์ ์์ ๋ณด ๊ฐ ์์ด์ผ ๋๋ค. <Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:xe" username="scott" password="tiger" maxTotal="20" maxIdle="10" maxWaitMillis="-1"/> --> <beans:bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <beans:property name="jndiName" value="java:comp/env/jdbc/myoracle"/> </beans:bean> <!-- ์๋ ์๋์ ์ฝ๋์ ๊ฐ๋ค dataSource = new JndiObjectFactoryBean(); dataSource.setJndiName("java:comp/env/jdbc/myoracle"); --> <!-- SqlSessionFactory ๊ฐ์ฒด Configuration.xml ๋ฌธ์๊ฐ ์ด๋์ ์๋์ง ์๋ ค์ผ ํ๋ค. --> <beans:bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <beans:property name="dataSource" ref="dataSource"/> <beans:property name="configLocation" value="classpath:com/ํจํค์ง๋ช /ํ๋ก์ ํธ๋ช /mybatis/Configuration.xml"/> </beans:bean> <!-- sessionFactory=new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setConfigLocation("classpath:com/gura/xxx"); --> <!-- SqlSession ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ SqlSessionTemplate(SqlSession) ๊ฐ์ฒด Dao ๊ฐ ์์กดํ๋ ๊ฐ์ฒด --> <beans:bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <beans:constructor-arg name="sqlSessionFactory" ref="sessionFactory"/> </beans:bean> <!-- ์์ bean ์ค์ ์ java code ๋ก ํ์ฐ ํ๋ฉด ์๋์ ๊ฐ๋ค dataSource = new JndiObjectFactoryBean(); dataSource.setJndiName("java:comp/env/jdbc/myoracle"); sessionFactory=new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setConfigLocation("classpath:com/gura/xxx"); new SqlSessionTemplate(sessionFactory); -->
2. Servers/context.xml ํ์ผ ์์
<Resource name="jdbc/myoracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:xe" username="scott" password="tiger" maxTotal="20" maxIdle="10" maxWaitMillis="-1"/>
3. com.aa.step03 ํจํค์ง ๋ด๋ถ์ ํจํค์ง ์์ฑ
- com.aa.step03.member.dao ์์ฑ
- com.aa.step03.member.dto ์์ฑ
4. ๊ฐ ํจํค์ง์ Dto, Dao ํ์ผ ์์ฑ
- MemberDao Interface๋ก ์์ฑ
- MemberDto Class๋ก ์์ฑ
5. Dao ํจํค์ง์ Dao๋ฅผ ๊ตฌํํ Class ์์ฑ
- MemeberDaoImpl Class ์์ฑ
1. Interface Type์ Dao๋ฅผ implements ํ๋ค.
public class MemberDaoImpl implements MemberDao {}
2. interface์ ์ ์๋ ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค.
public class MemberDaoImpl implements MemberDao { @Override public void insert(MemberDto dto) { session.insert("member.insert", dto); } @Override public void update(MemberDto dto) { } @Override public void delete(int num) { } @Override public MemberDto getData(int num) { return null; } @Override public List<MemberDto> getList() { return null; } }
3. ๊ตฌํ ํด๋์ค๋ฅผ Spring Bean Container์์ ๊ด๋ฆฌํ ์ ์๋๋ก ํ๋ค.
// Dao ๊ฐ์ฒด๋ Spring Bean Container์์ ๊ด๋ฆฌํ๋๋ก ์ด๋ ธํ ์ด์ ์ ๋ถ์ธ๋ค. @Repository public class MemberDaoImpl implements MemberDao {....}
4. SqlSessionTemplate์ ๋ฐ๋ SqlSession interface type ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
// Dao ๊ฐ์ฒด๋ Spring Bean Container์์ ๊ด๋ฆฌํ๋๋ก ์ด๋ ธํ ์ด์ ์ ๋ถ์ธ๋ค. @Repository public class MemberDaoImpl implements MemberDao { // Spring Bean Container ์์ SqlSession Type ๊ฐ์ฒด๋ฅผ ์ฐพ์์ ์ฃผ์ ํด(DI) ์ฃผ์ธ์๋ผ๋ ์๋ฏธ // Autowired ๋๋ฌธ์ Null ๊ฐ์ ๊ฐ์ง ์๋๋ค. @Autowired private SqlSession session; .... }
5. ๊ตฌํํ ๋ฉ์๋ ๋ด๋ถ์์ ๊ธฐ๋ฅ์ ๋ฐ๋ผ SqlSession ๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ค.
- session : SqlSession interface ํ์ ์ ๊ฐ์ฒด
- insert : SqlSessionTemplate ํด๋์ค์ ๋ฉ์๋
- ์ฒซ๋ฒ์งธ ์ธ์ : mapper์ ์ ์ํ "namespace.id"๋ฅผ ๋ช ์
- ๋๋ฒ์งธ ์ธ์ : mapper์ ์ ์ํ parameterType์ ์ธ์๊ฐ์ผ๋ก ๋งคํํ ์ ์๋๋ก ์ง์ @Override public void insert(MemberDto dto) { session.insert("member.insert", dto); }
6. Configuration.xml ํ์ผ์ด ์๋ mybatis ํจํค์ง ๋ด๋ถ์ xml ํ์ผ ์์ฑ
- ํด๋น xml ํ์ผ์ DB์ฐ๋ ๋ฐ DML์ ์ปจํธ๋กคํ๋ ํ์ผ์ด๋ค.
- ํ์ผ๋ช : MemberMapper.xml
- namespace : mybatis mapper ๊ฐ์ฒด๋ช
- insert ํ๊ทธ์ id : mybatis mapper ๋ฉ์๋๋ช
- parameterType : ์ฌ์ฉํ DTO ๊ฐ์ฒด ๊ฒฝ๋ก
- #{ํ๋๋ช } : Dto์์ ์ ์๋ ํ๋๋ช ์ ์์ฑ
- ๋ด๋ถ์ DML ๋ฌธ๋ฒ์ ์์ฑ<?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="member"> <insert id="insert" parameterType="com.aa.step03.member.dto.MemberDto"> INSERT INTO member (num, name, addr) VALUES (member_seq.NEXTVAL, #{name}, #{addr}) </insert> </mapper>
7. MemberMapper.xml ํ์ผ์ ์ฌ์ฉํ ์ ์๋๋ก Configuration.xml์ ๋ช ์ํ๋ค.
- mapper resource : MemberMapper.xml์ด ์๋ ๊ฒฝ๋ก๋ฅผ ์์ฑ<configuration> <mappers> <mapper resource="com/aa/step03/mybatis/MemberMapper.xml"/> </mappers> </configuration>
8. ์ค์ ์ฌ์ฉํ๋ Controller์์ Dao Interface type ๊ฐ์ฒด๋ฅผ DI ์ฃผ์ ํ๋ค.
- ๊ฒฐ๊ณผ์ ์ผ๋ก ์ค์ Dao๋ฅผ ๊ตฌํํ ํด๋์ค์์ ์์กด๊ด๊ณ๊ฐ ์ฝํด์ง๋ค.@Controller public class MemberController { // MemberDaoImpl ํด๋์ค ๊ฐ์ฒด๋ฅผ Interface type ๊ฐ์ฒด์ DI ์ฃผ์ ํ๋ค. @Autowired private MemberDao dao; @RequestMapping("/member/list.do") public String getList(HttpServletRequest req) { List<MemberDto> list = dao.getList(); req.setAttribute("list", list); return "member/list"; } }
- servlet-context.xml ํ์ผ ์์
1. ๋งจ ์์ ์๋ bean ๊ฐ์ฒด๋ฅผ ์๋ ์ฝ๋๋ก ์์ <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd 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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
2. ๋งจ ๋ฐ์ ์๋ ์ฝ๋(Ascpect ์ฝ๋) ์ถ๊ฐ
<!-- ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ์ผ๋ก aop ์ ์ฉํ ์ ์๋๋ก ์ค์ --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3. ์๋ ์ฝ๋(์ ๋ก๋ ๊ด๋ จ ์ฝ๋) ์ถ๊ฐ
<!-- Multipart ํผ ์ ์ก ์ฒ๋ฆฌ๋ฅผ ์ํ bean ์ค์ --> <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <beans:property name="maxUploadSize" value="102400000"/> </beans:bean>
- servlet-context.xml ํ์ผ ์์
<!-- Spring Transaction Manager ์ค์ --> <beans:bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <beans:property name="dataSource" ref="dataSource"/> </beans:bean> <!-- @Transactional ์ด๋ ธํ ์ด์ ์ผ๋ก ํธ๋ ์ ์ ์ ๊ด๋ฆฌ ํ ์ ์๋๋ก ์ค์ --> <tx:annotation-driven transaction-manager="txManager"/>