万能Map:ocean:

:yellow_heart:(万能方法)工作必备野路子方法,也可以使用到查询,修改,删除等等中:yellow_heart:

假设我们的实体类中,或者数据库的表中,字段或者参数很多,我们应当考虑使用Map插入

示例:UserMapper.java

public interface UserMapper {

    boolean addUser2(Map<String,Object> map);
}

示例:UserMapper.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.singerw.mapper.UserMapper">

    <!--增加用户-->
    <insert id="addUser2" parameterType="map">
        insert into g_users (userid, username, userphone, userpassword, jurisdiction, createtime, logintime, userstatus)
        values (null,#{username},#{userphone},#{userpassword},#{jurisdiction},now(),now(),#{userstatus});
    </insert>

</mapper>

示例:UserMapperTest.java

public class UserMapperTest {

    private SqlSession sqlSession = MyBatisUtils.getSqlSession();
    private UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    @Test
    public void addUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap hashMap = new HashMap<String,Object>();
        hashMap.put("username","singerw");
        hashMap.put("userphone","18888888888");
        hashMap.put("userpassword","123456");
        hashMap.put("jurisdiction",1);
        hashMap.put("userstatus",1);
        mapper.addUser2(hashMap);
    }
}
最后修改:2021 年 08 月 30 日 02 : 00 AM
如果觉得我的文章对你有用,请随意赞赏