mybatis新增之后返回保存的id值
<!-- 保存用户 -->
<insert id="saveUser" parameterType="com.zion.domain.User">
<!-- 配置插入操作后,获取数据的id -->
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
select last_insert_id()
</selectKey>
insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})
</insert>
实体类属性名与数据库列名不对应的解决方法
- 直接取别名(效率高,但一用到就要写一遍)
- resultMap
左边的property与实体类属性名严格对应
右边的column与数据库列名严格对应
<!-- 配置查询结构的列名和实体类的属性名的对应关系 -->
<resultMap id="userMap" type="com.zion.domain.User">
<!-- 主键字段的对应 -->
<id property="userId" column="id" />
<result property="userName" column="username" />
<result property="userAddress" column="address" />
<result property="userSex" column="sex" />
<result property="userBirthday" column="birthday" />
</resultMap>
<!-- 查询所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>
properties标签
<!-- 配置properties
可以在标签内部配置连接数据库的信息。也可以通过属性引用外部配置文件信息
resource属性:
用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下
url属性:
统一资源定位符。 可以唯一表示一个资源的位置。
是要求按照URL的写法来写地址
它的写法:
http://localhost://muybatisserver/demoServlet
URI:
统一资源标识符。它是在应用汇总可以唯一定位一个资源的
-->
<properties resource="jdbcConfig.properties">
<!--<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///eesy_mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>-->
</properties>
<!-- 配置环境 -->
<environments default="mysql">
<environment id="mysql">
<!-- 配置事务 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password"/>
</dataSource>
</environment>
</environments>
typeAliases标签
<!-- 使用typeAliases配置别名,它只能配置domain中类的别名 -->
<typeAliases>
<!-- typeAlias用于配置别名,type属性执行的是实体类全限定类名。alias属性指定别名,当指定了别名就不在区分带小学 -->
<!--<typeAlias type="com.zion.domain.User" alias="user"/>-->
<!-- 用于指定要配置的包,当指定之后,该包下的实体类都会注册别名,并且类名就是别名,不在区分大小写 -->
<package name="com.zion.domain"/>
</typeAliases>
mapper标签
<!-- 配置映射文件的位置 -->
<mappers>
<!--<mapper resource="com/zion/dao/IUserDao.xml" />-->
<!-- package标签用于指定dao接口所在的包,当指定了之后就不需要在写mapper已经class了 -->
<package name="com.zion.dao"/>
</mappers>
动态SQL
- if
ps:test里的userName和#{userName} 必须和实体类属性名对应
username 与数据库列名对应
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
-
where
ps:避免 where 1 = 1 的情况
<where>
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
</where>
- foreach
ps:查询指定id、删除多条
<where>
<if test="ids != null and ids.size() > 0">
<foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
- set
<update id="updateUser" parameterType="user">
UPDATE tb_user
<trim prefix="set" suffixOverrides=",">
<if test="userName!=null">user_name = #{userName},</if>
<if test="password!=null">password = #{password},</if>
<if test="name!=null">name = #{name},</if>
<if test="age!=null">age = #{age},</if>
<if test="sex!=null">sex = #{sex},</if>
<if test="birthday!=null">birthday = #{birthday},</if>
updated = now(),
</trim>
WHERE
(id = #{id});
</update>
- choose when otherwise
场景:查询男性用户,如果输入了姓名则按照姓名模糊查找,否则如果输入了年龄则按照年龄查找,否则查找姓名为“zhangsan”的用户
<select id="queryUserListByNameOrAge" resultType="User">
select * from tb_user WHERE sex=1
<!--
1.一旦有条件成立的when,后续的when则不会执行
2.当所有的when都不执行时,才会执行otherwise
-->
<choose>
<when test="name!=null and name.trim()!=''">
and name like '%${name}%'
</when>
<when test="age!=null">
and age = #{age}
</when>
<otherwise>
and name='zhangsan'
</otherwise>
</choose>
</select>
- 语句提取
<sql id="defaultUser">
select * from user
</sql>
<!-- 查询所有操作 -->
<select id="findAll" resultMap="userMap">
<include refid="defaultUser"></include>
</select>
If:testognl表达式或者简单java代码
Choose when otherwise—>相当于if else if else
When test参考if
Where set 都有一定的纠错功能
Trim:prefix suffix prefixOverrides suffixOverrides
Foreach:collection item saparator open close