Mybatis-Dao层开发之Mapper接口总结

Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象。

Mapper接口开发需要遵循以下规范:

1、  Mapper.xml文件中的namespace与mapper接口的类路径相同。

2、  Mapper接口方法名和Mapper.xml中定义的每个statement的id相同

3、  Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同

4、  Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

1、Mapper(.xml)映射文件

定义mapper映射文件UserMapper.xml(内容同Users.xml),需要修改namespace的值为UserMapper接口路径。将UserMapper.xml放在classpath 下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">
 
<!-- 命名空间,对sql进行分类化管理(sql隔离) -->
<mapper namespace="com.sw.mapper.UserMapper">
	<!-- 在映射文件中配置sql语句 -->
	<!-- 通过select执行查询,id用于标识映射文件中的sql(Statement-id)
	将sql语句封装到mappedstatement中
	#{}表示占位符
	parameterType-指定输入参数的类型
	#{id}-id表示输入的参数,参数名称就是id,如如果输入参数是简单类型,#{}中的参数可以任意
	resultType-指定sql输出结果所映射的java对象类型
	-->
	<!-- 通过id查询用户表的记录 -->
	<select id="findUserById" parameterType="int" resultType="com.sw.po.User">
		select *from user where id=#{id}
	</select>
	
	<!-- 根据用户名称模糊查询用户信息 -->
	<!-- resultType-指定单条记录所映射的对象类型 
	${}拼接sql串,接收参数的内容,不加任何修饰,拼接在sql中(存在sql漏洞)
	${}接收输入参数的内容,如果传入的类型是简单类型,${}中只能使用value
	-->
	<select id="findUserByName" parameterType="java.lang.String" resultType="com.sw.po.User">
	    SELECT *FROM USER WHERE username LIKE '%${value}%'
	</select>
	
	<!-- 添加用户 -->
	<!-- 指定输入参数类型是pojo(包括用户信息)
		#{}中指定pojo(User)属性名,接收到pojo的属性值
		Mybatis通过OGNL获取对象的属性值
	 -->
	<insert id="insertUser" parameterType="com.sw.po.User">
	    <!-- 获取刚增加的记录主键 
	    	返回id到poio对象(User)
	    	SELECT LAST_INSERT_ID():得到刚插入金进去记录的主键值,只适用于自增逐主键
	    	keyProperty:将查询到的主键值设置到parameterType指定的对象的id属性
	    	order:指SELECT LAST_INSERT_ID()的执行顺序,相对于insert来说(before/after)
	    	resultType:指定SELECT LAST_INSERT_ID()的结果类型
	    -->
	    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
	        SELECT LAST_INSERT_ID()
	    </selectKey>
	    INSERT INTO USER (username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})
	   
	    <!-- 使用mysql的uuid生成主键返回
	    	执行过程:
	    		首先通过uuid得到主键,然后将主键设置到id属性中
	    		其次在Inster执行的时候从User对象中取出id的属性值
	     -->
	   <!--   <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
	         SELECT UUID()
	     </selectKey>
	   INSERT INTO USER (id,username,birthday,sex,address) VALUES(#{id},#{username},#{birthday},#{sex},#{address})  -->
	</insert>
	
	<!-- 根据id删除用户 -->
	<delete id="deleteUser" parameterType="java.lang.Integer">
	    DELETE FROM USER WHERE id=#{id}
	</delete>
	
	<!-- 根据id更新用户 
		传入用户id以及相关更新信息
		#{id}:从输入的user对象中获取user的属性值
	-->
	<update id="updateUser" parameterType="com.sw.po.User">
	    UPDATE USER SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id}
	</update>
</mapper>

2、Mapper.java-接口文件

Mapper接口定义有如下特点:

1、Mapper接口方法名与Mapper.xml(UserMapper.xml)中定义的statement的id相同

2、Mapper接口方法的输入参数类型和mapper.xml(User.xml)中定义的statement的parameterType的类型相同

3、 Mapper接口方法的输出参数类型和mapper.xml中定义的statement的resultType的类型相同

代码如下;

package com.sw.mapper;
 
import java.util.List;
 
import com.sw.po.User;
 
/*
 *@Author swxctx
 *@time 2018年7月26日
 *@Explain:使用mapper接口(用户管理),相当于dao接口
 */
public interface UserMapper {
	//根据id查询用户的信息
	public  User findUserById(int id)throws Exception;
	//根据用户名查看用户列表
	public List<User> findUserByName(String username)throws Exception;
	//添加用户
	public void insertUser(User user)throws Exception;
	//删除用户
	public void deleteUser(int id)throws Exception;
}

注:

selectOne和selectList

动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。

namespace

mybatis官方推荐使用mapper代理方法开发mapper接口,程序员不用编写mapper接口实现类,使用mapper代理方法时,输入参数可以使用pojo包装对象或map对象,保证dao的通用性。

猜你喜欢

转载自blog.csdn.net/qq_39331713/article/details/81226118