标签:字符串 open ddr 生日 开始 属性 mes 基本 poj
<!-- namespace:命名空间,用于隔离sql,类似java的包 -->
<!-- #{}:点位符,相当于jdbc的? -->
<!-- ${}:字符串拼接指令,如果入参为普通数据类型时,{}里只能写value -->
<!-- 动态代理开发规则:
1. namespace必需是接口的全路径名
2. 接口的方法名必需与映射文件的sql id一致
3. 接口的输入参数必需与映射文件的parameterType类型一致
4. 接口的返回类型必须与映射文件的resultType类型一致
-->
<resultMap type="com.itheima.mybatis.pojo.Order" id="order_map"> <!-- 用<id>映射主键 --> <id property="id" column="id"/> <!-- 普通字段用<result>映射 --> <result property="userId" column="user_id"/> <!-- 单表查询下,字段与属性一致时可以省略 --> <!-- <result property="number" column="number"/> <result property="createtime" column="createtime"/> <result property="note" column="note"/> --> </resultMap> <!-- resultMap的使用 --> <select id="getOrderListResultMap" resultMap="order_map"> SELECT `id`, `user_id`, `number`, `createtime`, `note` FROM `order` </select>
<!-- 定义sql片段 --> <sql id="user_sql"> `id`, `username`, `birthday`, `sex`, `address` </sql> <!-- parameterType:入参的数据类型 resultType:返回结果的数据类型 --> <!-- 别名的使用,不区分大小写 --> <select id="getUserById" parameterType="int" resultType="uSEr"> SELECT <include refid="user_sql"/> FROM `user` WHERE id = #{id} </select>
<!-- resultType:返回为集合时,只需设置为其中一个元素的数据类型就可 --> <select id="getUserByUserName" parameterType="string" resultType="com.itheima.mybatis.pojo.User"> SELECT `id`, `username`, `birthday`, `sex`, `address` FROM `user` <!-- WHERE username LIKE #{name} --> <!-- --> WHERE username LIKE ‘%${value}%‘ </select>
<!-- resultType:返回为集合时,只需设置为其中一个元素的数据类型就可 --> <select id="getUserByWhere" parameterType="user" resultType="com.itheima.mybatis.pojo.User"> SELECT <include refid="user_sql"/> FROM `user` <!-- WHERE 1 = 1 <if test="username != null and username != ‘‘"> and username LIKE ‘%${username}%‘ </if> <if test="sex != null and sex != ‘‘"> and sex = #{sex} </if> --> <!-- <where>会自动添加where关键字,同时处理多余的and --> <where> <if test="username != null and username != ‘‘"> and username LIKE ‘%${username}%‘ </if> <if test="sex != null and sex != ‘‘"> and sex = #{sex} </if> </where> </select>
public class QueryVo { private User user; private List<Integer> ids; //..省略get和set方法 } <select id="getUserByIds" parameterType="queryvo" resultType="user"> SELECT `id`, `username`, `birthday`, `sex`, `address`, `uuid2` FROM `user` <!-- WHERE id IN(1,10,25,30,32) --> <if test="ids != null"> <where> <!-- collection:要遍历的集合,在这里就是pojo的id列表 open:循环开始之前输出的内容 close:循环结束之后输出的内容 item:每次循环的变量 separator:分隔符 --> <foreach collection="ids" open="id IN(" close=")" item="u_id" separator=","> #{u_id} </foreach> </where> </if> </select>
public class Order { private Integer id; private Integer userId; private String number; private Date createtime; private String note; private User user; //..省略get和set } public class OrderUser extends Order { private String username; private String address; //..省略get和set } //方式一查询 <select id="getOrderUser" resultType="orderUser"> SELECT o.`id`, o.`user_id`, o.`number`, o.`createtime`, o.`note`, u.`username`, u.`address` FROM `order` o LEFT JOIN `user` u ON u.`id` = o.`user_id` </select> //方式二查询用association <resultMap type="order" id="order_user_map"> <!-- 用<id>映射主键 --> <id property="id" column="id"/> <!-- 普通字段用<result>映射 --> <result property="userId" column="user_id"/> <!-- 单表查询下,字段与属性一致时可以省略 --> <result property="number" column="number"/> <result property="createtime" column="createtime"/> <result property="note" column="note"/> <!-- 一对一关联查询用association property:关联的属性,在这里就是订单的用户属性 javaType:user属性的数据类型,支持别名 --> <association property="user" javaType="com.itheima.mybatis.pojo.User"> <!-- 用<id>映射主键 --> <id property="id" column="user_id"/> <!-- 普通字段用<result>映射 --> <result property="username" column="username"/> <result property="address" column="address"/> <result property="birthday" column="birthday"/> <result property="sex" column="sex"/> </association> </resultMap> <select id="getOrderUserResultMap" resultMap="order_user_map"> SELECT o.`id`, o.`user_id`, o.`number`, o.`createtime`, o.`note`, u.`username`, u.`address`, u.`birthday`, u.`sex` FROM `order` o LEFT JOIN `user` u ON u.`id` = o.`user_id` </select>
public class User { private Integer id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 private String uuid2; private List<Order> orders; //..省略get和set } //第一种方法 <!-- resultType:返回为集合时,只需设置为其中一个元素的数据类型就可 --> <select id="getUserByQueryVo" parameterType="queryvo" resultType="com.itheima.mybatis.pojo.User"> SELECT `id`, `username`, `birthday`, `sex`, `address` FROM `user` <!-- WHERE username LIKE #{name} --> <!-- --> WHERE username LIKE ‘%${user.username}%‘ </select> //第二种方法是用标签collection <resultMap type="user" id="user_order_map"> <!-- 用<id>映射主键 --> <id property="id" column="id"/> <!-- 普通字段用<result>映射 --> <result property="username" column="username"/> <result property="address" column="address"/> <result property="birthday" column="birthday"/> <result property="sex" column="sex"/> <!-- 一对多关联用<collection> property:属性名称 ofType:属性的数据类型 --> <collection property="orders" ofType="order"> <id property="id" column="o_id"/> <!-- 普通字段用<result>映射 --> <result property="userId" column="id"/> <!-- 单表查询下,字段与属性一致时可以省略 --> <result property="number" column="number"/> <result property="createtime" column="createtime"/> <result property="note" column="note"/> </collection> </resultMap> <select id="getUserOrderMap" resultMap="user_order_map"> SELECT u.`id`, u.`username`, u.`birthday`, u.`sex`, u.`address`, u.`uuid2`, o.`id` o_id, o.`number`, o.`createtime`, o.`note` FROM `user` u LEFT JOIN `order` o ON o.`user_id` = u.`id` </select>
<insert id="insertUser" parameterType="User"> INSERT INTO USER (`username`, `birthday`, `sex`, `address`) VALUES (#{username}, #{birthday}, #{sex}, #{address}); </insert>
<delete id="deleteUser" parameterType="int"> DELETE FROM `user` WHERE `id` = #{id1} </delete>
<update id="updateUser" parameterType="User"> UPDATE USER SET username = #{username} WHERE id = #{id} </update>
标签:字符串 open ddr 生日 开始 属性 mes 基本 poj
原文地址:https://www.cnblogs.com/liushisaonian/p/9557368.html