mybatis官网链接:https://mybatis.org/mybatis-3/zh/index.html
1 文件头
<?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">
2 声明mapper
<mapper namespace="com.xxx.xxxMapper"> </mapper>
3 编写resultMap
<resultMap id="BaseResultMap" type="com.xxx.xxxEntiy"> <result column="UPDATE_USER" jdbcType="VARCHAR" property="updateUser" /> <result column="UPDATE_TIME" jdbcType="VARCHAR" property="updateTime" /> <result column="CREATE_USER" jdbcType="VARCHAR" property="createUser" /> <result column="CREATE_TIME" jdbcType="VARCHAR" property="createTime" /> </resultMap> <!-- 左边column为数据库对应字段名称;右边property为实体类对象相应的变量名称-;同文件可编写多个resultMap,使用不同id即可->
4 sql列
<sql id="Base_Column_List"> C.UPDATE_USER,C.UPDATE_TIME,C.CREATE_USER,C.CREATE_TIME </sql> <!-- 填写需要使用的数据库对应字段名即可-->
5 select语句
<select id="query" parameterType="java.lang.String" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from table_name C <where> <if test="xxx!=null and xxx !=''"> and C.XXX like '%${xxx}%' </if> </where> order by C.XXX </select> <!--include句即为调用上面声明的sql列,resultMap调用上面编写好的resultMap-->
6 insert语句
<insert id="insert" parameterType="com.xxx.xxxEntiy"> insert into table_name <trim prefix="(" suffix=")" suffixOverrides=","> <if test="updateUser != null"> UPDATE_USER, </if> <if test="updateTime != null"> UPDATE_TIME, </if> <if test="createUser != null"> CREATE_USER, </if> <if test="createTime != null"> CREATE_TIME, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="updateUser != null"> #{updateUser,jdbcType=VARCHAR}, </if> <if test="updateTime != null"> #{updateTime,jdbcType=VARCHAR}, </if> <if test="createUser != null"> #{createUser,jdbcType=VARCHAR}, </if> <if test="createTime != null"> #{createTime,jdbcType=VARCHAR}, </if> </trim> </insert> <!--parameterType需要写全类名,代表输入参数类型-->
7 update语句
<update id="update" parameterType="com.xxx.xxxEntiy"> update table_name <set> <if test="xxx!= null and xxx!= '' "> XXX= #{xxx,jdbcType=VARCHAR} </if> </set> <where> XXX= #{xxx,jdbcType=VARCHAR} </where> </update>
8 delete语句
<delete id="delete" parameterType="java.lang.String"> delete from table_name where XXX= #{xxx,jdbcType=VARCHAR} </delete>
注:以上语句除1 2 之外,都需要写在2的mapper之中,1为必写且固定项。
9 sql 字段名类型对应(jdbcType)
(oracle)
VARCHAR2(32) ->> jdbcType="VARCHAR" ->> String CHAR(2) ->> jdbcType="VARCHAR" ->> String NUMBER(10,4) ->> jdbcType="DECIMAL" ->> BigDecimal
10 常用全类名
parameterType="java.lang.String" resultType="java.util.HashMap"