mybatis mapper

2017-02-13  本文已影响401人  持续进步者

mybatis

<?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.springmvc.dao.UserMapper">


<resultMap id="UserMap" type="com.springmvc.domain.User">
    <id property="id" column="id"/>
    <result property="username" column="username"></result>
    <result property="birthday" column="birthday"></result>
    <result property="sex" column="sex" ></result>
    <result property="address" column="address"></result>
</resultMap>

<select id="findUserById" parameterType="int" resultType="com.springmvc.domain.User">
    SELECT * FROM USER WHERE id=#{id}
</select>
<insert id="addUser" parameterType="com.springmvc.domain.User">
    INSERT INTO USER (username,birthday,sex,address)VALUES (#{username},#{birthday},#{sex},#{address})
</insert>
<select id="findAll"  resultType="com.springmvc.domain.User" >
    SELECT * FROM USER
</select>

<update id="updateUser" parameterType="com.springmvc.domain.User" >
    UPDATE  USER SET username=#{username} WHERE id=#{id}
</update>

<!--添加用户-->
<insert id="insertUser" parameterType="com.mybaties.domain.User">
    <selectKey keyProperty="id" resultType="int" order="AFTER">
        SELECT LAST_INSERT_ID()
    </selectKey>
    INSERT  INTO USER (username,birthday,sex,address)
    VALUES (#{username},#{birthday},#{sex},#{address})
</insert>

<select id="groupbyUsername" resultType="map">
    SELECT username,COUNT(*) as count FROM USER GROUP BY username ORDER BY count DESC
</select>
</mapper>

mapper

<mapper namespace="com.springmvc.dao.UserMapper">
    <resultMap id="UserMap" type="com.springmvc.domain.User">
        <id property="id" column="id"/>
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex" ></result>
        <result property="address" column="address"></result>
    </resultMap>

    <select id="findUserById" parameterType="int"   resultType="com.springmvc.domain.User">
        SELECT * FROM USER WHERE id=#{id}
    </select>
</mapper>

resultMap bean属性与数据库字段对应关系

<resultMap id="UserMap" type="com.springmvc.domain.User">
    <id property="id" column="id"/>
    <result property="username" column="username"></result>
    <result property="birthday" column="birthday"></result>
    <result property="sex" column="sex" ></result>
    <result property="address" column="address"></result>
</resultMap>

普通sql

select

<select id="findUserById" parameterType="int" resultType="com.springmvc.domain.User">
    SELECT * FROM USER WHERE id=#{id}
</select> 

update

<update id="updateUser" parameterType="com.springmvc.domain.User" >
    UPDATE  USER SET username=#{username} WHERE id=#{id}
</update>

返回插入的id

  <insert id="insertUser" parameterType="com.mybaties.domain.User">
    <selectKey keyProperty="id" resultType="int" order="AFTER">
        SELECT LAST_INSERT_ID()
    </selectKey>
    INSERT  INTO USER (username,birthday,sex,address)
    VALUES (#{username},#{birthday},#{sex},#{address})
</insert>

聚合操作

<select id="groupbyUsername" resultType="map">
    SELECT username,COUNT(*) as count FROM USER GROUP BY username ORDER BY count DESC
</select>

分页

<select id="fenye" parameterType="Map" resultType="com.springmvc.domain.User">
    SELECT * FROM USER limit #{start},#{count}
</select>

@RequestMapping("fenye")
public String fenye(Model model, Integer start) {
    Map<String ,Object> parameter = new HashMap<>();
    parameter.put("start",start*5);
    parameter.put("count",5);
    List<User> datas = userService.fenye(parameter);
    System.out.println(datas);
    model.addAttribute("name", datas);
    return "demo";
}

动态sql操作

动态查询

    <!--动态sql 查询-->
<select id="dynamicSqlwithSelect" parameterType="map" resultType="com.springmvc.domain.User">
    SELECT * FROM USER
    <where>
        <if test="pname!=null">
            AND username LIKE #{pname}
        </if>
        <if test="paddress!=null" >
            AND address=#{paddress}
        </if>

    </where>

</select>

动态更新

<update id="dynamicSqlUpdate" parameterType="com.springmvc.domain.User">
    update USER
      <set>
          <if test="username!=null">
              username=#{username},
          </if>
          <if test="address != null">
              address=#{address},
          </if>
      </set>
     WHERE id=#{id}

</update>

动态删除

<delete id="dynamicSqldelete" >
    delete from user where id in
    <foreach item="ids" collection="list" open="(" close=")" separator=",">
        ${ids}
    </foreach>
</delete>

动态插入

<sql id="key">
    <trim suffixOverrides=",">
        <if test="id!=null">
            id,
        </if>
        <if test="username != null">
            username,
        </if>
        <if test="birthday != null">
            birthday,
        </if>
        <if test="address != null">
            address,
        </if>
        <if test="sex != null">
            sex,
        </if>
    </trim>

</sql>

<sql id="val">
    <trim suffixOverrides=",">
        <if test="id !=null">
            #{id},
        </if>
        <if test="username!=null">
            #{username},
        </if>
        <if test="birthday != null">
            #{birthday},
        </if>
        <if test="address != null">
            #{address},
        </if>
        <if test="sex!=null">
            #{sex},
        </if>
    </trim>
</sql>

<insert id="dynamicInsert" parameterType="com.springmvc.domain.User">
    insert into USER(<include refid="key"/>) VALUES (<include refid="val"/>)
</insert>

联表查询

bean
public class User implements Serializable {

    private Integer id;
    private String username="";
    private String birthday;
    private String sex;
    private String address;

    private List<Address> addressList = new ArrayList<>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List<Address> getAddressList() {
        return addressList;
    }

    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday='" + birthday + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                ", addressList=" + addressList +
                '}';
    }
}

public class Address {
    private int id;
    private String name;
    private User user;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}    

一对一 一个地址对应一个用户

AddressMapper.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="com.springmvc.dao.AddressMapper">

    <resultMap id="address" type="com.springmvc.domain.Address">
        <id property="id" column="id"/>
        <result property="name" column="name"></result>
    </resultMap>

    <resultMap id="address2" type="com.springmvc.domain.Address">
        <id property="id" column="id"/>
        <result property="name" column="name"></result>
        <association property="user"    resultMap="com.springmvc.dao.UserMapper.UserMap"/>
    </resultMap>


    <select id="findById" parameterType="int" resultMap="address2" >
        SELECT a.name,u.username FROM address a INNER JOIN user u ON a.uid=u.id  AND a.id=#{id}
    </select>


</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.springmvc.dao.UserMapper">


    <resultMap id="UserMap" type="com.springmvc.domain.User">
        <id property="id" column="id"/>
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex" ></result>
        <result property="address" column="address"></result>
        <association property="addressList" resultMap="com.springmvc.dao.AddressMapper.address"/>
    </resultMap>

    <select id="findAddress" parameterType="int" resultMap="UserMap">
         SELECT u.username,a.* FROM user u INNER JOIN  address a ON a.uid=u.id AND u.id=#{id}
    </select>

</mapper>
上一篇下一篇

猜你喜欢

热点阅读