Mybatis_sql映射文件
常用的SQL映射操作。
sql语句传入多个参数
1、在方法中给多个参数
/** * 根据年龄和性别查询信息 */ List<User> selectBySexAge(@Param("sex") int sex,@Param("age") int age);
1.1使用 #{param1} 或 #{arg0} 取第一个参数
<select id="selectBySexAge" resultType="com.lurenjia.pojo.User"> select * from t_user where sex=#{param1} and age=#{param2} </select>
1.2也可使用(@parm)注解值读取
<select id="selectBySexAge" resultType="com.lurenjia.pojo.User"> select * from t_user where sex=#{sex} and age=#{age} </select>
2、在方法中使用一个对象传递多个参数
/** * 根据年龄和性别查询信息 */ List<User> selectBySexAge(User user);
2.1使用#{属性名}获取参数,底层调用get方法。
<select id="selectBySexAge" resultType="com.lurenjia.pojo.User"> select * from t_user where sex=#{sex} and age=#{age} </select>
3、使用Map集合传递参数
使得把数据存入键值对中,使用#{key}即可获取对应值。
resultMap使用
与 resultType 对应。
resultType给 数据库表中字段名与持久层类中属性名一致的 进行自动映射。
resultMap则是自己手动设置属性与字段的映射关系。
1、基本使用
当字段名与属性名不一致时,手动给它们设置映射。
<!-- id:唯一标识,type:返回值类型--> <resultMap id="user_resultMap" type="User"> <!--主键字段的映射--> <id column="uid" property="id"></id> <!--非主键字段的映射--> <result column="uname" property="name"></result> </resultMap> <select id="selectAll" resultMap="user_resultMap"> select * from t_user; </select>
2、实现多表查询
需求:
查询多个表的数据到一个对象中。
例子:
1、数据库:
学生表,其中有一个外键为老师id
老师表,其中包括老师信息。
2、pojo类:
学生类中包含了一个老师对象
3、sql映射:
<!--自定义映射--> <resultMap id="stuMap" type="students"> <!--其它的同名字段可以省略,但是作为参数的列不能省略--> <result property="tid" column="tid"/> <!--关联一个对象,property为对象在students中的名字(属性名),column为使用此列作为查询参数,select表示调用此查询语句--> <association property="teachers" column="tid" select="com.lurenjia.mapper.TeachersMapper.selectById"></association> <!--关联的是一个集合对象,ofType为集合中的具体对象(泛型)--> <collection property="list" ofType="teachers" column="tid" select="com.lurenjia.mapper.TeachersMapper.selectById"></collection> </resultMap> <!--查询学生表的所有信息,返回值为自定义的结果集--> <select id="selectAll" resultMap="stuMap"> select * from students </select>
bind标签
使用bind标签给属性添加前缀后缀,例子:使用like查询时候,给值添加%%前后缀。
<!--根据给定的学生和老师进行查询学生信息总条数--> <select id="selectCountByPageInfo" resultType="java.lang.Long"> select count(*) from students <where> <if test="sname!=null and sname!=''"> <bind name="sname" value="'%'+sname+'%'"></bind> and name like #{sname} </if> <if test="tname!=null and tname!=''"> <bind name="tname" value="'%'+tname+'%'"></bind> and tid in (select id from teachers where name like #{tname}) </if> </where> </select>
sql片段
在sql语句嵌入固定片段。
<!--sql片段--> <sql id="t_user_column"> uid, uname, password, age, sex, birth </sql> <select id="selectAll" resultType="User"> select <include refid="t_user_column"></include> from t_user; </select>
特殊字符
xml中的特殊字符,比如:<
<!--转义字符:< < --> <select id="selectById" resultType="User"> select * from t_user where uid < #{id} </select> <!--CDATA区域:把内容转为纯文本--> <select id="selectById" resultType="User"> select * from t_user where uid <![CDATA[ < ]]> #{id} </select>
查:多条件动态查询
有多个条件可选时,但用户只选择了部分条件,进行动态sql查询。
<!--多条件动态查询--> <select id="selectBySexAge" resultType="com.lurenjia.pojo.User"> select * from t_user <where><!--会自动去掉第一个and,并且单没有添加满足时不会生成where--> <if test="sex!=null"><!--如果满足条件,则生成语句--> and sex=#{sex}</if> <if test="age!=null"> and age=#{age}</if> </where> </select>
查:单条件动态查询
有多个条件可选时,只能选择其中一个,则根据选择的条件进行查询。
<!--单条件动态查询--> <select id="selectBySome" resultType="com.lurenjia.pojo.User"> select * from t_user where <choose><!--相当于switch语句--> <when test="sex!=null">and sex=#{sex}</when><!--相当于case语句--> <when test="age!=null">and age=#{age}</when> <otherwise>1=1</otherwise><!--相当于default语句--> </choose> </select>
增:新增信息后获取到主键值
在新增数据时候,一般不需要传入主键值(自动增长),在新增成功后,可以将主键值赋给数据对象。
//要新增的对象 User user = new User("lrj","136",33,0,"2022-5-13"); //通过mapper接口调用sql语句 userMapper.add(user); //获取到主键uid的值 System.out.println(user.getUid());
<!--新增数据,指定useGeneratedKeys为true、指定主键属性名,即可在添加成功后获取到主键值--> <insert id="add" useGeneratedKeys="true" keyProperty="uid"> insert into t_user (uname, password, age, sex, birth) values (#{uname},#{password},#{age},#{sex},#{birth}) </insert>
改:动态修改数据
传入一个数据对象,若有对应属性,则修改其中的值。
/** * 修改一条信息 * @return */ int updateById(User user);
<!--动态修改数据:有则改--> <update id="updateById"> update t_user <set> <if test="uname!=null and uname!=''"> uname = #{uname}, </if> <if test="password!=null and password!=''"> password = #{password}, </if> <if test="age!=null"> age = #{age}, </if> <if test="sex!=null"> sex = #{sex}, </if> <if test="birth!=null and birth!=''"> birth = #{birth}, </if> </set> where uid = #{uid} </update>
删:删除多条数据
根据选中的id删除对应的数据。
/** * 删除指定id的信息 */ int deleteByids(@Param("ids") int[] ids);
<!--删除指定id的数据--> <delete id="deleteByids"> delete from t_user where uid in <!--参数:要变量的参数,遍历中的对象,分隔符,头部,尾部--> <foreach collection="ids" item="uid" separator="," open="(" close=")"> #{uid} </foreach> </delete>