mybatis配置文件模板及常用标签介绍说明
下文笔者讲述mybatis中配置文件中常见的标签简介说明及mapper配置文件简介说明,如下所示
mybatis配置文件简介
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!--配置和数据库链接相关的信息-->
<!--四大件
驱动
地址
用户名
密码
-->
<!--
default 默认的环境, 在这个配置文件中 可以创建多个环境,如果有多个的时候需要指定默认值,这个值就是下面环境的id值
-->
<environments default="">
<environment id="">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///数据库名?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<!-- 注册映射文件 -->
<mappers>
<!--第一种-->
<mapper resource="com/qianfeng/mybatis/mapper/UserMapper.xml"/>
<!--第二种-->
<!-- 使用下面的注册方式需要满足以下三点要求:
1) 映射文件要与Dao接口在同一个包下
2) 映射文件名要与Dao接口的简单类名相同
3) 映射文件的<mapper/>标签的namespace属性值为Dao接口的全限定性类名
满足以上三个条件,那么这里的class属性值就可以填写Dao接口的全限定性类名
-->
<mapper class="com.qianfeng.mybatis.mapper.OderMapper"/>
<!--第三种-->
<!-- 使用下面的注册方式需要满足以下四点要求:
1) 映射文件要与Dao接口在同一个包下
2) 映射文件名要与Dao接口的简单类名相同
3) 映射文件的<mapper/>标签的namespace属性值为Dao接口的全限定性类名
4) 使用动态Mapper
满足以上四个条件,那么这里的name属性值就可以填写Dao接口所在的包名
-->
<package name="com.qianfeng.mybatis.mapper"/>
<!--第四种-->
<!-- 下面的方式可以使映射文件存放到本地文件系统,但此方式不常用 -->
<mapper url="file:///e:/mapper.xml"/>
</mappers>
</configuration>
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"> <!--namespace 命名空间 对sql的statement进行分类的,我们调用这里面sql 语句的时候会使用 在进行接口式编程时这里是对应接口的全限定名称 如<mapper namespace="com.qianfeng.mybatis.mapper.UserMapper"> --> <mapper namespace=""> </mapper>
select标签
<!-- id 代表当前sql语句的id,我们需要通过这个id来找到这个sql语句
parameterType: 代表的是参数的数据类型, mybatis内部帮我们封装了一些类型 比如 int代表的是 java.lang.Integer
resultType: 返回结果的全限定名称,或者是mybatis内置的类型
public String com.qianfeng.mybatis.pojo.User.getId()//方法的全限定名称
#{} 占位符,相当于我们在使用jdbc的时候的? ,里面写什么呢? 如果是一个参数,并且参数的类型是基本数据类型或者是字符串的时候,可以随便写
但是为了方便了解是做什么或者是为了方便知道参数的含义,建议写对应的数据名字
-->
<!--在进行接口式编程时
id必须和接口中对应方法的名字一样
parameterType 必须和方法的参数一致
resultType必须和方法的返回值一致
-->
<select id="findUserById" parameterType="int" resultType="com.qianfeng.mybatis.pojo.User">
SELECT * FROM user WHERE id=#{id}
</select>
insert标签
<!--
insert是sql里面最特殊的一个语句,只有两种结果,一个是成功,一个是抛异常,不像update
#{username}代表从user对象中获取一个名字叫username的属性(注意不是变量名),属性就是get/set 方法去掉getting/set后首字母小写
当参数是一个对象的时候,这里面要通过占位符的方式进行填写参数,参数的名字就是要作为参数值的属性的 属性名
-->
<insert id="insertUser" parameterType="com.qianfeng.mybatis.pojo.User">
INSERT INTO tb_user (username,password,email,gender,flag,role,code) VALUES (# {username},#{password},#{email},#{gender},#{flag},#{role},#{code})
</insert>
selectkey标签
<!--
selectKey: 获取主键
resultType: 主键值的类型
keyProperty: 代表主键的值放入到参数中的哪个属性上面,比如当前期望将主键的值放到我们的user对象的id属性上面
order: 代表selectKey对应的sql语句的执行顺序,在下面真正的sql语句执行之前还是之后执行,此处我们需要插入数据之后才能知道结果,所以是after
SELECT LAST_INSERT_ID() 获取最后一次插入的主键,这个方法只能获取当前事物中的最后一次插入主键
-->
<insert id="insertUser1" parameterType="com.qianfeng.mybatis.pojo.User">
<selectKey resultType="int" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO user (iusername,password,email,gender,flag,role,code) VALUES (#{username},#{password},#{email},#{gender},#{flag},#{role},#{code})
</insert>
动态sql标签
mybatis的动态sql语句是基于OGNL表达式的,可以方便的在sql语句中实现某些逻辑。
mybatis的动态sql标签分为以下几类
if标签(简单的条件判断)
where标签(主要用来简化sql语句中的wheret条件判断
能够智能处理 and or,不必担心多余导致语法错误)
choose标签(when,otherwize)相当于java中的switch,与jstl中的choose类似
trim标签(对包含的内容加上prefix,或者suffix等,前缀和后缀)
set标签(主要用于更新时)
foreach标签(主要用于mybatis in 语句查询时)
if标签
<select id="findUserLike" resultType="User">
SELECT * FROM user WHERE 1 =1
<if test="name != null">
AND name like #{name}
</if>
</select>
where标签
<!--查询数据的时候,会有多个条件
但是这些条件会出现可能会有,可能没有的情况
select * from xxx where a=xxx and b =xxx and c =xxx
不允许出现where 1=1 因为这会导致数据库索引失效
<where> 会自动去掉第一个符合条件的and
-->
<select id="findUserByMult" resultType="com.qianfeng.mybatis.pojo.User">
SELECT id, username, password, email, gender, flag, role, code from tb_user
<where>
<if test="username!=null and username !=''">
and username=#{username}
</if>
<if test="email!=null and email !=''">
and email=#{email}
</if>
</where>
</select>
choose标签
<!--当when标签条件成立时就执行,然后跳出,当所有的when标签条件都不成立的时候,执行otherwiseb标签中的内容
when和otherwise条件只有一个会输出
-->
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from user where 1 = 1
<choose>
<when test="name != null">
and name = #{name}
</when>
<when test="info != null">
and info = #{info}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
set标签
<!--set标签主要用于更新操作的时候
主要是在包含语句前面输出一个set
如果包含语句是以逗号(,)结束的话,会将其忽略
如果set包含内容为空的话,会报错
使用set可以动态更新修改字段
-->
<update id="updateUser_if_set" parameterType="com.qianfeng.mybatis.pojo.User">
UPDATE user
<set>
<if test="username!= null and username != '' ">
username = #{username},
</if>
<if test="sex!= null and sex!= '' ">
sex = #{sex},
</if>
<if test="birthday != null ">
birthday = #{birthday},
</if>
</set>
WHERE user_id = #{userid};
</update>
trim标签
trim标签是更加灵活用于去除多余关键字的标签,它可以用来实现where和set的效果 主要包括四个属性 prefix:在包含内容之前加上某些前缀 suffix:在包含内容之后加上某些后缀 prefixOverrides:把包含内容首部的某些内容覆盖掉,即忽略 suffixOverrides:把包含内容尾部的某些内容覆盖掉,即忽略
trim代替where
<select id="dynamicTrimTest" parameterType="User" resultType="User">
select * from user
<trim prefix="where" prefixOverrides="and |or">
<if test="name != null">
name = #{name}
</if>
<if test="info != null">
and info = #{info}
</if>
<if test="owner != null">
or owner = #{owner}
</if>
</trim>
</select>
trim代替set
<update id="updateUser_if_trim" parameterType="User">
UPDATE user
<trim prefix="SET" suffixOverrides=",">
<if test="username != null and username != '' ">
username = #{username},
</if>
<if test="sex != null and sex != '' ">
sex = #{sex},
</if>
<if test="birthday != null ">
birthday = #{birthday},
</if>
</trim>
WHERE user_id = #{user_id}
</update>
foreach标签
foreach标签主要用于构建in条件中 主要包括六个属性 index: 指定一个名字,用于迭代过程中,每次迭代到的位置 collection: 代表要遍历的存放参数的集合 item: 每次遍历后的参数存放的位置 open: 遍历之前添加什么内容 close: 遍历完成之后添加什么 separator: 分隔符 ,每次遍历之后添加分隔符,但是最后一次遍历不添加 collection属性的值必须指定,但是在不同情况下,该属性的值是不一样的
主要有以下三种情况
<!--第一种--> <!--单参数,且参数类型是一个list的时候 collection属性值为list--> <select id="dynamicForeachTest" resultType="User"> select * from user where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> <!--第二种--> <!--单参数,且参数类型是一个array的时候 collection属性值为array--> <select id="dynamicForeach2Test" resultType="User"> select * from user where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> <!--第三种--> <!--如果传入的参数是多个的话,我们就需要把它封装成map集合 当然单参数也可以封装成map,实际上如果你在传入参数的时候, 在breast里面也是会把它封装成一个Map的, map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key。 --> <select id="dynamicForeach3Test" resultType="User"> select * from user where title name "%"#{name}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
include标签
<!--
声明一个sql语句,此语句可以被复用 使用方式在使用的地方通过include id= sql id
-->
<sql id="getall">
SELECT id, username, password, email, gender, flag, role, code from user
</sql>
<!--
include
导入一条已经声明好的sql语句
-->
<select id="findUserByMult1" resultType="User">
<include refid="getall"/>
<where>
<if test="username!=null and username !=''">
and username=#{username}
</if>
<if test="email!=null and email !=''">
and email=#{email}
</if>
</where>
</select>
resultmap标签
<!--
resultMap 返回结果的映射,主要用于,返回的列的name与对象本身不一致,或者是多表关联的时候
属性
id 当前resultmap的标记
type 当前resultmap真正的返回结果类型
标签
id sql中返回的列中主键的列名,
column sql语句返回的列名,property代表在最终对象上面的属性名
result 普通属性
-->
<resultMap id="orderby" type="Order">
<id column="id_" property="id" javaType="string"></id>
<result column="money_" property="money"/>
<result column="status_" property="status"/>
</resultMap>
<!--
将返回结果通过resultmap进行解析处理
-->
<select id="findOrderById1" parameterType="string" resultMap="orderby">
select id id_ , money money_ , status status_ from tb_order where id =#{id}
</select>
association标签(一对一关系)
<resultMap id="addressresultmap" type="Address">
<id column="aid" property="id"></id>
<result property="detail" column="detail"></result>
<result property="name" column="name"></result>
<!--对一的标签
property 当前这个一的对象在最终返回结果对象中的属性名
javaType 指的是关联对象的类型,不设置javatype的话不会给我们自动创建对象
-->
<association property="user" javaType="User">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="password" property="password"></result>
</association>
</resultMap>
<select id="findAddressAndUserInfoByAddressId" resultMap="addressresultmap" parameterType="int">
SELECT a.id aid,a.detail , a.name,a.phone,a.level,u.id,u.username,u.password,u.email,u.gender,u.flag,u.role,u.code from tb_address a ,tb_user u where a.uid=u.id and a.id=#{id}
</select>
collection标签(一对多关系)
<!--
一对多的关系
collection 在user对象中 oders集合的属性名, 也就多的一方在少的一方中的属性名
ofType 集合的泛型
-->
<resultMap id="userorderresultmap" type="com.qianfeng.mybatis.pojo.User" extends="basemap">
<!--<id column="uid" property="id"></id>-->
<!--<result column="username" property="username"/>-->
<!--<result column="password" property="password"/>-->
<collection property="orders" ofType="com.qianfeng.mybatis.pojo.Order">
<id column="id" property="id"></id>
<result column="money" property="money"/>
<result column="status" property="status"/>
</collection>
</resultMap>
<select id="findUserAndOrderByUserId" resultMap="userorderresultmap">
select u.id uid , u.username, u.password,o.id,o.money,o.status from tb_user u ,tb_order o where u.id=o.uid and u.id =#{id}
</select>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


