๐ฅ [Spring] MyBatis & Bean
Backend
์์DB
์ ์ ๊ทผํ ๋ ์ฌ์ฉํ๋ ๊ธฐ์ ์ธ MyBatis ์ ๋ํด ์์๋ณด์
MyBatis ๋?
๋นจ๊ฐ์ ๋ค๋ชจ ๋ถ๋ถ์ด MyBatis ๋์ ์์น
Component๋ Business Logic์ ํธ์ถ
Business Logic์ Resources Factory์์ Connection์ ๋ฐ์์ ์ํํ๋ค.
DAOImpl์ ์ญํ ์ ๋ค์๊ณผ ๊ฐ๋ค.
connection
๋ฐ๊ธฐ- ์ฟผ๋ฆฌ๋ฌธ ์์ฑ - PreparedStatement
- ๊ฐ ๋ฐ์ธ๋ฉ
- ์ฟผ๋ฆฌ๋ฌธ ์ํ
- close
์์ 5๊ฐ์ง ์ญํ ์ ํ๋ ์์ํฌ ํํ๋ก ๋ฌถ์ ์ ์๋ค.
์ด๊ฒ์ด MyBatis Framework ์ด๋ค.
MyBatis Framework
์ ์ญํ ์ ํ๋ ๋ค๋ฅธ Framework
๋ ๋ค์ํ๊ฒ ์กด์ฌํ๋ค.
Spring JDBC(์ผ๋ณธ์ด ๋ง์ด ์ฌ์ฉ), Hibernate(๋ฏธ๊ตญ), MyBatis(ํ๊ตญ) ๋ฑ
๊ตฌํ๋ถ์ ์์ด์ ์์ ์ ํ๋ฆ
1.ํ ์ด๋ธ ์ธํ
1
2
3
4
5
6
7
8
9
10
11
12
create table mysawon(
num number constraint mysawon_num_pk primary key,
id varchar2(20) constraint mysawon_id_nn not null,
pwd varchar2(20),
name varchar2(40),
age number(3),
hiredate date,
constraint mysawon_id_uq unique(id));
create sequence mysawon_seq
increment by 1
start with 1;
2.vo ์์ฑ
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
package com.service.mybatis.vo;
public class MySawon {
private int num, age; //์ปฌ๋ผ๋ช
๊ณผ ๋์ผ
private String id, pwd, name, hiredate; //์ปฌ๋ผ๋ช
๊ณผ ๋์ผ
//Framework์์๋ ๋ฐ๋์ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ํ์...
public MySawon() {}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "MySawon [num=" + num + ", age=" + age + ", id=" + id + ", pwd=" + pwd + ", name=" + name + ", hiredate="
+ hiredate + "]";
}
}
3.MyBatis Framework ์ค์ ๋ฌธ์๋ฅผ ๋ฑ๋ก
dbconn.properties (DB ์๋ฒ ์ ๋ณด)
1
2
3
4
5
### dbconn.properties file....dbServer Information Storing
jdbc.oracle.driver=oracle.jdbc.driver.OracleDriver
jdbc.oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:XE
jdbc.oracle.username=dbid
jdbc.oracle.password=1234
sqlMapConfig.xml (MyBatis Framework์ ํต์ฌ ๋ฌธ์)
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
<?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">
<!--
MyBatis์ ํต์ฌ์ด ๋๋ ์ค์ ๋ฌธ์๋ก์
1. db์๋ฒ์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค...dbconn.properties ํ์ผ์ ์๊ธฐ์ Wiring๋์ด์ง๋ค.
2. DataSource์ ๋ํ ์ ๋ณด๋ฅผ ๋ฑ๋ก
3. sql Query๋ฌธ์ ๋ํ ์ ๋ณด๋ฅผ ๋ฑ๋ก...mapper.xmlํ์ผ์ ์๊ธฐ์ Wiring ๋์ด์ง๋ค.
4. VO๋ฅผ ์ฌ๊ธฐ๋ค ๋งคํ์ํจ๋ค..์๋ฆฌ์ผ์ค๋ฅผ ๊ธฐ์ตํ์!!
-->
<configuration>
<properties resource="config/dbconn.properties"/>
<typeAliases><!--vo๋ฅผ ๋งคํ -->
<typeAlias type="com.service.mybatis.vo.MySawon" alias="mySawon"/>
</typeAliases>
<environments default="AA">
<environment id="AA">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED"> <!--DriverManager ๋ฐฉ์ ๋ง์ฝ pooled๋ก ํ๋ฉด ResourcesFactory ๋ฐฉ์์ด๋ค-->
<!--setDriver(); -->
<!-- value์๋ dbconn.properties๋ฅผ ๋ณด๊ณ key๊ฐ์ด ๋ค์ด๊ฐ๋ค -->
<property name="driver" value="${jdbc.oracle.driver}"/>
<property name="url" value="${jdbc.oracle.url}"/>
<property name="username" value="${jdbc.oracle.username}"/>
<property name="password" value="${jdbc.oracle.password}"/>
</dataSource>
</environment>
</environments>
<!--sql ์ฟผ๋ฆฌ๋ฌธ ๋ฑ๋ก๋ ๋ฉํ๋ฐ์ดํฐ --> mapper๊ฐ ์ถ๊ฐ๋ ๋๋ง๋ค ์ถ๊ฐํด์ ์์ฑํด์ค์ผํจ
<mappers>
<mapper resource="mapper/mysawon-mapping.xml"/>
</mappers>
</configuration>
4.mysawon-mapping.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- config๋ฅผ ๋ชจ๋ mapper๋ก ๋ฐ๊ฟ์ผ ํจ -->
<?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="SawonMapper">
<insert id="sawonAdd" parameterType="mySawon">
INSERT
INTO mysawon (num, id, pwd, name, age, hiredate)
VALUES (mysawon_seq.nextVal, #{id},#{pwd},#{name},#{age},sysdate)
</insert>
</mapper>
SqlMapConfig.xml
์ vo
, mysawon.xml
, dbconn.properties
์ ๋ชจ๋ hasing
, ๋ชจ๋ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
SqlSessionFactory
๊ฐ SqlMapConfig
์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ ๊ฐ๋ค.
SqlSession
์ SqlSessionFactory
์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ ๊ฐ๋ค.
์ฆ, SqlMapConfig
๊ฐ DI
์ SqlSessionFactory
์ ํด์ฃผ๊ณ SqlSessionFactory
๊ฐ DI
์ SqlSession
์
ํด์ค๋ค.
SqlSession
์ DB
์ ์ํตํ๋ค.
insert(), delete(), update(), selectList(), selectOne() ๋ฑ
MyBatis ์ Null ์ฒ๋ฆฌ
DB์ null
๊ฐ์ด ๋ค์ด๊ฐ๋ฉด ํฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
1
2
3
4
//==> Test ์ฉ User instance ์์ฑ ๋ฐ age / regData null setting
//==>@@@ null๊ฐ์ ํ์ฉํ ๋ ค๋ฉด SqlMapConfig01.xml์ Settings ํ๊ทธ๋ฅผ ๋ถ์ฐฉํด์ผํ๋ค.@@@
User user = new User("user04","์ฃผ๋ชฝ","user04",null,1);
user.setRegDate(null);
์์ ๊ฐ์ด null
๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ฉด ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
Oracle
์ null
๊ฐ ํ์ฉ์ด ๋์ง ์์ผ๋ฏ๋ก
๋ฐ๋ผ์ MyBatis
์์ null
์ฒ๋ฆฌํด์ผํ๋ค.
SqlMapConfig01.xml
๋ก ๋ค์ด๊ฐ์ ํ์ผ์ ์์ ํด ์ฃผ์ด์ผ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>
<properties resource="config/dbconn.properties"/>
<!--์ค๋ผํด์์๋ null ๊ฐ์ ํ์ฉํ๋ ์ต์
์ ์ง์ :: MySqL์์๋ ์ด๋ถ๋ถ์ด ๊ธฐ๋ณธ์ ์ผ๋ก ๋ด์ฅ๋์ด ์๋ค. -->
<settings>
<setting name="jdbcTypeForNull" value="NULL"/>
</settings>
<typeAliases> <!--vo๊ฐ ์ฌ๋ฌ๊ฐ๋ฉด ์ฌ๋ฌ๊ฐ ๋ง๋ค์ด์ผ ํ๋ค. alias๋ ์๋ฌธ์๋ก ์ค์ผ ํ๋ค -->
<!-- <typeAlias type="mybatis.services.domain.User" alias="user"/> -->
<package name="mybatis.services.domain"/> <!-- ํจํค์ง ์์ ๋ค์ด์๋ User๊ฐ user๋ก alias ๋๋ค. -->
</typeAliases>
</configuration>
Alias Skip
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
<?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">
<!--์ด๊ฑฐ ์์ฑํ๋
CRUD ๋ค ํด๋ณด๋ ๊ฒ์ -->
<mapper namespace="UserMapper07">
<!-- AS ์์ฐ๊ธฐ ์ํด -->
<!--๊ฒฐ๊ณผ๋งต์ ๋ช
์์ ์ผ๋ก ์ง์ ํ๋ฉด SELECT ํ๊ทธ๋ง๋ค AS ์ฌ์ฉ ์ํด๋ ๋๋ค. -->
<!--resultType ๋์ ์ resultMap์ ์์ผ๋ก๋ ์ฌ์ฉํ๋ค. -->
<resultMap type="user" id="userSelectMap">
<result property="userId" column="user_id"/>
<result property="userName" column="user_name"/>
<result property="password" column="password"/>
<result property="age" column="age"/>
<result property="grade" column="grade"/>
<result property="regDate" column="reg_date"/>
</resultMap>
<!-- id๊ฐ user01์ธ ์ฌ๋์ ๊ฒ์ํ๋ ์ฟผ๋ฆฌ -->
<select id="getUserList01" parameterType="user" resultMap="userSelectMap">
SELECT
user_id ,
user_name ,
password,
age,
grade,
reg_date
FROM users
WHERE user_id=#{userId}
</select>
<!--๋งค๋ฒ AS ์ฐ๋ฉด ๊ท์ฐฎ -->
</mapper>
MyBatis Dynamic SQL
์๋จ์ ์ฟผ๋ฆฌ๋ ์ ์ ์ธ ์ฟผ๋ฆฌ๋ผ Business Logic
์ด ๋ง์ฝ 100๊ฐ๋ฉด 100๊ฐ์ Query
๋ฅผ ๋ง๋ค์ด์ผํ๋ค.
๋ฐ๋ผ์ ์ข ๋ ๋์ ์ธ ์ฟผ๋ฆฌ๋ฅผ ๋ง๋ค์ด์ผํ๋ค.
์๋ฅผ ๋ค์ด ์๋ 4๊ฐ์ ์ฟผ๋ฆฌ๋ฅผ ํ๋ฒ์ ์ฒ๋ฆฌํ ์ ์๋ค.
- userName์ด โํ๊ธธ๋โ ์ด๊ณ age=30 ์ธ ์ฌ๋์ ๊ฒ์
- userName๋ง โํ๊ธธ๋โ ์ธ ์ฌ๋์ ๊ฒ์
- age=30 ์ธ ์ฌ๋ ๊ฒ์
- ๋ชจ๋ ์ฌ๋์ ๊ฒ์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<select id="getUserList" parameterType="user" resultMap="userSelectMap">
SELECT
user_id ,
user_name ,
password,
age,
grade,
reg_date
FROM users
<where>
<if test="userName != null">
user_name LIKE #{userName}
</if>
<if test="age != null">
OR age LIKE #{age}
</if>
</where>
ORDER BY user_id DESC
</select>
์์ ๊ฐ์ SQL ๊ตฌ๋ฌธ์ ์ง ๋ค.
<where></where>
๊ตฌ๋ฌธ์ ์ฌ์ฉํด WHERE ์ ์ ๋๋ ์ฃผ์๋ค.
๋๋ ์ค ๋๋ <if></if>
๋ฅผ ์ฌ์ฉํด ์กฐ๊ฑด๋ฌธ์ ์ฌ์ฉํด ์ฃผ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
User user = new User("user01","ํ๊ธธ๋","user01",new Integer(30),1);
//์ด๋ฆ์ด ํ๊ธธ๋์ด๊ณ age=30์ธ
(List)session.selectList("UserMapper08.getUserList",user);
//age=30์ธ ์ฌ๋์ ๊ฒ์
user.setUserName(null);
(List)session.selectList("UserMapper08.getUserList",user);
//์ด๋ฆ์ด ํ๊ธธ๋์ธ ์ฌ๋์ ๊ฒ์
user.setUserName("ํ๊ธธ๋");
user.setAge(null);
(List)session.selectList("UserMapper08.getUserList",user);
//๋ชจ๋ ์ฌ๋์ ๊ฒ์
user.setUserName(null);
user.setAge(null);
(List)session.selectList("UserMapper08.getUserList",user);
SQL ๊ตฌ๋ฌธ์ ๋ชจ๋ํ
์๋ง์ ์ฟผ๋ฆฌ ์ค์ ๊ณตํต๋๋ ๋ถ๋ถ์ ๋ฐ๋ก ๋ชจ๋ํ๋ฅผ ํ๋ ๊ฒ์ด ์ข๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<sql id="select-users">
SELECT
user_id ,
user_name ,
password,
age,
grade,
reg_date
FROM
users
</sql>
<sql id="orderby-userid-desc">
ORDER BY user_id DESC
</sql>
์์ ๊ฐ์ด <sql></sql>
๊ตฌ๋ฌธ์ ์ฌ์ฉํด ๋ชจ๋ํ๋ฅผ ์งํํ๋ค.
<sql>
์์ id๋ฅผ ์ค์ ํด ์ฐธ์กฐํ ์ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<select id="getUser" parameterType="user" resultMap="userSelectMap">
<include refid="select-users" />
WHERE password=#{password}
</select>
<select id="getUserList" parameterType="user" resultMap="userSelectMap">
<include refid="select-users"></include>
<where>
<if test="userName != null">
user_name LIKE #{userName}
</if>
<if test="age != null">
OR age LIKE #{age}
</if>
</where>
<include refid="orderby-userid-desc"></include>
</select>
<include>
ํ๊ทธ๋ฅผ ์ฌ์ฉํด refid ์ ์์ฑ๊ฐ์ sql ํ๊ทธ์ id๊ฐ ๋ฃ์ผ๋ฉด ๋๋ค.
MyBatis
์์ ๊ฐ์ฅ ํต์ฌ๋ฌธ๋ SqlMapConfig.xml
์ด๋ค.
์ด ๋ฌธ์๋ SqlSessionFactory
๊ฐ ๋จน๊ณ ,
SqlSessionFactory
๋ SqlSession
์ด ๋จน๊ฒ ๋๋ค.
MyBatis
์๋ SqlSession
์ด ์ต์์์ง๋ง
์ด๊ฒ์ WAS
์ ๋๊ฒจ์ผ ํ๋ค.
๋ฐ๋ ๊ฒ์ Business Logic Layer
์์ DAOImpl
์ด ๋จน๊ฒ ๋๋ค.
DAO
์ธํฐํ์ด์ค์๋ 5๊ฐ์ ํ
ํ๋ฆฟ์ด ๋ง๋ค์ด ์ ธ์ผ ํ๋ค.
mybatis-userservice-mapping ์์ ์ฟผ๋ฆฌ๋ฅผ 5๊ฐ ์งฐ๊ธฐ ๋๋ฌธ์ด๋ค.
๋ํ DAO ์ธํ ํ์ด์ค ํ ํ๋ฆฟ ์ด๋ฆ์ ๊ฐ๊ฐ ์ฟผ๋ฆฌ 5๊ฐ์ id๋ก ํด์ผํ๋ค.
์ฆ, ๋ชจ๋ ๊ฒ์ ์ฐ๊ฒฐ๋์ด์ผ ํ๋ค.
๋จผ์ mybatis-userservice-mapping10.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
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
<?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">
<!--์ด๊ฑฐ ์์ฑํ๋
CRUD ๋ค ํด๋ณด๋ ๊ฒ์ -->
<mapper namespace="UserMapper10">
<resultMap type="user" id="userSelectMap">
<result property="userId" column="user_id" />
<result property="userName" column="user_name" />
<result property="password" column="password" />
<result property="age" column="age" />
<result property="grade" column="grade" />
<result property="regDate" column="reg_date" />
</resultMap>
<sql id="select-users">
SELECT
user_id ,
user_name ,
password,
age,
grade,
reg_date
FROM
users
</sql>
<sql id="orderby-userid-desc">
ORDER BY user_id DESC
</sql>
<select id="getUser" parameterType="user" resultMap="userSelectMap">
<include refid="select-users" />
WHERE password=#{password}
</select>
<select id="getUserList" parameterType="user" resultMap="userSelectMap">
<include refid="select-users"></include>
<where>
<if test="userName != null">
user_name LIKE #{userName}
</if>
<if test="age != null">
OR age LIKE #{age}
</if>
</where>
<include refid="orderby-userid-desc"></include>
</select>
<insert id="addUser" parameterType="user">
INSERT
INTO users(user_id, user_name, password, age, grade, reg_date)
VALUES(
#{userId}, #{userName}, #{password},#{age},#{grade},#{regDate}
)
</insert>
<update id="updateUser" parameterType="user">
UPDATE users
SET user_name=#{userName}
WHERE user_id=#{userId}
</update>
<delete id="removeUser" parameterType="string">
DELETE
users
WHERE user_id=#{VALUE}
</delete>
</mapper>
์ฟผ๋ฆฌ์ parameterType
๊ณผ id
, resultMap
์ type์ ์ ์ดํด๋ณด์.
์ด ์ฟผ๋ฆฌ๋ก ๋ง๋ค์ด์ง ๋ฐ์ดํฐ๋ฅผ ์ด์ WAS์ DAO๊ฐ ๋ฐ์์ผ ํ๋ค.
์ฟผ๋ฆฌ๋ฌธ tag id ๊ฐ์ด Template ๊ธฐ๋ฅ์ ์ด๋ฆ์ด ๋๋ค.
์ฟผ๋ฆฌ๋ฌธ tag parameterType์ด Template ๊ธฐ๋ฅ์ ์ธ์ ๊ฐ
์ฟผ๋ฆฌ๋ฌธ tag resultMap, resultType์ ๊ฐ์ด Template๊ธฐ๋ฅ์ return type๊ณผ ์ฐ๊ฒฐ
1
2
3
4
5
6
7
public interface UserDAO {
int addUser(User user)throws Exception;
int updateUser(User user)throws Exception;
int removeUser(String userId)throws Exception;
User getUser(String userId)throws Exception;
List<User> getUserList(User user)throws Exception;
}
์ด์ ์ด interface๋ฅผ ๊ตฌํํด์ค์ผ ํ๋ค.
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
public class MyBatisUserDAOImpl10 implements UserDAO{
private SqlSession sqlSession;
public static final String MAPPER_NAME="UserMapper10.";
//DI... ์์ฑ
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
System.out.println("::" + getClass().getName() + ".setSqlSession()...");
}
@Override
public int addUser(User user) throws Exception {
int result = sqlSession.insert(MAPPER_NAME + "addUser", user);
sqlSession.commit();
return result;
}
@Override
public int updateUser(User user) throws Exception {
int result = sqlSession.update(MAPPER_NAME+"updateUser", user);
sqlSession.commit();
return result;
}
@Override
public int removeUser(String userId) throws Exception {
int result = sqlSession.delete(MAPPER_NAME+"removeUser", userId);
sqlSession.commit();
return result;
}
@Override
public User getUser(String userId) throws Exception {
return sqlSession.selectOne(MAPPER_NAME+"getUser", userId);
}
@Override
public List<User> getUserList(User user) throws Exception {
return sqlSession.selectList(MAPPER_NAME+"getUserList",user);
}
}
DAO๋ SqlSession์ผ๋ก ๋ถํฐ DI ๋๋ ๊ฒ์ด๋ค.
1
2
3
4
5
6
7
private SqlSession sqlSession;
//DI... ์์ฑ
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
System.out.println("::" + getClass().getName() + ".setSqlSession()...");
}
setSqlSession()
์ ํตํด sqlSession
์ ์ป๋ ๊ฒ์ด๋ค.
๊ทธ๋ผ ์ด๋ป๊ฒ ํ์ฉํ ๊น? ํ ์คํธ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
1
2
3
4
5
6
7
8
9
10
SqlSession session = null;
//==> TestUtil ์ getSqlSessionFactory()์ ์ด์ฉ SqlSessionFactory instance GET
SqlSessionFactory factory = TestUtil.getSqlSessionFactory();
session=factory.openSession();
MyBatisUserDAOImpl10 dao = new MyBatisUserDAOImpl10();
dao.setSqlSession(session); //DI ์ฃผ์
!!!!!!!!!!!!!!!!!!!!!!!!!
User user = new User("user04","์ฃผ๋ชฝ","user04",null,1);
dao.addUser(user)
์ด๋ฐ ์์ผ๋ก DI
๋ฅผ ํตํด ๋ฐ์์จ SqlSession
์ ํ์ฉํ ์ ์๋ค.
Business Logic Layer ๋ถ๋ฆฌ
ํ์ ์์๋ Business Ligic Layser๋ฅผ ๋๋ก ๋๋ ์ ์๋ค.
Service Layer
์ Persistence Layer
๋ก ๋๋ ์ ์๋ค.
Persistence Layer์์ DB์ ์ ๋ณด๋ฅผ ๋ฐ์์ค๊ณ
Service Layer์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณต ํ๋ ๊ฒ
์๋ฅผ ๋ค์ด 1000๊ฐ์ column์ Persistence Layer
์์ ๊ฐ์ ธ์ค๋ฉด
ํ ํ๋ฉด์ ๋์ค๋ฉด ๋๋ฌด ๋ง์ผ๋๊น Service Layer์์ 10๊ฐ์ฉ ํ์ด์ง์ ๋ณด์ด๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค
๋จผ์ Service Layer๋ฅผ ๋ง๋ค์ด ์ฃผ์
1
2
3
4
5
6
7
8
9
10
11
12
package mybatis.services.user;
import java.util.List;
import mybatis.services.domain.User;
public interface UserService {
void addUser(User user)throws Exception;
void updateUser(User user)throws Exception;
User getUser(String userId)throws Exception;
List<User> getUserList(User user)throws Exception;
}
DAO
์ ๋งค์ฐ ์ ์ฌํ๋ค
์์ ๋ ์ ์ด remove
์ฟผ๋ฆฌ๊ฐ ๋น ์ก๋ค.
์๋ํ๋ฉด Service Layer
์์๋ ๊ตณ์ด ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ ์ด์ ๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
๋ฐ์ดํฐ๋ง ๊ฐ๊ณตํ๋ฉด ๋๋ค.
๋ํ addUser()
์ updateUser()
์ return ํ์
์ด void๋ก ๋ฐ๊พธ์๋ค.
๊ตณ์ด ์ฟผ๋ฆฌ๊ฐ ์ ๋๋ก ๋์๊ฐ๋์ง ํ์ธํ ํ์๊ฐ ์๋ ๊ฒ์ด๋ค.
์ interface๋ฅผ ๊ตฌํํด์ฃผ์.
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
public class MyBatisUserServiceImpl11 implements UserService{
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
System.out.println("::" +getClass().getName() + "sqlSesseion()");
}
@Override
public void addUser(User user) throws Exception {
userDAO.addUser(user);
}
@Override
public void updateUser(User user) throws Exception {
userDAO.updateUser(user);
}
@Override
public User getUser(String userId) throws Exception {
return userDAO.getUser(userId);
}
@Override
public List<User> getUserList(User user) throws Exception {
return userDAO.getUserList(user);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SqlSession session = null;
//==> TestUtil ์ getSqlSessionFactory()์ ์ด์ฉ SqlSessionFactory instance GET
SqlSessionFactory factory = TestUtil.getSqlSessionFactory();
session=factory.openSession();
//==> MyBatisUserDAOImpl10 ์์ฑ ๋ฐ SqlSession ๊ฐ์ฒด setter injection
MyBatisUserDAOImpl10 dao = new MyBatisUserDAOImpl10();
dao.setSqlSession(session);
//==> IBatisUserServiceImpl11 ์์ฑ ๋ฐ IBatisUserDAOImpl10 ๊ฐ์ฒด setter injection
MyBatisUserServiceImpl11 userService = new MyBatisUserServiceImpl11();
userService.setUserDAO(dao);
User user = new User("user04","์ฃผ๋ชฝ","user04",null,1);
userService.addUser(user);
DAO์ SqlSession์ ๋ฐ๊ณ ๊ทธ๊ฒ์ ๋ค์ Service์ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ๊ฒ์ด๋ค.
DI Bean ์ฐ๊ฒฐ
5๊ฐ์ bean์ ๋ง๋ ๋ค.
์ฌ์ค SqlSessionFactory
๊ฐ SqlMapConfig
๋ง ๊ฐ์ ธ๊ฐ๋ ๊ฒ์ด ์๋๋ค. DataSourceFactory
์ Connection
์
๋ฐ์์จ๋ค. ๋ฐ๋ผ์ Connection
์ ๋ณด๊ดํ๋ Factory Bean, Connection
๊ณผ SqlMapConfig
์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ๊ฐ
SqlSessionFactory Bean, ์ด๊ฒ์ ๋ชจ๋ ๊ฐ์ ธ๊ฐ SqlSession Bean, SqlSession์ ์ ๋ณด๋ฅผ ๋ฐ์
MyBatisUserDAOImpl Bean, ์ด๊ฒ์ ๋ชจ๋ ๊ฐ์ ธ๊ฐ๋ UserServiceImpl Bean ๋ฐ๋ผ์ ์ด 5๊ฐ์ด๋ค.
์ด์ bean
์ ๋ฑ๋กํ ์ค์ ๋ฌธ์ ๋ง๋ ๋ค.
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
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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-4.0.xsd">
<!--
1. DataSource ๋น ์ ์ :: API Bean
2. SqlSessionFactory ๋น ์ ์ :: API Bean
3. SqlSession ๋น ์ ์ :: API Bean
4. UserDAOImpl ๋น ์ ์ :: User Definition Bean
5. UserServiceImpl ๋น ์ ์ :: User Definition Bean
-->
<context:property-placeholder location="classpath:config/dbconn.properties"/>
<!--MyBatis Framework API Bean -->
DataSource Bean(Factory Bean)
<bean id = "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.oracle.driver}"/>
<property name="url" value="${jdbc.oracle.url}"/>
<property name="username" value="${jdbc.oracle.username}"/>
<property name="password" value="${jdbc.oracle.password}"/>
</bean>
SqlSessionFactory Bean
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="config/SqlMapConfig01.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
SqlSession Bean
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean>
<!--User Definition Bean -->
UserDAOImpl Bean
<bean id="myBatisUserDAOImpl12" class="mybatis.services.user.impl.MyBatisUserDAOImpl12">
<property name="sqlSession" ref="sqlSessionTemplate"/>
</bean>
UserServiceImpl Bean
<bean id="myBatisUserServiceImpl12" class="mybatis.services.user.impl.MyBatisUserServiceImpl12">
<property name="userDAO" ref="myBatisUserDAOImpl12"/>
</bean>
</beans>
์ ๋ณด๋ฉด ref
๋ก ์๋ก๋ฅผ ์ฐ๊ฒฐํ๊ณ ์๋ค.
๋ฐ๋ผ์ ๋ง์ง๋ง bean๋ง ๊ฐ์ ธ์ค๋ฉด ๋ชจ๋ ์ฎ์ฌ์๋ ๊ฒ์ด๋ค.
์ด์ ๋น์ ํ์ฉํ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
1
2
3
4
5
ApplicationContext context =new ClassPathXmlApplicationContext(new String[] {"/bean/userservice12.xml"});
UserService userService = (UserService)context.getBean("myBatisUserServiceImpl12");
User user = new User("user04","์ฃผ๋ชฝ","user04",40,40);
userService.addUser(user);
getBean()
์ ์ฌ์ฉํ๋ฉด์ ์ธ์๋ก myBatisUserServiceImpl12
๊ฐ ๋ค์ด๊ฐ๋ค.
์ค์ํ์ ์ ์ฒซ๋ฒ์งธ ๊ธ์๊ฐ ์๋ฌธ์๋ก ๋ค์ด๊ฐ๋ค. MyBatisUserServiceImpl12
๊ฐ ์๋ ๊ฒ์ด๋ค.
๋น์ฐํ๊ฒ์ด bean์ฃผ๋ฌธ์ id๊ฐ myBatisUserServiceImpl12๊ฐ ์ด๊ธฐ ๋๋ฌธ์ด๋ค.
๋์ค์ ๋ฐฐ์ธ @Autowired
๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ ์ผ๋ก ์๋ฌธ์๋ก ๋ฐ๋๊ธฐ ๋๋ฌธ์ ์ฃผ์ํด์ผ ํ๋ค.
๋ฒ์ธ๋ก Bean์ ํ์ฉํ๋ฉด Sql์์ Commit() ์ ํ ํ์๊ฐ ์๋ค. ์๋์ ์ผ๋ก ํด์ค๋ค.
๋ฐ๋ผ์ DAOImpl์์ sqlSession.commit()
์ ์ฃผ์ ์ฒ๋ฆฌํด์ค์ผ ํ๋ค.
Annotation ์ ํ์ฉํ Bean ์ฐ๊ฒฐ
Annotation
์ bean
์ฃผ๋ฌธ์๋ฅผ ์ถ์ฝํ๋ ๊ฒ์ด๋ค.
์ฐ๋ฆฌ๊ฐ ์์์ ๋ดค๋ bean
์ฃผ๋ฌธ์์ <bean></bean>
์ @Component
๋ฅผ ์ฌ์ฉํด ๋์ฒดํ ์ ์๋ค.
1
2
3
4
5
6
7
8
9
@Component
public class MyBatisUserDAOImpl13 implements UserDAO{
...
}
@Component
public class MyBatisUserServiceImpl13 implements UserService{
...
}
์ฌ์ค Service Layer์ Persistence Layer ๋ชจ๋ @Component
๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
ํ์ง๋ง ์ ๋ฌธ๊ฐ๋ค์ @Component ์ ์์ด๋ค.
PersistenceLayer์๋ @Repository
ServiceLayer์๋ @Service
๊ทธ ๋ค์ ํ๋๊ฐ ์ ์ธ ๋งจ ์์ @Autowired
๋ฅผ ์ฌ์ฉํด bean์ ์๋ก ์ฎ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Repository
public class MyBatisUserDAOImpl13 implements UserDAO{
@Autowired //setterํ ํ์์์
private SqlSession sqlSession;
...
}
@Service
public class MyBatisUserServiceImpl13 implements UserService{
@Autowired
private UserDAO userDAO;
...
}
์ด๋ ๊ฒ ์์ฑํ๊ฒ ๋๋ฉด ์์์ ๋ดค๋ bean ์ฃผ๋ฌธ์์์ User Definition Bean์ ๋ชจ๋ ์ง์๋ ๋๋ค.!!
Annotation์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ค.
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
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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-4.0.xsd">
<context:property-placeholder location="classpath:config/dbconn.properties"/>
<!--MyBatis Framework API Bean -->
<bean id = "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.oracle.driver}"/>
<property name="url" value="${jdbc.oracle.url}"/>
<property name="username" value="${jdbc.oracle.username}"/>
<property name="password" value="${jdbc.oracle.password}"/>
</bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="config/SqlMapConfig01.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean>
<!-- DI์๊ฒ base-package ํ์๋ฅผ ๊ผญ ๋ด๋ผ๊ณ ์๋ ค์ค๋ค. -->
<context:component-scan base-package="mybatis.services.user.impl"></context:component-scan>
</beans>
๋ง์ง๋ง์ผ๋ก <context:component-scan>
ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋ค.
์์์ Annotation์ ์์ฑํ ๋ฌธ์๊ฐ ์์นํ package ๋ช
์ base-package
์ ๊ฐ์ผ๋ก ๋ฃ์ด์ค๋ค.
๊ทธ๋ ๊ฒ ๋๋ฉด Spring์ ์์น๋ฅผ ๋ณด๊ณ Annotation ์ ์ธ๋ ํด๋์ค๋ฅผ bean์ผ๋ก ๋ฑ๋กํ๋ ๊ฒ์ด๋ค.