通常查询数据库返回javabean这样的查询结果时,写查询的语句需要先配置好,如下例:
"demo.entity.AppList" id="resultMap">
<result column="appid" property="appid" jdbcType="VARCHAR" />
<result column="schoolid" property="schoolid" jdbcType="VARCHAR" />
<result column="appname" property="appname" jdbcType="VARCHAR" />
<result column="app_departid" property="app_departid" jdbcType="VARCHAR" />
<result column="apptype" property="apptype" jdbcType="VARCHAR" />
标签中type属性是指定要输出的javabean,标签中,column对象数据库中的字段名,property对应javabean中的属性名,jdbcType指定该字段的数据类型。
接下来就是几种查询语句的写法:
1. 分页查询
<select id="selectAll" parameterType="int" resultMap="resultMap">
select>
.查询表t_ampa_appdeploydt 中从num开始的后10条数据,上例中,#{num}为传入的变量,中的 parameterType属性指定该变量的数据类型,resultMap属性就是映射之前配置好的,并以其配置好的格式进行输出。(注:因select *from t_ampa_appdeploydt limit #{num},10中涉及数据变量#{num},最好加上![CDATA[]将该句子包裹,否则可能会造成无法识别为变量)
对应的查询方法为: selectList(“selectAll”,int类型的变量); 其中selectAll对应配置文件中相应查询语句的id。
2.多条件查询
<select id="selectBySclAndTypeAndName" parameterType="java.util.Map" resultMap="resultMap">
<if test="schoolid!='全部'">
if>
<if test="apptype!='全部'">
if>
<if test="appname!=''">
if>
select>
上例中,标签的parameterType属性指定为java.util.Map,那么传入的值就是Map类型,其中可包含多个键值对,引用时只需要写上对应的键名即可。带条件的查询语句写法如上,若schoolid、apptype、appname的值不满足条件,不会被带入条件查询当中,而最后都会把limit #{num},10带入进行分页查询。
对应的查询方法为:selectList(“selectBySclAndTypeAndName”,Map类型的变量);
3.查询单条数据
<select id="selectAllCount" parameterType="int" resultType="java.lang.Integer">
select>
对应的查询方法为:selectOne(“selectAllCount”);
4.插入语句
<insert id="insertStatement" parameterType="demo.entity.User">
insert>
5.批量插入
<insert id="insertMoreStatement" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=",">
foreach>
insert>
6.删除语句
<delete id="deleteStatement" parameterType="demo.entity.User">
delete>
7.批量删除
<delete id="deleteMoreStatement" parameterType="java.util.List">
<foreach collection="list" item="item" open="(" separator="," close=")">
foreach>
delete>
8.更新语句
<update id="updataStatement" parameterType="demo.entity.User">
update>
9.批量更新
<update id="updataMoreStatement" parameterType="java.util.List">
<set>
set>
<foreach collection="list" item="item" open="(" close=")" separator=",">
foreach>
update>