Mybatis第三天
Mybatis第三天
1.动态sql语句
-
if
-
where:在sql语句的开头加上where,并去除头部的and或or
-
set:在sql语句前添加set,并把sql语句中最后的”,”去除
-
trim:可以完成set或者是where标记的功能
-
choose+when+otherwise
-
SQL 片段
-
foreach
<select id="" resultType=""> select <include refid="users"/>from users <where> <!-- 防止集合为空 --> <if test="ids.size()!=0"> <!-- collection:指定输入对象中的集合属性 item:每次遍历生成的对象 open:开始遍历时的拼接字符串 close:结束时拼接的字符串 separator:遍历对象之间需要拼接的字符串 select * from user where id in (1,2,3) --> <foreach collection="ids" item="id" open="id in(" close=")" separator=","> #{id} </foreach> </if> </where> </select>
-
模糊查询
2.逆向工程(generator)
1.下载jar包
2.配置逆向工程文件
1)在工程根目录下新建xml文件
2)配置文件
3)编写java启动类,执行逆向工程的配置文件,生成数据库中指定表的bean和dao层
3.分页助手(pageHelper)
1)引入jar包
2)在配置文件conf.xml中添加
3)dao类和相应的映射文件中要有查询方法
dao类
package com.zhiyou100.zfx.dao; import java.util.List; import com.zhiyou100.zfx.bean.Users; public interface UsersMapper { /** * 根据id删 */ int deleteByPrimaryKey(Integer id); /** * 全部添加 */ int insert(Users record); /** * 部分添加 */ int insertSelective(Users record); /** * 通过id查询 */ Users selectByPrimaryKey(Integer id); /** * 按主键选择性修改 */ int updateByPrimaryKeySelective(Users record); /** * 通过主键修改 */ int updateByPrimaryKey(Users record); List<Users> selectAll(); }
mapper
<?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="com.zhiyou100.zfx.dao.UsersMapper"> <!-- resultMap --> <resultMap id="BaseResultMap" type="com.zhiyou100.zfx.bean.Users"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="NAME" jdbcType="VARCHAR" property="NAME" /> <result column="age" jdbcType="INTEGER" property="age" /> </resultMap> <!-- 公共代码块 --> <sql id="Base_Column_List"> id, NAME, age </sql> <!-- 根据id查 --> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from users where id = #{id,jdbcType=INTEGER} </select> <!-- 查询所有 --> <select id="selectAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from users </select> <!-- 根据id删 --> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from users where id = #{id,jdbcType=INTEGER} </delete> <!-- 增加全部 --> <insert id="insert" parameterType="com.zhiyou100.zfx.bean.Users"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> VALUES IDENTITY_VAL_LOCAL() </selectKey> insert into users (NAME, age) values (#{NAME,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) </insert> <!-- 增加部分 --> <insert id="insertSelective" parameterType="com.zhiyou100.zfx.bean.Users"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> VALUES IDENTITY_VAL_LOCAL() </selectKey> insert into users <trim prefix="(" suffix=")" suffixOverrides=","> <if test="NAME != null"> NAME, </if> <if test="age != null"> age, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="NAME != null"> #{NAME,jdbcType=VARCHAR}, </if> <if test="age != null"> #{age,jdbcType=INTEGER}, </if> </trim> </insert> <!-- 修改部分 --> <update id="updateByPrimaryKeySelective" parameterType="com.zhiyou100.zfx.bean.Users"> update users <set> <if test="NAME != null"> NAME = #{NAME,jdbcType=VARCHAR}, </if> <if test="age != null"> age = #{age,jdbcType=INTEGER}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <!-- 通过id修改 --> <update id="updateByPrimaryKey" parameterType="com.zhiyou100.zfx.bean.Users"> update users set NAME = #{NAME,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update> </mapper>


浙公网安备 33010602011771号