JDBC MyBatis 다양한 활용

JDBC MyBatis 다양한 활용

< ?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.DeptMapper" >

< select id = "selectAll" resultType = "Dept" >

select * from dept

< / select >

< insert id = "insert" parameterType = "Dept" >

insert into dept (deptno,

dname, loc) values (#{deptno}, #{dname}, #{loc})

< / insert >

< update id = "update" parameterType = "String" >

update dept set loc=#{loc} where deptno=99

< / update >

< delete id = "delete" parameterType = "int" >

delete from dept where deptno = #{int}

< / delete >

< select id = "selectByDeptno" parameterType = "int"

resultType = "Dept" >

select * from dept where deptno = #{int}

< / select >

< select id = "countRecord" resultType = "int" >

select count(*) from dept

< / select >

< select id = "selectByHashMap" resultType = "hashmap"

parameterType = "int" >

select * from dept where deptno=#{deptno}

< / select >

< select id = "selectDynamicDeptno" resultType = "Dept"

parameterType = "hashmap" >

select * from dept

< if test = "deptno != null" >

where deptno = #{deptno}

< / if >

< / select >

< / 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 = "com.dept.DeptMapper2" >

< select id = "selectByHashMap" parameterType = "hashmap"

resultType = "Dept" >

select * from dept

where deptno = #{key1} or deptno =

#{key2}

< / select >

< select id = "selectDynamicChoose" parameterType = "hashmap"

resultType = "Dept" >

select * from dept

< choose >

< when test = "dname != null" >

where dname = #{dname}

< / when >

< when test = "loc != null" >

where loc = #{loc}

< / when >

< otherwise >

where deptno=10

< / otherwise >

< / choose >

< / select >

< update id = "multiupdate" parameterType = "arraylist" >

update dept set loc = '제주'

where deptno IN

< foreach item = "item" index = "index" collection = "list" open = "("

separator = "," close = ")" >

#{item}

< / foreach >

< / update >

< select id = "multiSelect" parameterType = "arraylist"

resultType = "Dept" >

select *from dept where deptno In

< foreach item = "item" index = "index" collection = "list" open = "("

separator = "," close = ")" >

#{item}

< / foreach >

< / select >

< delete id = "multiDelete" parameterType = "arraylist" >

delete from dept where deptno IN

< foreach item = "item" index = "index" collection = "list" open = "("

separator = "," close = ")" >

#{item}

< / foreach >

< / delete >

< insert id = "multiInsert" parameterType = "arraylist" >

insert all

< foreach item = "item" index = "index" collection = "list" >

into dept (deptno, dname, loc)

values(#{item.deptno}, #{item.dname}, #{item.loc})

< / foreach >

select *

from dual

< / insert >

< / 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 = "com.dept.DeptMapper3" >

< select id = "selectTopN" resultType = "Dept" >

select * from dept

order by 1

asc

< / select >

< select id = "locselectTopN" parameterType = "String"

resultType = "Dept" >

select * from dept

where loc = #{loc}

order by 1 asc

< / select >

< select id = "selectAll2" resultType = "Dept" >

< ![CDATA[select * from dept where deptno > 10

]]>

< / select >

< select id = "selectAll3" resultType = "Dept" >

select deptno, dname, loc from dept

where deptno > 10

< / select >

< select id = "multiSelectmap" parameterType = "hashmap"

resultType = "Dept" >

select * from dept

where deptno In

(#{key1}, #{key2})

< / select >

from http://cocoshin.tistory.com/30 by ccl(A) rewrite - 2021-12-03 03:02:19