ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค mybatis ์‚ฌ์šฉํ•˜๊ธฐ
    JAVA 2020. 8. 21. 23:59

    ๐ŸŽฏ ์ž๋ฐ” ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์—ฐ์Šตํ•ด๋ณธ๋‹ค.

     

     

     

     

    ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค (MyBatis)

    ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋Š” ํผ์‹œ์Šคํ„ด์Šค ํ”„๋ ˆ์ž„์›Œํฌ(Persistence Framework)์ด๋‹ค. ์ด ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์—ฐ์Šตํ•ด๋ณด๊ธฐ

    ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ

     

     

    0. Test ํด๋ž˜์Šค์˜ main ํ•จ์ˆ˜ ์‹คํ–‰ ์ „

    0-1. ์˜ค๋ผํด ๋“œ๋ผ์ด๋ฒ„ jarํŒŒ์ผ๊ณผ mybatis ๋“œ๋ผ์ด๋ฒ„ jarํŒŒ์ผ์„ ๋นŒ๋“œํŒจ์Šค์— ์ถ”๊ฐ€ํ•œ๋‹ค.

    0-2. com.config ํŒจํ‚ค์ง€์˜ MySqlSessionFactory ํด๋ž˜์Šค

    #MySqlSessionFactory
    
    public class MySqlSessionFactory {
    	static SqlSessionFactory sqlSessionFactory = null;
    	static {// ์ดˆ๊ธฐํ™”
    		// jdbc.propertiesํŒŒ์ผ ์‚ฌ์šฉ
    		String resource = "Configuration.xml";
    		InputStream inputStream = null;
    		try {
    			inputStream = Resources.getResourceAsStream(resource);
    			System.out.println("์ดˆ๊ธฐํ™” ์„ฑ๊ณต");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    	}// end static
    
    	// SqlSession ๋ฐ˜ํ™˜
    	public static SqlSession getSqlSession() {
    		SqlSession session = sqlSessionFactory.openSession();
    		return session;
    	}
    }

    JDBC ์ปค๋„ฅ์…˜์„ ๋‹ด๋‹นํ•˜๋˜ ํƒฌํ”Œ๋ฆฟ ํด๋ž˜์Šค์˜ ์—ญํ• ์„ MySqlSessionFactory ํด๋ž˜์Šค๊ฐ€ ๋‹ด๋‹นํ•œ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” ์ œ์ผ ๋จผ์ € static ๋ธ”๋Ÿญ์„ ์ฝ์–ด์˜ค๋ฉด์„œ ์–ด๋–ค ํŒŒ์ผ์„ ์ €์žฅํ•  ๊ฒƒ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. resuorce ๋ณ€์ˆ˜์— ๋‹ด๊ธด 'Configuration.xml'ํŒŒ์ผ์— jdbc์—ฐ๊ฒฐ์— ํ•„์š”ํ•œ 4๊ฐ€์ง€ ์ •๋ณด๋ฅผ ๋‹ด๋Š”๋‹ค.

    #MySqlSessionFactory
    
    InputStream inputStream=null;
    		try {
    			inputStream = Resources.getResourceAsStream(resource);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	 sqlSessionFactory =
    		new SqlSessionFactoryBuilder().build(inputStream);

    ์ด ๋ถ€๋ถ„์€ ํŒŒ์ผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ธํ’‹์ŠคํŠธ๋ฆผ์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ์ปค๋„ฅ์…˜์„ ์–ป์–ด๊ฐ€๋Š” ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.

    #MySqlSessionFactory
    
    //SqlSession ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ
    	// MySqlSessionFactory.getSession()์œผ๋กœ ํ˜ธ์ถœ ์‚ฌ์šฉ
    	public static SqlSession getSession() {
    		SqlSession session = sqlSessionFactory.openSession();
    		//์‹ค์ œ ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉํ•  connection๊ณผ ๋น„์Šทํ•œ ๊ฐ์ฒด
    		return session;
    	}

    getSession์„ ํ†ตํ•ด์„œ db์— ์ ‘์†ํ•˜๊ณ  ์‚ฌ์šฉํ•œ๋‹ค. SqlSession์ด JDBC์˜ Connection์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

    0-3. XML์—์„œ SqlSessionFactory ๋นŒ๋“œํ•˜๊ธฐ

    <?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>
      <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
          </dataSource>
        </environment>
      </environments>
      <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
      </mappers>
    </configuration>

    Configuration.xml ํŒŒ์ผ์€ ์ด๋ ‡๊ฒŒ ์ƒ๊ฒผ๋‹ค. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ํ™ˆํŽ˜์ด์ง€์—์„œ ๋นŒ๋“œ ํ˜•์‹์„ ๊ทธ๋Œ€๋กœ ๋ถ™์—ฌ๋„ฃ์€ xml ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ํ•„์š”ํ•œ ๋“œ๋ผ์ด๋ฒ„ ์ •๋ณด๋งŒ ์ˆ˜์ •ํ•œ๋‹ค.

    #๋“œ๋ผ์ด๋ฒ„ ๊ธฐ๋ณธ ์ •๋ณด ์„ค์ •
    
    <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
    <property name="username" value="scott"/>
    <property name="password" value="tiger"/>

    property ํƒœ๊ทธ์— 4๊ฐ€์ง€ ๊ธฐ๋ณธ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    # ์‹คํ–‰ํ•  ์‹œํ€„๋ฌธ์„ ๊ฐ€์ง„ xmlํŒŒ์ผ ์ง€์ •
    
    <mappers>
        <mapper resource="DeptMapper.xml"/>
      </mappers>

    sql ์ฟผ๋ฆฌ๋Š” ๋งคํผ๋ผ๋Š” xml ํŒŒ์ผ์— ์ž‘์„ฑํ•œ๋‹ค.

     

    ** ๋งŒ์•ฝ xml ๋ฌธ์„œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์œผ๋ฉด ์ฝ˜์†”์ฐฝ ๊ทผ์ฒ˜์— ์žˆ๋Š” source๋ฅผ ํด๋ฆญํ•œ๋‹ค.

     

     

    1. Test ํด๋ž˜์Šค์˜ main ํ•จ์ˆ˜ ์‹คํ–‰

    #main
    
    OracleMyBatisService service = new OracleMyBatisService();
    List<Dept> list = service.select();
    for (Dept dept : list) {
    	System.out.println(dept);
    }

    ์˜ค๋ผํด Scott ๊ณ„์ •์— ์žˆ๋Š” Deptํ…Œ์ด๋ธ”์„ JDBC ์—ฐ๋™์— ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ๋จผ์ € select() ๋ฉ”์†Œ๋“œ๋กœ ์—ฐ์Šต์„ ์‹œ์ž‘ํ•œ๋‹ค. ๋ฉ”์ธ์—์„œ๋Š” ์„œ๋น„์Šค ํด๋ž˜์Šค๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. ์„œ๋น„์Šค ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ  Dept์ œ๋„ˆ๋ฆญํƒ€์ž…์˜ list ๋ณ€์ˆ˜์— service.select()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

     

     

    2. ์„œ๋น„์Šค ํด๋ž˜์Šค

    public class OracleMyBatisService {
    
    	OracleMybatisDAO dao;
    	public OracleMyBatisService() {
    		dao = new OracleMybatisDAO();
    	}
    	public List<Dept> select() {
    		SqlSession session = MySqlSessionFactory.getSession();
    		List<Dept> list = null;  //๋ฐ˜ํ™˜๊ฐ’์„ ์ค€๋น„
    		try {
    			list = dao.selectAll(session); //dao์— SqlSession ์ „์†ก
    		}finally {
    			session.close(); //connection.close()์™€ ๋น„์Šท, ๋ชจ๋“  ํ•จ์ˆ˜๋งˆ๋‹ค ๋‹ซ๊ธฐ
    		}return list;
    	}// end class

    ์ƒ์„ฑ์ž์—์„œ DAO๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋ฉ”์ธ์—์„œ serviceํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ dao๊ฐ์ฒด๋„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

    2-1. ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ SqlSession์„ ์–ป์–ด์˜จ๋‹ค. (connection ์–ป์–ด์˜ค๊ธฐ์™€ ๋น„์Šทํ•˜๋‹ค)

    2-2. ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฆฌ์ŠคํŠธ์— dao์˜ ์…€๋ ‰ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์—ฐ๊ฒฐ๋œ ์„ธ์…˜์„ ์ธ์ž๋กœ ๋ณด๋‚ธ๋‹ค. ์„ธ์…˜ ์‚ฌ์šฉ ํ›„ close()๋กœ ์ž˜ ๋‹ซ์•„์ฃผ๊ณ  ํ˜ธ์ถœ๋กœ ๋ฐ›์•„์˜จ list๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

     

     

    3. DAO ํด๋ž˜์Šค

    public class OracleMybatisDAO {
    	public List<Dept> selectAll(SqlSession session) {
    		List<Dept> list = session.selectList("selectAll");
    		return list;
    	}
    }

    3-1. daoํด๋ž˜์Šค์˜ ์ฝ”๋“œ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. ์„œ๋น„์Šค ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์„ธ์…˜์„ ๋ฐ›์•˜์œผ๋‹ˆ ์ด์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ์ฟผ๋ฆฌ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค. ์ฟผ๋ฆฌ๋Š” ๋งคํผ xml ํŒŒ์ผ์—์„œ ์‚ฌ์šฉํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ sqlsession ๊ฐ์ฒด์˜ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ธ select์„ ์‹คํ–‰์‹œ selectList ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  sql ์ฟผ๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ con.preparestatement(sql) ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ sql๋ฌธ์„ ๋งคํผ์—์„œ ์ง€์ •ํ•œ ์•„์ด๋””๋กœ ํ˜ธ์ถœํ•œ๋‹ค.

     

     

    4. Maper.xml ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

    5. sql select๋ฌธ์„ select ํƒœ๊ทธ ์•ˆ์— ์ž‘์„ฑํ•œ๋‹ค.

    6. ๊ฒฐ๊ณผ๋ฅผ resultType์— ์ €์žฅํ•œ๋‹ค. ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋ชจ๋ธ ํด๋ž˜์Šค๋Š” Dept๋กœ ์ง€์ •ํ•œ๋‹ค.

    # DTDMapper.xml
    
    <mapper namespace="org.mybatis.example.BlogMapper">
    	<select id="selectAll" resultType="com.dto.Dept">
    	<!-- id=selectAll ์‹คํ–‰ ํ›„ ๊ฒฐ๊ณผ๋ฅผ resultType์œผ๋กœ ์ €์žฅ -->
    		select deptno, dname, loc from dept
    	</select>
    </mapper>

    resultSet์— while๋ฌธ์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๊บผ๋‚ด์˜ค๋˜ ์ž‘์—…์ด ์‚ฌ๋ผ์ง€๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋ชจ๋ธํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•œ ๊ฒƒ์ด๋‹ค.

    public class Dept {
    
    	private int deptno;
    	private String dname;
    	private String loc;

    ๋ชจ๋ธ ํด๋ž˜์Šค Dept๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์ด๋ฆ„์ด dept ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์ด๋ฆ„๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์ƒ์„ฑ์ž์™€ getter setter๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  toString๊นŒ์ง€ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด์ค€๋‹ค.

     

    7. ๋งตํผ์—์„œ dao๋กœ ๋ฆฌํ„ด

    8. dao์—์„œ ์„œ๋น„์Šค๋กœ ๋ฆฌํ„ด

    9. ์„œ๋น„์Šค์—์„œ ๋ฉ”์ธ์œผ๋กœ ๋ฆฌํ„ด

    10. ๋ฉ”์ธ์—์„œ ๋ฆฌํ„ด๊ฐ’ ์ถœ๋ ฅ

     

    ๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ ค๋ณด๋ฉด ๋ฆฌํ„ด๊ฐ’์ด ๋„˜์–ด์˜จ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

     

     

     

    SQL๋ฌธ ์ถ”๊ฐ€ํ•ด์„œ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์—ฐ์Šตํ•˜๊ธฐ

    1. selectByDeptno

    <select id="selectByDeptno" resultType="com.dto.Dept">
    	select deptno, dname, loc from dept where deptno=10
    </select>
    #dao
    
    public Dept selectByDeptno(SqlSession session){
    		 Dept dept = session.selectOne("selectByDeptno");
    		 return dept;
    	}
    #service
    
    public Dept selectByDeptno() {
    		SqlSession session = MySqlSessionFactory.getSession();
    		Dept dept = null;
    		try {
    			dept = dao.selectByDeptno(session);
    		} finally {
    			// TODO: handle exception
    		}return dept;
    	}
    
    #main
    
    Dept dept = service.selectByDeptno();
    System.out.println(dept);

     

     

    2. selectByDeptnoUse

    ์ด๋ฒˆ์—” deptno๋ฅผ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š๊ณ  ๋ฉ”์ธํ•จ์ˆ˜์—์„œ ์ธ์ž๋กœ ๋ฐ›์•„์˜จ๋‹ค. ๋ฐ›์•„์˜ฌ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์„ ์—ฌ๊ธฐ์„œ ๋ฏธ๋ฆฌ ์ •ํ•ด์ค€๋‹ค.

    <select id="selectByDeptnoUse" parameterType="int" resultType="com.dto.Dept">
    	select deptno, dname, loc from dept where deptno = #{deptno}
    </select>
    #dao
    
    public Dept selectByDeptnoUse(SqlSession session, int deptno) {
    		Dept dept = session.selectOne("selectByDeptnoUse", deptno);
    		return dept;
    	}
    #service
    
    public Dept selectByDeptnoUse(int deptno) {
    	SqlSession session = MySqlSessionFactory.getSession();
    	Dept dept = null;
    	try {
    		dept = dao.selectByDeptnoUse(session, deptno);
    	} finally {
    		session.close();
    	}return dept;
    }
    #main
    
    Dept dept2 = service.selectByDeptnoUse(40);
    System.out.println(dept2);

     

     

    3. deptInsert

    DML ๋ช…๋ น๋ฌธ์€ ํŠธ๋žœ์žญ์…˜์„ ์ž๋™ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปค๋ฐ‹, ๋กค๋ฐฑ ๋“ฑ์œผ๋กœ ์ข…๋ฃŒ๋ฅผ ํ•ด์•ผํ•œ๋‹ค. ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค๋Š” ์˜คํ† ์ปค๋ฐ‹์ด ์•ˆ๋œ๋‹ค. ๊ธฐ๋ณธ์ด false๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— dml๋ฌธ์ด ๋๋‚˜๋ฉด ์ปค๋ฐ‹์„ ํ•ด์•ผํ•œ๋‹ค.

    <insert id="deptInsert" parameterType="com.dto.Dept">
      	insert into dept (deptno, dname, loc) values
      	(#{deptno}, #{dname}, #{loc})
    </insert>
    dao
    
    public void insert(SqlSession session, Dept dept) {
    	int num = session.insert("deptInsert", dept);
    	System.out.println("์ถ”๊ฐ€๋œ ๋ ˆ์ฝ”๋“œ ====" + num);
    }
    service
    
    public void insert(Dept dept) {
    	SqlSession session = MySqlSessionFactory.getSqlSession();
    	try {
    		dao.insert(session, dept);
    		session.commit();//๋ช…์‹œ์ ์œผ๋กœ ๊ผญ ํ•ด์ฃผ์–ด์•ผ ํ•จ. 
    	} finally {
    		session.close();
    	}
    }
    service.insert(new Dept(99, "๊ฐœ๋ฐœ","๊ฐ•๋‚จ"));

     

     

    4. deptDelete

    mapper
    
    <delete id="deptDelete" parameterType="com.dto.Dept">
      	delete from dept where deptno = #{deptno}
      </delete>
    dao
    
    public void delete(SqlSession session,int deptno) {
    	int num= session.delete("deptDelete", deptno);
    	System.out.println("์‚ญ์ œ๋œ ๊ฐฏ์ˆ˜ ===="+ num);
    }
    service
    
    public void delete(int deptno) {
    	SqlSession session = MySqlSessionFactory.getSqlSession();
    	try {
    		dao.delete(session, deptno);
    		session.commit();// ๋ช…์‹œ์ ์œผ๋กœ ๊ผญ ํ•ด์ฃผ์–ด์•ผ ํ•จ.
    	} finally {
    		session.close();
    	}
    }
    Main
    
    OracleMyBatisService service = new OracleMyBatisService();
    List<Dept> list = service.selectAll();
    for (Dept dept : list) {
    	System.out.println(dept);
    }		
    Dept dept= service.selectByDeptno(99);
    System.out.println(dept);
    service.insert(new Dept(99, "๊ฐœ๋ฐœ","๊ฐ•๋‚จ"));
    service.update(new Dept(99,"์˜์—…", "์„œ์šธ"));
    service.delete(99);

     

     

    5. deptRecordCount

    dept ํ…Œ์ด๋ธ”์˜ ์ „์ฒด ๋ ˆ์ฝ”๋“œ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

    <select id="deptRecordCount" resultType="int">
      	select count(*) from dept
      </select>
    public int deptRecordCount(SqlSession session) {
    		int num = session.selectOne("deptRecordCount");
    		return num;
    	}
    public int deptRecordCount() {
    		SqlSession session = MySqlSessionFactory.getSqlSession();
    		int num = 0;
    		try {
    			num = dao.deptRecordCount(session);
    		} finally {
    			session.close();
    		}return num;
    	}
    System.out.println(service.deptRecordCount());

     

     

    6. selectByHashMap

    select ๊ฒฐ๊ณผ๋ฅผ ํ•ด์‰ฌ๋งต์œผ๋กœ ๋ฐ›๋Š”๋‹ค. ์กฐ๊ฑด๋ฌธ ๊ฒ€์ƒ‰์— between a and b์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

    <select id="selectByHashMap" parameterType="HashMap" resultType="Dept">
     	select * from dept where deptno between #{a} and #{b}
    </select>
    public List<Dept> selectByHashMap(SqlSession session, HashMap<String, Integer> map){
    		List<Dept> list= session.selectList("selectByHashMap", map);
    		return list;
    	}
    public List<Dept> selectByHashMap(HashMap<String, Integer> map){
    		SqlSession session = MySqlSessionFactory.getSqlSession();
    		List<Dept> list = null;
    		try {
    			list = dao.selectByHashMap(session, map);
    		} finally {
    			session.close();
    		}return list;
    }
    HashMap<String, Integer> map = new HashMap<> ();
    map.put("a", 1);
    map.put("b", 50);
    List<Dept> list = service.selectByHashMap(map);
    for (Dept dept : list) {
    	System.out.println(dept);
    }

     

     

    7. selectInHashMap()

    <select id="selectInHashMap" parameterType="HashMap"
    		resultType="Dept">
    		select * from dept where deptno in (#{a},#{b},#{c})
    	</select>
    public List<Dept> selectInHashMap (SqlSession session, HashMap<String,Integer> map){
    		List<Dept> list= session.selectList("selectInHashMap", map);
    		return list;
    	}
    public List<Dept> selectInHashMap(HashMap<String,Integer> map){
    		SqlSession session = MySqlSessionFactory.getSqlSession();
    		List<Dept> list = null;
    		try {
    			list = dao.selectInHashMap(session, map);
    		} finally {
    			session.close();
    		}return list;
    	}
    map.put("a", 10);
    		map.put("b", 70);
    		map.put("c", 90);
    		list=service.selectInHashMap(map);
    		for (Dept d : list) {
    			System.out.println(d);
    		}

     

     

    8. selectByDeptnoHashMap

    <select id="selectByDeptnoHashMap" parameterType="int"
    				resultType="HashMap">
    	select * from dept where deptno=#{deptno}
    </select>
    public HashMap selectByDeptnoHashMap(SqlSession session,int empno) {
    	HashMap map = session.selectOne("selectByDeptnoHashMap",empno);
    	return map;
    }
    public HashMap selectByDeptnoHashMap(int deptno) {
    	SqlSession session = MySqlSessionFactory.getSqlSession();
    	HashMap map=null;
    	try {
    		map = dao.selectByDeptnoHashMap(session, deptno);
    	} finally {
    		session.close();
    	}return map;
    }
    HashMap map2  = service.selectByDeptnoHashMap(10);
    System.out.println(map2);
    Set<String> keys = map2.keySet();
    System.out.println(map2.get("DNAME"));
    System.out.println(keys);

     

     

     

    ํ”„๋กœํผํ‹ฐ ๋งŒ๋“ค๊ธฐ

    ์•ž์œผ๋กœ ์˜ค๋ผํด ์—ฐ๋™์„ ์œ„ํ•œ 4๊ฐ€์ง€ ๊ธฐ๋ณธ์ •๋ณด๋Š” properties๋กœ ๋“ฑ๋กํ•œ ํ›„ ํ‚ค๊ฐ’๋งŒ ๋ฐ”๊ฟ”์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ๋‚˜์ค‘์— DB๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์—ฌ๊ธฐ ์™€์„œ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.

    ํ”„๋กœ์ ํŠธ์—์„œ ์˜ค๋ฅธ์ชฝ๋งˆ์šฐ์Šค๋ฅผ ํด๋ฆญํ•˜๊ณ  new -> file์„ ์„ ํƒํ•œ๋‹ค.

     

    ์ด์ œ configuration.xmlํŒŒ์ผ์— ๊ฐ€์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.

    <!--  jdbc.propertiesํŒŒ์ผ ๋“ฑ๋ก -->
    <properties resource="jdbc.properties"/>
      <environments default="development">
        <environment id="development">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
          	<property name="driver" value="${oracle.jdbc.driver.OracleDriver}"/>
          	<property name="url" value="${jdbc:oracle:thin:@localhost:1521:orcl}"/>
          	<property name="userid" value="${soctt}"/>
          	<property name="passwd" value="${tiger}"/>
          <!--  ๊ธฐ์กด ์‚ฌ์šฉํ•˜๋˜ 4๊ฐ€์ง€ ์ •๋ณด
            <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
            <property name="username" value="scott"/>
            <property name="password" value="tiger"/> -->

    ๊ธฐ์กด์— ์ž‘์„ฑํ–ˆ๋˜ ์ •๋ณด๋Š” ์ฃผ์„์ฒ˜๋ฆฌํ•˜๊ณ  ํ”„๋กœํผํ‹ฐ์—์„œ ์ผ๋˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

     

     

     

    ๋งคํผ.xml ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉํ•˜๊ธฐ

    <mappers>
        <mapper resource="DeptMapper.xml"/>
        <mapper resource="DeptMapper2.xml"/>
      </mappers>

    ํ”„๋กœ์ ํŠธ ์•ˆ์— ๋งคํผ๋ฅผ ๋˜ ํ•˜๋‚˜ ๋งŒ๋“  ๋’ค configuration.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.dept.DeptMapper2">
    
     <select id="deptRecordCount" resultType="int">
      	select count(*) from dept
      </select>
    <select id="selectAll" resultType="com.dto.Dept"><!-- ๊ฒฐ๊ณผ๋ฅผ Dept์˜ Listํ˜•ํƒœ๋กœ -->
        select deptno, dname, loc from dept
      </select>
    </mapper>

     

    <mapper namespace="com.dept.DeptMapper2">

    ๋งคํผ์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋„ ์ˆ˜์ •ํ•ด์ฃผ๊ธฐ.

    ์ด๋ ‡๊ฒŒ ๋งคํผ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋“ฑ๋กํ•  ๊ฒฝ์šฐ ๋‚˜์ค‘์— ๋งคํผ1์˜ ๋‚ด์šฉ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์ฟผ๋ฆฌ ์•„์ด๋”” + ๋งคํผ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๊นŒ์ง€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

     <!--  typealias ๋“ฑ๋ก -->
    <typeAliases>
    	<typeAlias type="com.dto.Dept" alias="Dept"></typeAlias>
    </typeAliases>

    ์ด๋Ÿด ๊ฒฝ์šฐ configuration.xmp ์—์„œ ๊ฐ„๋‹จํ•œ ๋ณ„์นญ์„ ์ง€์ •ํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณ„์นญ ์ง€์ • ํ›„ ๋งคํผ์—์„œ ๋‹ค ์ˆ˜์ •ํ•œ ๋’ค ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์—ฐ์Šต๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€