Mybatis中,DAO层的代码只需要定义接口,其他的事情由Mybatis框架帮忙搞定。一个例子:
package example.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import example.model.User;
public interface UserMapper {
public User findById(int id);
public User findByName(String name);
public void addUser(User user);
public List<User> findUsers(@Param("start")int start, @Param("size")int size);
public List<User> findUsers1(int limit);
}
定义了几个方法,方法名称和mapper.xml中的sql定义的id相同。例如findById,在mapper.xml中定义了一个:
<select id="findById" parameterType="HashMap" resultMap="usermap">
select
a.id as user_id,
a.name as user_name,
a.sex as user_sex,
a.age as user_age
from T_User a where a.id=#{id}
</select>
需要注意的是,如果方法定义只有一个参数的,在mapper.xml文件中定义sql时可以直接使用参数名作为sql语句中的占位符。如果方法定义有多个参数的,要么使用@Param注解指定mapper.xml中的占位符名称,要么在mapper.xml中使用param1, param2等作为占位符。下面是mapper.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">
<!--这块等于dao接口的实现 namespace必须和接口的类路径一样-->
<mapper namespace="example.dao.UserMapper">
<resultMap type="User" id="usermap">
<id property="id" column="user_id" />
<result column="user_name" property="name" typeHandler="example.dao.typehandler.SimpleTypeHandler"/>
<result property ="sex" column="user_sex" />
<result property="age" column="user_age" />
<!--
<discriminator javaType="byte" column="user_sex">
<case value="1" resultType="example.model.GameUser">
<result property="wins" column="wins" />
</case>
<case value="0" resultType="example.model.SingUser">
<result property="sings" column="sings" />
</case>
</discriminator>
-->
</resultMap>
<resultMap type="User" id="userMapWithAlbum">
<id property="id" column="user_id" />
<result property="name" column="user_name" />
<result property ="sex" column="user_sex" />
<result property="age" column="user_age" />
<collection property="albums" column="user_id" ofType="Album" select="selectAlbumForUser" />
</resultMap>
<select id="findById" parameterType="HashMap" resultMap="usermap">
select
a.id as user_id,
a.name as user_name,
a.sex as user_sex,
a.age as user_age
from T_User a where a.id=#{id}
</select>
<!-- 这里的#{start}和#{size},如果代码中没有用@Param指定,就需要使用#{param1},#{param2} -->
<select id="findUsers" parameterType="HashMap" resultMap="usermap">
select
a.id as user_id,
a.name as user_name,
a.sex as user_sex,
a.age as user_age
from T_User a limit #{start},#{size}
</select>
<select id="findUsers1" parameterType="HashMap" resultMap="usermap">
select
a.id as user_id,
a.name as user_name,
a.sex as user_sex,
a.age as user_age
from T_User a limit #{limit}
</select>
<select id="selectAlbumForUser" resultType="Album">
select * from T_Album where userId=#{user_id}
</select>
<select id="findByName" parameterType="HashMap" resultType="User" resultMap="usermap">
select
a.id as user_id,
a.name as user_name,
a.sex as user_sex,
a.age as user_age
from T_User a where a.name=#{name}
</select>
<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
insert into T_User (name,sex,age) values(#{name},#{sex},#{age})
</insert>
</mapper>