quilt code

[iBatis] iBatis (1) 본문

daily/파이썬

[iBatis] iBatis (1)

김뱅쇼 2023. 3. 21. 21:32

1. src/main/dao

 

DAO)


1. MemDao.java


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package kr.co.aiai.dao;
 
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.ibatis.sqlmap.client.SqlMapClient;
 
import kr.co.aiai.model.MemVo;
import kr.co.aiai.ibatis.MySqlMapper;
 
public class MemDao {
    private static SqlMapClient sqlMapper;
 
    public MemDao() {
        sqlMapper = MySqlMapper.getSqlMapper();
    }
 
    public List<MemVo> selectList() {
        List<MemVo> list = null;
        try {
            list = sqlMapper.queryForList("mem.selectList");
        } catch (SQLException e) {
            System.out.println("error:"+e);
        }
        return list;
    }
 
    public MemVo select(String m_id) {
        MemVo vo = null;
        try {
            vo = (MemVo) sqlMapper.queryForObject("mem.select", m_id);
        } catch (SQLException e) {
            System.out.println("[MemDao][select] error: :"+e);
        }
        return vo;
    }
 
    public int insert(MemVo vo) {
        int cnt = 0;
        try {
            cnt = sqlMapper.update("mem.insert", vo); 
        } catch (SQLException e) {
            System.out.println("[MemDao][insert] error: :"+e);
        }
        return cnt;
    }
 
    public int update(MemVo vo)  {
        int cnt = 0;
        try {
            cnt = sqlMapper.update("mem.update", vo); 
        } catch (SQLException e) {
            System.out.println("[MemDao][update] error: :"+e);
        }
        return cnt;
    }
    
    
    public int delete(String m_id) {
        int cnt = 0;
        try {
            cnt = sqlMapper.update("mem.delete", m_id); //ibatis문법 
        } catch (SQLException e) {
            System.out.println("[MemDao][delete] error: :"+e);
        }
        return cnt;
    }
    
    
    public static void main(String[] args) throws SQLException {
        MemDao dao = new MemDao();
        List<MemVo> list = dao.selectList();
//        MemVo ct = new MemVo("3","2","1","4");
//        int cnt = dao.update(ct);
//        System.out.println("cnt: " + cnt);
        
        //select
//        MemVo mv = dao.select("2");
        
        
 
        
//        for(int i=0; i<list.size(); i++) {
//            System.out.println(list.get(i).getM_id());
//        }
 
        //delete
//        dao.delete("1");
        
        //insert, update 여기에서 문구만 바꾸기!
//        int cnt = dao.insert(new MemVo("2","5","5","5"));
        
//        System.out.println(cnt);
    }
 
}
cs


 

iBatis)


2.mem.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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
 
<sqlMap namespace="mem" >
    <select id="mem.select" resultClass="kr.co.aiai.model.MemVo">
        select m_id, m_name, tel, email from mem
        where m_id = #m_id#
    </select>
    
    <select id="mem.selectList" parameterClass="string" resultClass="kr.co.aiai.model.MemVo">
        select m_id, m_name, tel, email from mem
    </select>
    
    <update id="mem.insert" parameterClass="kr.co.aiai.model.MemVo">
        INSERT INTO mem (m_id, m_name, tel, email) VALUES (#m_id#, #m_name#, #tel#, #email#)
    </update>    
    
    <update id="mem.update" parameterClass="kr.co.aiai.model.MemVo">
        update mem set
            m_name = #m_name#,
            tel = #tel#,
            email = #email#
        where
            m_id = #m_id#
    </update>
        
    <update id="mem.delete" parameterClass="string">
        delete from mem where m_id=#m_id#
    </update>
 
</sqlMap>
cs

 


3. MySqlMapper.java


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
package kr.co.aiai.ibatis;
 
import java.io.IOException;
import java.io.Reader;
 
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
 
public class MySqlMapper {
    private static SqlMapClient sqlMapper;
 
    static {
        try {
            
            Reader reader = Resources.getResourceAsReader("kr/co/aiai/ibatis/SqlMapConfig.xml");
            sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException("iBatis instance Error:" + e, e);
        }
    }
 
    public static SqlMapClient getSqlMapper() {
        return sqlMapper;
    }
}
cs

4. SqlMapConfig.xml



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
 
<sqlMapConfig>
 
    <transactionManager type="JDBC" commitRequired="false">
        <dataSource type="SIMPLE">
            <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
            <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521/xe" />
            <property name="JDBC.Username" value="python" />
            <property name="JDBC.Password" value="python" />
        </dataSource>
    </transactionManager>
 
    <sqlMap resource="kr/co/aiai/ibatis/mem.xml" />
 
</sqlMapConfig>
 
cs

 

Model)


5.MemVO.java

 

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package kr.co.aiai.model;
 
public class MemVo {
    private String m_id = "";
    private String m_name = "";
    private String tel = "";
    private String email = "";
 
    
public MemVo() {
}
    
    public MemVo(String m_id, String m_name, String tel, String email) {
        super();
        this.m_id = m_id;
        this.m_name = m_name;
        this.tel = tel;
        this.email = email;
    }
 
    public String getM_id() {
        return m_id;
    }
 
 
    public void setM_id(String m_id) {
        this.m_id = m_id;
    }
 
 
    public String getM_name() {
        return m_name;
    }
 
 
    public void setM_name(String m_name) {
        this.m_name = m_name;
    }
 
    public String getTel() {
        return tel;
    }
 
    public void setTel(String tel) {
        this.tel = tel;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    @Override
    public String toString() {
        return "MemVo [m_id=" + m_id + ", m_name=" + m_name + ", tel=" + tel + ", email=" + email + "]";
    }
    
}
cs


 

 

'daily > 파이썬' 카테고리의 다른 글

[python] crawl - selenium  (0) 2023.04.25
[python] flask  (0) 2023.04.24
[iBatis] iBatis (2)  (0) 2023.03.21
[파이썬] Day02  (0) 2023.03.15
[파이썬] Day01  (0) 2023.03.13