在mybatis框架中,存放定制化sql的是xml文件。
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" >
<!-- 命名空间。xxxMapper.xml与xxxMapper.java通过namespace关联起来,xxxMapper.xml中id与xxxMapper.java中的接口方法一一对应 -->
<mapper namespace="com.su.mybatis.mysql.dao.UserMapper" >
<!-- 自定义转换规则 id是当前命名空间中resultMap的唯一标识;type:实体类路径或者别名-->
<resultMap id="BaseResultMap" type="com.su.mybatis.mysql.model.User" >
<!-- id标签:数据的主键,数据库字段与sql中字段对应关系,提高效率,在一对多查询中做结果合并时使用 -->
<id column="id" property="id" jdbcType="INTEGER" />
<!-- result标签:数据库非主键字段与sql中字段对应关系。column为sql查询出来的字段名称(没有使用别名时,即数据库字段),property为实体类中对应字段,-->
<!-- jdbcType为数据库字段对应的jdbc类型-->
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="phone" property="phone" jdbcType="CHAR" />
<result column="money" property="money" jdbcType="INTEGER" />
<!-- association标签:一对一结果合并时使用;collection:一对多结果合并时使用。 -->
</resultMap>
<!-- 使用sql标签,sql中会直接使用下面字段替换 -->
<sql id="Base_Column_List" >
id, name, age, phone, money
</sql>
<!-- select标签表示sql的类型是查询 。id为唯一标识,与对应的namespace中接口一一对应;parameterType为入参类型,当接口类中以@Param传参时,可以不加parameterType配置 -->
<!-- resultMap为返回结果类型,自定义转换规则,也可以用resultType。使用resultType,值为实体类时,实体类中字段需要与数据库表字段相同-->
<!-- (完全相同或者数据库字段由_拼接,实体类中字段为驼峰,增加驼峰转换的配置)或者使用别名将sql中查询的数据库字段与实体类中字段一一对应起来 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<!-- 引用上面sql标签的字段 -->
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<!-- delete标签表示sql的类型是删除。-->
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where id = #{id,jdbcType=INTEGER}
</delete>
<!-- insert标签表示sql的类型是新增。-->
<insert id="insert" parameterType="com.su.mybatis.mysql.model.User" >
insert into user (id, name, age,
phone, money)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{phone,jdbcType=CHAR}, #{money,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.su.mybatis.mysql.model.User" >
insert into user
<!-- trim标签用于sql拼接。trim标签内的sql以prefix的值开始,以suffix的值结束,suffixOverrides的值表示去掉trim标签中sql去掉的后缀 -->
<trim prefix="(" suffix=")" suffixOverrides="," >
<!-- if标签为判断条件语句标识,test的值为判断条件,如果需要判断id不等于某个特定值,外层使用单引号,eg:test='id != "1"' -->
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="age != null" >
age,
</if>
<if test="phone != null" >
phone,
</if>
<if test="money != null" >
money,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=INTEGER},
</if>
<if test="phone != null" >
#{phone,jdbcType=CHAR},
</if>
<if test="money != null" >
#{money,jdbcType=INTEGER},
</if>
</trim>
</insert>
<!-- update标签表示sql的类型是更新。-->
<update id="updateByPrimaryKeySelective" parameterType="com.su.mybatis.mysql.model.User" >
update user
<!-- set动态拼接更新的语句 -->
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
</if>
<if test="phone != null" >
phone = #{phone,jdbcType=CHAR},
</if>
<if test="money != null" >
money = #{money,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.su.mybatis.mysql.model.User" >
update user
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
phone = #{phone,jdbcType=CHAR},
money = #{money,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
驼峰转换配置
<settings>
<!-- 设置自动驼峰转换 -->
<setting value="true" name="mapUnderscoreToCamelCase"/>
</settings>
其他常用标签
where标签:拼接sql后的where条件,如果where标签内没有过滤条件满足条件,则不会拼接;如果存在过滤条件满足条件,则sql会加上where关键字和过滤条件;
foreach标签:常见于循环语句中,eg:in的条件,批量插入数据等。
举个例子
MybatisTest.java
package com.su.mybatis.mysql.controller;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.alibaba.fastjson.JSON;
import com.su.mybatis.mysql.dao.UserMapper;
import com.su.mybatis.mysql.model.User;
public class MybatisTest {
public static void main(String[] args) {
MybatisTest m = new MybatisTest();
m.getUserInfo();
}
public void getUserInfo() {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("mybatisConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = factory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
List<User> res = userMapper.getUserByList(list);
System.out.println("res:" + JSON.toJSONString(res));
}
}
UserMapper.java
package com.su.mybatis.mysql.dao;
import java.util.List;
import com.su.mybatis.mysql.model.User;
public interface UserMapper {
List<User> getUserByList(List<Integer> list);
}
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="com.su.mybatis.mysql.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.su.mybatis.mysql.model.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="phone" property="phone" jdbcType="CHAR" />
<result column="money" property="money" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, name, age, phone, money
</sql>
<select id="getUserByList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
<where> id in
<foreach collection="list" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
</mapper>
输出结果
res:[{"age":27,"id":1,"money":1000,"name":"su","phone":"18912341234"},{"age":18,"id":2,"money":1000,"name":"xie","phone":"18912345678"}]
其他标签这里不做说明。
如果有写的不对的地方,请大家多多批评指正,非常感谢!