不支持Mapper方法重载
-
从源码可以看出mybatis是通过
全限定名.方法名
来作为唯一的命名的,所以同名的方法是会抛异常的public interface UserMessageMapper { public UserMessage findUserMessageById(Long id); public UserMessage findUserMessageById(String mobileNoInternet); } <select id="findUserMessageById" resultMap="userMessageBaseMap"> select <include refid="userMessageColumns" /> from t_user_message t where t.id=#{id}; </select> <select id="findUserMessageById" resultMap="userMessageBaseMap"> select <include refid="userMessageColumns" /> from t_user_message t where t.mobile_no_internet=#{mobileNoInternet}; </select> @Test public void testFindUserMessageIdForMapper(){ String resource = "Configuration.xml"; InputStream is = UserMessageTest.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession session = sessionFactory.openSession(); UserMessageMapper userMessageMapper = session.getMapper(UserMessageMapper.class); UserMessage userMessage = userMessageMapper.findUserMessageById(7777L); System.out.println(userMessage); } @Test public void testFindUserMessageIdForMapper2(){ String resource = "Configuration.xml"; InputStream is = UserMessageTest.class.getClassLoader().getResourceAsStream(resource); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession session = sessionFactory.openSession(); UserMessageMapper userMessageMapper = session.getMapper(UserMessageMapper.class); UserMessage userMessage = userMessageMapper.findUserMessageById("15210321876"); System.out.println(userMessage); }
Mybatis更新值为0问题
-
mybatis
mybatis中,如果更新字段类型为Long或Integer,将更新该字段值为0时,if条件遇0则会判断为false,值会更新不成功; 解决方法: 判断值是否为空写为这种方式:<if test="(testvalue!=null and testvalue!='') or testvalue==0"></if>
常用语句
-
数组参数查询
public List<Role> findRoleByIds(Long[] roleId); <select id="findRoleByIds" resultMap="roleBaseMap"> SELECT tr.* FROM t_role tr WHERE tr.`id` in <foreach item="item" index="index" collection="array" open="("separator="," close=")"> #{item} </foreach> </select>
-
Like查询
使用函数拼接CONCAT(CONCAT('%', #{roleName}),'%') <select id="findRoleListByPage" parameterType="java.util.Map" resultMap="roleBaseMap"> select <include refid="roleColumns"/> from t_role <if test="roleName!=null"> where role_name like CONCAT(CONCAT('%', #{roleName}),'%') </if> order by role_name </select>
-
数组+其他参数混合
-
批量更新
public int updateReadBatch(@Param("userId")Long userId,@Param("msgIds") Long[] msgIds); <update id="updateReadBatch"> <foreach collection="msgIds" item="item" index="index" open="" close="" separator=";"> update t_user_message SET read_status=0 where user_id = #{userId} and id =#{item} </foreach> </update>
-
批量删除
public int deleteBatch(@Param("userId")Long userId,@Param("msgIds") Long[] msgIds); <!--删除消息,设置为无效状态 --> <delete id="deleteBatch"> <foreach collection="msgIds" item="item" index="index" open="" close="" separator=";"> update t_user_message SET status=0 where user_id = #{userId} and id =#{item} </foreach> </delete>
-