dao模块中会有一个Mapping文件需要进行设置,这个是一个映射文件来的,是要配合java文件进行使用,一个执行类的java文件是离不开映射文件的,下面就来示范一个Mapping文件的大概配置:
<?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.itany.zshop.pojo.ProductType">
<sql id="typeColumn">
id,
name,
status
</sql>
<select id="aelectAll" resultType="productType">
select <include refid="typeColum"/>
from t_product_type
</select>
<select id="selectById" parameterType="integer" resultType="productType">
select <include refid="typeColumn"/>
from t_product_type
where id=#{id}
</select>
<insert id="insert">
insert into t_product_type
(name,status)
values
(#{name},#{status})
</insert>
<update id="updateName">
update t_product_type
set name=#{name}
where id=#{id}
</update>
<update id="updateStatus">
update t_product_type
set status=#{status}
where id=#{id}
</update>
<delete id="deleteById" parameterType="integer">
delete from t_product_type
where id=#{id}
</delete>
</mapper>
在http那行以及它前面的那几行,都是mybatis文件中复制过来,与mybatis有关,在下面,<mapper namespace="com.itany.zshop.pojo.ProductType">
这个代表的是路径,这个pojo的ProductType类表示的是get,set方法,是一个实体类,方法等着被调用,而dao文件下的ProductTypeDao这个文件则是一个接口,提供了各种方法比如select id,delete id,insert,update等等方法,但是这些方法还没有具体的方法,它只是一个接口,真正可以实现这些方法的就是pojo下面的ProductType这个类,因此在dao文件下的这个Mapping文件引入了pojo的方法类,为实现ProductTypeDao。
定义一个sql的id
<sql id="typeColumn">
id,
name,
status
</sql>
其中id,name,status这些是当前页面的三个属性。
<select id="aelectAll" resultType="productType">
select <include refid="typeColum"/>
from t_product_type
</select>
这个id名字就是dao里面一个文件的名字(ProductTypeDao文件里面的一个方法的名字),里面就是它的方法select从这个sql里面进行select,typeColum就是这个sql的id,在这里写好后会被直接调用,然而下面的form t_product_type就表示数据库里面的一个数据的名称
下面是一样的道理