#{}和$ {}区别
1. #{}是预编译处理,$ {}是字符串替换。
2. mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值; mybatis在处理 $ { } 时,就是把 ${ } 替换成变量的值。
多参数传值
如果是类似string类型,没有getter,setter方法,用@Param("名字")取名,便可以传值过去
paramerterType 和 resultType
parameterType 属性可以是基本类型,引用类型,还可以是实体类类型
基本类型和String我们可以直接写类型名称,也可以使用包名.类名的方式。mybatis已经配置好
而自己的实体类,需要在config.xml文件配置。
<configuration>
<typeAliases>
<!-- 批量定义别名-->
<package name="com.tledu.MybatisTest.entity"/>
</typeAliases>
</configuration>
会将包名下的实体类注册别名,别名为类名且不区分大小写
区分:
<mapper>
<package name> 通过name属性指定mapper接口所在的包名,此时对应的映射文件必须与接口位于同一路径下
</mapper>
resultType
resultType 属性可以指定结果集的类型,它支持基本类型和实体类类型。和 parameterType 一样,如果注册过类型别名的,可以直接使用别名。实体类中的属性名称必须和查询语句中的列名保持一致
resultMap
通过resultMap,我们可以指定查询结果字段和实体属性字段的映射关系。
type=“User”是实体类别名,column是sql语句中的字段名,如果sql起了别名,需要与别名一致,property是User的属性,即为数据库表中的字段名。
动态sql
if判断
<select id="list" parameterType="User" resultMap="userResult">
select * from t_user where 1=1
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="nickname != null and nickname != ''">
and nickname like concat('%',#{nickname},'%')
</if>
</select>
where 标签
<select id="list" parameterType="User" resultMap="userResult">
select * from t_user
<where>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="nickname != null and nickname != ''">
and nickname like concat('%',#{nickname},'%')
</if>
</where>
</select>
作用: 当满足if条件时,去掉第一个and,都不满足时去掉where,搜索全表
set标签
进行更新操作
<update id="updateNickname">
update t_user
<set>
<if test="nickname != null and nickname != ''">
nickname = #{nickname},
</if>
<if test="username != null and username != ''">
username = #{username},
</if>
</set>
where id = #{id}
</update>
作用:当if满足时,去掉逗号,但是都不满足时,sql语句会报错
foreach标签
批量操作
<insert id="batchInsert">
insert into t_user (username, password, nickname) VALUES
<foreach collection="list" index="idx" item="item" separator=",">
(#{item.username},#{item.password},#{item.nickname})
</foreach>
</insert>
其中,collection是集合或数组 item是每一个集合或数组的对象 index是下标 open代表sql以..开始 close代表以..结尾(例如 open="(", close=")", ) separator:分隔符
联查
一对一联查,有三种方法
1.在resultMap用select标签
<resultMap id="address" type="Address">
<id column="id" property="id" />
<association property="user" column="user_id" javaType="User" select="com.tledu.erp.dao.IUser2Dao.selectById"/>
</resultMap>
association标签一般放在resultMap标签内部,然后配置标签的属性,property属性为实体类的属性名,column属性为数据库表的列名,javaType属性为封装的实体类对象名
2.在association中配置映射字段
<resultMap id="address" type="Address" autoMapping="true">
<id column="id" property="id" />
<association property="user" column="user_id" javaType="User" >
<id column="user_id" property="id" />
<result column="school_name" property="schoolName" />
</association>
</resultMap>
<select id="selectOne" resultMap="address">
select * from t_address left join t_user tu on tu.id = t_address.user_id where t_address.id = #{id}
</select>
autoMapping 代表自动映射,当实体类属性名和查询到的数据库的字段名保持一致,自动一一对应
3.嵌套,引用result Map
<resultMap id="addressMap" type="Address" autoMapping="true">
<id column="id" property="id"/>
<association property="user" column="user_id" resultMap="userMap">
</association>
</resultMap>
<resultMap id="userMap" type="User" autoMapping="true">
<id column="user_id" property="id" />
<result column="school_name" property="schoolName"/>
</resultMap>
<select id="selectOne" resultMap="addressMap">
select t_address.id,
addr,
phone,
postcode,
user_id,
username,
password,
nickname,
age,
sex,
school_name
from t_address
left join t_user tu on tu.id = t_address.user_id
where t_address.id = #{id}
</select>