Title: MyBatis
1MyBatis
? ??? ???? (dispatcher-servlet.xml)
lt?xml version"1.0" encoding"UTF-8"?gt lt!
?? ?? ?? --gt ltbean id"sqlSessionFactory"
class"org.mybatis.spring.SqlSessionFactoryBean"gt
ltproperty name"dataSource"
ref"dataSource" /gt ltproperty
name"configLocation" value"/WEB-INF/MapperConfig
.xml" /gt lt/beangt ltbean
id"sqlSessionTemplate" class"org.mybatis.spring.
SqlSessionTemplate"gt ltconstructor-arg
ref"sqlSessionFactory" /gt lt/beangt
?dispatcher-servlet.xml ? MyBatis? ???? ?? ?? ??
(SqlSessionFactoryBean / SqlSessionTemplate) ?M
yBatis ?? XML ??? ?? ??
2MyBatis
? ??? ???? (dispatcher-servlet.xml)
lt?xml version"1.0" encoding"UTF-8"?gt lt!
?? ?? ?? --gt ltbean id"memberDao"
class"org.mybatis.spring.mapper.MapperFactoryBean
"gt ltproperty name"mapperInterface"
valueedu.spring.board.dao.MemberDao" /gt
ltproperty name"sqlSessionFactory"
ref"sqlSessionFactory" /gt ltproperty
name"sqlSessionTemplate" ref"sqlSessionTemplate"
/gt lt/beangt
?Spring Jdbc Template / iBatis Template ? Dao
????? ?? Query ? ???? ?? XML ??? ??????
MyBatis ? XML? Dao ???(?????)? ???? ???? ?? ?
Dao ??? ??? Query? ???? ??? ?? ?Dao ? ?? Bean ??
???? ?? ????.. (?? ?? ??? ??)
3MyBatis
? MyBatis ?? ?? (MapperConfig.xml)
lt?xml version"1.0" encoding"UTF-8" ?gt lt!DOCTYPE
configuration PUBLIC "-//mybatis.org//DTD
Config 3.0//EN" "http//mybatis.org/
dtd/mybatis-3-config.dtd"gt ltconfigurationgt ltsett
ingsgt ltsetting name"mapUnderscoreToCamelCase"
value"true" /gt lt/settingsgt lttypeAliasesgt
lttypeAlias alias"String" type"java.lang.String"
/gt lt/typeAliasesgt ltmappersgt ltmapper
resource"edu/spring/board/dao/MemberDao.xml"
/gt lt/mappersgt lt/configurationgt
MyBatis ??? ??? ?? ??
??? ??? ????? ?? ??
? sqlMap ??? ?? ??
?MyBatis ?? XML ??? ?? ???? Query (SqlMap) ???
??? ??
4MyBatis
? MyBatis Sql Map ?? (?? ???.xml)
lt?xml version"1.0" encoding"UTF-8" ?gt lt!DOCTYPE
mapper PUBLIC "-//mybatis.org//DTD Mapper
3.0//EN" "http//mybatis.org/dtd/myb
atis-3-mapper.dtd"gt ltmapper namespaceedu.spring
.board.dao.MemberDao"gt ltselect
id"selectMember" parameterType"java.util.Map"
resultType"java.util.Map"gt // Query
lt/selectgt ltinsert id"insertMember"
parameterType"edu.spring.board.dao.MemberDto"gt
// Query lt/insertgt lt/mappergt
iBatis ? namespace? ???? ??? MyBatis ??? ???? ???
Dao ???? ??
select / insert / update / delete ?? ??? ?? ????
? SQL? ??
5MyBatis
? MyBatis Sql Map ?? (?? ???.xml) - select
ltselect id"selectMember" resultType"java.util.Ma
p"
parameterType"java.util.Map"gt SELECT ID, PW,
NAME, GRADE FROM MEMBER WHERE 1 1
AND ID id lt/selectgt
???? ???? ?? ??? ??? ?? ??? ?? ??? resultType ?
??? ???? ???? ?? ???? parameterType ? ????
? ????? ???? parameterType? ??? key? ???
?? ex) MapltString, Objectgt paramMap new
HashMapltString, Objectgt()
paramMap(inputId, seorab) ?? ?? ???
???? parameterType ?? ??? ?? inputId ? ??
6MyBatis
? MyBatis Sql Map ?? (?? ???.xml) - insert
ltinsert idinsertMember" parameterType"java.util
.Map"gt INSERT INTO MEMBER VALUES (
id, pw, name, grade,
DATE_FORMAT(NOW(), 'YmdHiS') ) lt/selectgt
???? ???? ?? ????? ???? ?? ???? parameterType ?
???? ? ???? ??? ?? Integer ??? ?? ? resultType
?? ??
7MyBatis
? MyBatis Sql Map ?? (?? ???.xml) - update
ltupdate idupdateMember" parameterType"java.util
.Map"gt UPDATE MEMBER SET ID
id, PW pw, NAME name, GRADE
grade , ACCEPT_DATE
DATE_FORMAT(NOW(), 'YmdHiS') WHERE ID
id lt/updategt
???? ???? ?? ????? ???? ?? ???? parameterType ?
???? ? ???? ??? ?? Integer ??? ?? ? resultType
?? ??
8MyBatis
? MyBatis Sql Map ?? (?? ???.xml) - delete
ltdelete iddeleteMember" parameterType"java.util
.Map"gt DELETE FROM MEMBER WHERE ID
id lt/updategt
???? ???? ?? ????? ???? ?? ???? parameterType ?
???? ? ???? ??? ?? Integer ??? ?? ? resultType
?? ??
9MyBatis
? MyBatis Sql Map ?? (?? ???.xml) - Dynamic
SQL
ltselect id"selectMember" resultType"java.util.Ma
p"
parameterType"java.util.Map"gt SELECT ID, PW,
NAME, GRADE FROM MEMBER WHERE 1 1
ltif testid ! null and id gt
AND ID id lt/ifgt lt/selectgt
????? ???? ???? ?? ?select / insert / update /
delete ?? ?? ??? ?? ?? ??? http//mybatis.githu
b.io/mybatis-3/ko/ ??
10MyBatis
? MyBatis ??
- Dao ???
public interface MemberDao public int
insertMember(MemberDto memberDto) public
ListltMapltString, Objectgtgt selectMember(MapltString,
Objectgt paramMap)
- Service ???
_at_Service public class MemberService
_at_Autowired private MemberDao memberDao
public boolean join(MemberDto memberDto)
int result memberDao.insertMember(memberDto)
return result gt 0 ? true false
. ?? .
?Dao ?????? ?? ?Service ?? Dao ? ????? Sql Map
XML? ?? ??