1. 程式人生 > 其它 >Map與模糊查詢

Map與模糊查詢

Map與模糊查詢

  • 假設,我們的實體類,或者資料庫中的表,欄位或者引數過多我們應當考慮使用Map!
int addUser2(Map<String, Object> map);
<insert id="addUser2" parameterType="map">
    insert into users (id, name, pwd) VALUES (#{userid}, #{username}, #{password})
</insert>
@Test
public void testAddUser2(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("username", "李逵");
    map.put("userid", 5);
    map.put("password", "1q212");
    mapper.addUser2(map);
    sqlSession.commit();
    sqlSession.close();
}

map傳遞引數,直接在sql中取出key即可【parameterType=“map”】

物件傳遞引數,直接在sql中取物件的屬性即可【parameterType=“Object”】

只有一個基本型別引數的情況下,可以直接在sql中取到

3.8、模糊查詢

3.8.1、java程式碼執行的時候,傳遞萬用字元

<select id="getUserLike" resultType="com.saxon.pojo.User">
    select * from users where name like concat('%',#{value},'%')
</select>