To demonstrate, I did a project. There is no Data Source Management in STS, so we will add the plug-in. 

Check this figure out to understand the Spring Framework structure with MyBatis.

 

The first file that you need to set is pom.xml. This is for the configuration, so you must add the dependencies to use the libraries in the project. 

Next, we need to take care of is web.xml. There are three excellent contents: the location of the servlet class, the servlet mapping, the connection to the servlet-context.xml, and UTF-8 encoding.

 

<servlet-class> and <servlet-mapping>

root-context.xml and servlet-context.xml

encoding : UTF-8

In the servlet-context.xml, you will see the base-package is myBatis1, and the root-context.xml is for database connection. In this file that I am demonstrating, there is nothing much in the root-context.xml, but 

 

In the DeptDaoImpl.java, you will see the @Autowired annotation. This @Autowired annotation is for injecting the SQLs. 

 

In the resources directory, there are configuration files. 

In the configuration.xml, you will see three like the ones below. 

 

<typeAlias>

<property>

<mapper resource>

This project needs six configuration files: pom.xml, web.xml, servlet-context.xml, root-context.xml, and configuration.xml.

 

Compare the mapper class in the Spring to the average MyBatis Model2 Mapper Class; it is way more straightforward in the Spring Framework. From Model2, when we print out the result, we use EL(Expression Language). 

 

Spring Mapper Class

<?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="deptns">
	
	<!-- Use type aliases to avoid typing the full classname every time. -->
	<resultMap id="deptResult"    type="dept">
		<result property="deptno" column="deptno" />
		<result property="dname"  column="dname" />
		<result property="loc"	  column="loc" />
	</resultMap>
	
	<select id="list" resultMap="deptResult">
		select * from dept order by deptno
	</select>
	
	<select id="select" parameterType="int" resultType="dept">
		select * from dept where deptno=#{deptno}
	</select>
	
	<update id="update" parameterType="dept">
		update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
	</update>
	
	<delete id="delete" parameterType="int">
		delete from dept where deptno=#{deptno}
	</delete>
	
</mapper>

 

MyBatis Model2 Mapper Class

<?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="mymember">


	<insert id="insert" parameterType="member">
	  insert into member0609 values(#{id},#{passwd},#{name},#{jumin1},#{jumin2},
	  #{mailid},#{domain},#{tel1},#{tel2},#{tel3},#{phone1},#{phone2},#{phone3},
	  #{post},#{address},#{gender},#{hobby},#{intro},sysdate)
	</insert>
	

	<select id="idcheck" parameterType="String" resultType="member">
	 select * from member0609 where id = #{id}
	</select>
	

	<update id="update" parameterType="member">
	  update member0609 set name=#{name}, jumin1=#{jumin1}, jumin2=#{jumin2}, 
	  mailid=#{mailid}, domain=#{domain}, tel1=#{tel1}, tel2=#{tel2}, tel3=#{tel3},
	  phone1=#{phone1}, phone2=#{phone2}, phone3=#{phone3}, post=#{post}, address=#{address},
	  gender=#{gender}, hobby=#{hobby}, intro=#{intro} where id = #{id}
	</update>


	<delete id="delete" parameterType="String">
	  delete from member0609 where id = #{id}
	</delete> 

</mapper>

 

 

 

+ Recent posts