Thursday, January 12, 2012

Using Sql Queries in Mybatis

Mybatis provides XML descriptors such as "<select></select>", "<insert></insert>", "<update></update>","<delete></delete>" for SQL DML statements. The DML statements can be accessed through "id" property in the XML descriptors. The "parameters" descriptor defines the user parameters passed to the DML statements and "resultType" descriptor defines the type of result the SQL statement returns. The parameters are used in the SQL statments with #{PARAMETER_NAME}. Here is the sample of basic DML operations in Mybatis.

<select id="getEmployee" parameterType="java.util.Map" resultType="java.util.Map">
select * from employee
where emp_id=#{EMP_ID}
and emp_name like #{EMP_NAME}||'%'
</select>


<insert id="insertEmployee" parameterType="java.util.Map">
insert into employee(emp_id,emp_name) values (#{EMP_ID},#{EMP_NAME})
</insert>


<update id="updateEmployee" parameterType="java.util.Map">
update employee set emp_name=#{EMP_NAME}
where emp_id=#{EMP_ID}
</update>


<delete id="deleteEmployee" parameterType="java.util.Map">
delete from employee
where emp_id=#{EMP_ID}
</delete>


Unlike SQL statements in oracle semi-colon must be avoided in the DML statements at the end.

No comments: