mybatis小技巧=>将 映射配置文件 与 mapper接口 写在共同的java包下

mybatis本身 是需要 项目打包后 在 target 目录中 

 映射配置文件 与 mapper接口  在共同的包内;

xml配置文件写的位置可以在 resources 目录下 与 Java目录下 层级结构相同 的 resources 目录中;

如果直接把xml 映射配置文件 写在 java 目录中 与mapper接口放在一起;

需要在pom文件做如下配置,意思是把xml文件一起打包进 target中

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

<build>

<resources><!--资源-->
<resource>
<directory>src/main/java</directory>
<includes><!--包含-->
<include>**/*.xml</include>
</includes>
</resource>
<resource><!--不能少了这个配置文件资源-->
<directory>src/main/resources</directory><!--目录-->
</resource>
</resources>

</build>

/**************************************************************************************************************************************/

既然看到这里,顺手把mybatis 的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.XXX.dao.CheckItemMapper">

<!--先写一个查询全部,系统先动起来-->
<select id="findList" resultType="CheckItem">
select * from t_checkitem
<if test="value != null and value.length > 0">
where code = #{value} or name like concat('%',#{value},'%')
</if>
</select>

<!--增-->
<!--addCheckItem-->
<insert id="addCheckItem">
insert into t_checkitem values (null,#{code},#{name},#{sex},#{age},#{price},#{type},#{attention},#{remark})
</insert>

<!--删根据id-->
<delete id="deleteById">
delete from t_checkitem where id=#{id};
</delete>

<!--edit-->
<update id="edit">
update t_checkitem
<set>
<if test="name != null">
name = #{name},
</if>
<if test="sex != null">
sex = #{sex},
</if>
<if test="code != null">
code = #{code},
</if>
<if test="age != null">
age = #{age},
</if>
<if test="price != null">
price = #{price},
</if>
<if test="type != null">
type = #{type},
</if>
<if test="attention != null">
attention = #{attention},
</if>
<if test="remark != null">
remark = #{remark},
</if>
</set>
where id=#{id};
</update>

<!--findById-->
<select id="findById" resultType="CheckItem">
select * from t_checkitem where id=#{id};
</select>

<!--/*查我这个 检查项 有没有在谁的 组 里面*/
int findCountByCheckItemId(Integer id);-->
<select id="findCountByCheckItemId" resultType="long">
select count(checkgroup_id) from t_checkgroup_checkitem where checkitem_id =#{id};
</select>

<!--/*根据checkGroupId查询检查项checkItems们*/
List<CheckItem> findMycheckItems(Integer id);-->
<select id="findMycheckItems" resultType="CheckItem">
select * from t_checkitem where id in
(select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{id});
</select>

</mapper>

/^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/

既然到这里 在加一个小技巧

mybatis是ORM 对象关系模型 但是有时候 直接用map封装查询数据更加灵活,

对于这样的接口 可以用这样的 写法  

接口

List<Map> findListByCategoryName(String categoryName);

mapper映射
<select id="findListByCategoryName" resultType="java.util.HashMap">
select name , image FROM tb_brand where id in
(select brand_id from tb_category_brand where category_id in (
select id from tb_category where name = #{name}
))
</select>
原文地址:https://www.cnblogs.com/cjd01/p/14053656.html