MyBatis 使用简单的 XML或注解用于配置和原始映射
MyBatis的工作原理

映射器

<select>元素用于映射SQL的select语句<insert>元素用于映射插入语句<update>和<delete>元素用于映射更新和删除语句<sql>元素的作用在于可以定义SQL语句的一部分(代码片段)<resultMap>元素表示结果映射集,是MyBatis中最重要也是最强大的元素
配置
<configuration>
    <!-- 告诉 MyBatis到哪里去找映射文件 -->
    <mappers>
        <mapper resource="easymall/mybatis/UserMapper.xml"/>
        <mapper resource="easymall/mybatis/ProductsMapper.xml"/>     
    </mappers>
</configuration>
 
select

<!-- 根据uid查询一个用户信息 -->
<select id="selectUserById" parameterType="Integer" resultType="com.po.MyUser">
    select * from user where uid = #{uid}
</select>
 
insert
<!-- 添加一个用户,成功后将主键值回填给uid(pojo类的属性)-->
<insert id="addUserKey" parameterType="com.pojo.MyUser" keyProperty="uid"  useGeneratedKeys="true">
    insert into user (uname,usex) values(#{uname},#{usex})
</insert>
 

update和delete
<!-- 修改一个用户 -->
<update id="updateUser" parameterType="com.po.MyUser">
    update user set uname = #{uname},usex = #{usex} where uid = #{uid}
</update>
    <!-- 删除一个用户 -->
<delete id="deleteUser" parameterType="Integer"> 
    delete from user where uid = #{uid}
</delete>
 
sql
<sql id="comColumns">uid,uname,usex</sql>
<select id="selectUser" resultType="com.po.MyUser">
    select <include refid="comColumns"/> from user
</select>
 
resultMap
<resultMap>元素的type属性表示需要的POJO,id属性是resultMap的唯一标识。
 子元素<constructor>用于配置构造方法(当POJO未定义无参数的构造方法时使用)。
 子元素<id>用于表示哪个列是主键。
 子元素<result>用于表示POJO和数据表普通列的映射关系。
 子元素<association> 、<collection> 和<discriminator>是用在级联的情况下。
<resultMap type="" id="">
    <constructor><!-- 类在实例化时,用来注入结果到构造方法 -->
	<idArg/><!-- ID参数,结果为ID -->
	<arg/><!-- 注入到构造方法的一个普通结果 -->
    </constructor>
    <id/><!-- 用于表示哪个列是主键 -->
    <result/><!-- 注入到字段或JavaBean属性的普通结果 -->
    <association property=""/><!-- 用于一对一关联 -->
    <collection property=""/><!-- 用于一对多、多对多关联 -->
    <discriminator javaType=""><!-- 使用结果值来决定使用哪个结果映射 -->
        <case value=""/>	<!-- 基于某些值的结果映射 -->
    </discriminator>
</resultMap>
 
例子
ProductsMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="easymall.dao.ProductsDao">
<select id="allcategories" resultType="String">
	select distinct(category) from products
</select>
<select id="prodlist" resultType="easymall.po.Products" parameterType="map">
	select * from products where (price between #{minPrice} and #{maxPrice})
	<if test="name!=null and name!=''">
		and name like concat('%',#{name},'%')
	</if>
	<if test="category!=null and category!=''">
		and category=#{category}
	</if>
</select>
<select id="oneProduct" parameterType="String" resultType="easymall.po.Products">
	select * from products where id=#{pid}
</select>
<select id="proclass" parameterType="String" resultType="easymall.po.Products">
	select * from products where category=#{category}
</select>
</mapper>
 
UserMapper.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="easymall.dao.UserDao">
<!-- 用户登录功能,返回User类对象 -->
<select id="login" parameterType="String" resultType="easymall.po.User">
	select * from user where username=#{username}
</select>
<insert id="regist" parameterType="easymall.po.User" keyProperty="id" useGeneratedKeys="true">
    insert into user(username,password,nickname,email) values(#{username},#{password},#{nickname},#{email})
</insert>
</mapper>
 
动态SQL

<if>元素是最常用的元素。它类似于Java中的 if 语句。
<trim>元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀
<where>元素的作用是会在写入<where>元素的地方输出一个where语句
在动态update语句中,可以使用<set>元素动态更新列
<foreach>元素主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合
<bind>元素解决SQL映射文件实现不同数据库的拼接函数或连接符号不同的问题










