MySQL和mybatis查询相关
Posted 何其有静
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL和mybatis查询相关相关的知识,希望对你有一定的参考价值。
0.mybatis的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="me.gacl.dao.UserMapper" >
</mapper>
1.mybatis 中 foreach collection的用法
<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
2.mybatis批量插入更新
方法一:
<update id="batchUpdate" parameterType="java.util.List"> <foreach separator=";" index="index" item="item" collection="list" close="" open=""> update sys_group set level = #{item.level,jdbcType=INTEGER} where group_id = #{item.groupId,jdbcType=INTEGER} </foreach> </update>
方法二:
<update id="batchUpdate1" parameterType="java.util.List"> update sys_group set level = null where level in <foreach separator="," index="index" item="item" collection="list" close=")" open="("> #{item} </foreach> </update>
3.mybatis映射
<resultMap id="BaseResultMap" type="me.gacl.domain.User" >
<id column="user_id" property="userId" jdbcType="CHAR" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="user_birthday" property="userBirthday" jdbcType="DATE" />
<result column="user_salary" property="userSalary" jdbcType="DOUBLE" />
</resultMap>
<sql id="Base_Column_List" >
user_id, user_name, user_birthday, user_salary
</sql>
4.mybatis查询select
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" > select <include refid="Base_Column_List" /> from t_user where user_id = #{userId,jdbcType=CHAR} </select>
5.mybatis删除delete
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" > delete from t_user where user_id = #{userId,jdbcType=CHAR} </delete>
6.mybatis插入insert
一:
<insert id="insert" parameterType="me.gacl.domain.User" > insert into t_user (user_id, user_name, user_birthday, user_salary) values (#{userId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userBirthday,jdbcType=DATE}, #{userSalary,jdbcType=DOUBLE}) </insert>
二:
<insert id="insertSelective" parameterType="me.gacl.domain.User" > insert into t_user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="userId != null" > user_id, </if> <if test="userName != null" > user_name, </if> <if test="userBirthday != null" > user_birthday, </if> <if test="userSalary != null" > user_salary, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="userId != null" > #{userId,jdbcType=CHAR}, </if> <if test="userName != null" > #{userName,jdbcType=VARCHAR}, </if> <if test="userBirthday != null" > #{userBirthday,jdbcType=DATE}, </if> <if test="userSalary != null" > #{userSalary,jdbcType=DOUBLE}, </if> </trim> </insert>
7.mybatis更新update
一:
<update id="updateByPrimaryKey" parameterType="me.gacl.domain.User" > update t_user set user_name = #{userName,jdbcType=VARCHAR}, user_birthday = #{userBirthday,jdbcType=DATE}, user_salary = #{userSalary,jdbcType=DOUBLE} where user_id = #{userId,jdbcType=CHAR} </update>
二:
<update id="updateByPrimaryKeySelective" parameterType="me.gacl.domain.User" > update t_user <set > <if test="userName != null" > user_name = #{userName,jdbcType=VARCHAR}, </if> <if test="userBirthday != null" > user_birthday = #{userBirthday,jdbcType=DATE}, </if> <if test="userSalary != null" > user_salary = #{userSalary,jdbcType=DOUBLE}, </if> </set> where user_id = #{userId,jdbcType=CHAR} </update>
以上是关于MySQL和mybatis查询相关的主要内容,如果未能解决你的问题,请参考以下文章
SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper