Simple Example of Mybatis JAVA Maven Implementation 3 – How java interface and xml interface are connected

We will go through the analysis and talk about how the xml interface and java interface are connected.

1. The xml name space matches the java interface package name, and the xml file name matches the java interface file (class) name.

<!-- CategoryMapper.xml ->
<?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.springriver.example.mybatis.mapper.CategoryMapper">
...
</mapper>


<!--CategoryMapper.java ->
package com.springriver.example.mybatis.mapper;

import com.springriver.example.mybatis.bean.Category;

public interface CategoryMapper {
...
}

2. The id in the xml file matches the method name in the java file, parameterType in xml matches the parameter in java method, and the resultType in xml matches return type in java method

<!--CategoryMapper.xml ->
<?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.springriver.example.mybatis.mapper.CategoryMapper">

<select id="selectCategoryById" parameterType="int" resultType="com.springriver.example.mybatis.bean.Category">
	select category_name as categoryName from Category where category_id = #{id}
</select>

...
</mapper>

<!-- CategoryMapper.java ->
package com.springriver.example.mybatis.mapper;

import com.springriver.example.mybatis.bean.Category;

public interface CategoryMapper {
	public Category selectCategoryById(int id);
	...
}

3. you can use short bean class name Category instead of full name by importing the package of the bean in the java interface file, how would you do that in xml interface? by adding a type alias in the configuration file.

database-config.xml

<?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>
	<typeAliases>
		<typeAlias type="com.springriver.example.mybatis.bean.Category" alias="Category"/>		
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/springriver/example/mybatis/mapper/CategoryMapper.xml" />
	</mappers>
</configuration>

CategoryMapper.xml

<?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.springriver.example.mybatis.mapper.CategoryMapper">


<select id="selectCategoryById" parameterType="int" resultType="Category">
	select category_name as categoryName from Category where category_id = #{id}
</select>

<insert id="insertCategory" parameterType="Category" useGeneratedKeys="true" keyProperty="categoryId">
	insert into Category(category_name) values (#{categoryName})
</insert>

</mapper>

Download the source code
springriver.mybatis.simple3

Tagged with: , , , , ,
Posted in MyBatis

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>