Java Web Learning Notes - mybatis-07-dao development and use (mapper agent development)

Need to write mapper interface (equivalent to dao interface)
mapper.xml mapping file needs to be written
mybatis can automatically generate proxy objects of mapper interface implementation classes

Development specification
1. namespace in mapper.xml is equal to the address of mapper interface
2. The method name in the mapper.java interface is the same as the id name of the statement in mapper.xml
3. The method input parameter type of the mapper.java interface is the same as that specified by the parameterType of the statement in mapper.xml
4. The method return value type of mapper.java interface is consistent with the specified type of resultType of statement in mapper.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">
<!--Use mappper Proxy method, namespace It plays an important and special role-->
<mapper namespace="com.sws.mapper.UserMapper">
    <select id="findUserByID" parameterType="int" resultType="com.sws.entity.User">
          select * from user where id = #{id}
    </select>

    <select id="findUserByName" parameterType="java.lang.String" resultType="com.sws.entity.User">
        --SELECT * from user where username like #{username}
          select * from user where name like '%${username}%'
    </select>

    <insert id="insertUser" parameterType="com.sws.entity.User">

        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            select last_insert_id()
        </selectKey>
        insert into user(id,username,birthday,sex,address) value (#{id},#{username},#{birthday},#{sex},#{address})
    </insert>
    <!--delete user-->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete  from user where id = #{id}
    </delete>
    <!--Update users-->
    <update id="updateUser" parameterType="java.com.sws.entity.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id = #{id}
    </update>
</mapper>
//Configure under SqlMapConfig.xml
<mappers>
    <mapper source="UserMapper.xml" />
</mappers>
//mapper interface writing must meet the development specifications
public interface UserMapper {
    public User findUserByID(int id) throws Exception;

    public void insertUser(User user) throws  Exception;

    public void deleteUser(int id)throws Exception;
}
//Call sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
//Create the UserMapper object, and mybatis automatically generates the mapper proxy object
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//Call userMapper method
User user = userMapper.findUserByID(1);
System.out.println(user);

mapper interface method parameters can only have one. You can use pojo of wrapper type to meet different types of method requirements

Keywords: xml Java Mybatis encoding

Added by Zomie on Fri, 01 May 2020 01:27:37 +0300