mybatis的mapper.xml标签使用
//namespace命名空间,对应一个mapper类
<mapper namespace="com.blog.mapper.BlogsMapper">
//resultMap 一个基础返回对象,column是数据库的表字段名,property是对应的实体类字段名,jdbcType是字段的数据库类型
//也可以做一个复杂的返回对象,通过包装
<resultMap id="resultBaseMap" type="com.blog.mapper.BlogsMapper">
<id column="blogid" property="blogid" jdbcType="VARCHAR"/>
<result column="userid" property="userid" jdbcType="VARCHAR"/>
<result column="title" property="title" jdbcType="VARCHAR"/>
<result column="blog_content" property="blog_content" jdbcType="VARCHAR"/>
<result column="createtime" property="createtime" jdbcType="DATETIME"/>
<result column="updatetime" property="updatetime" jdbcType="DATETIME"/>
</resultMap>
//复杂返回对象
<resultMap id="resultBaseMap" type="com.blog.pojo.Blogs">
<id column="blogid" property="blogid" jdbcType="VARCHAR"/>
<result column="userid" property="userid" jdbcType="VARCHAR"/>
<result column="title" property="title" jdbcType="VARCHAR"/>
<result column="blog_content" property="blog_content" jdbcType="VARCHAR"/>
<result column="createtime" property="createtime" jdbcType="DATETIME"/>
<result column="updatetime" property="updatetime" jdbcType="DATETIME"/>
//association 插入一个对象
//collection 这个用法跟上面的差不多,只不过这个是list
<association property="commentContent" resultMap="commentContent"/>
</resultMap>
//-----------------------------------------------
<resultMap id="commentContent" type="com.blog.pojo.CommentContent">
<id column="commentid" property="commentid" jdbcType="VARCHAR"/>
<result column="blogid" property="blogid" jdbcType="VARCHAR"/>
<result column="userid" property="userid" jdbcType="VARCHAR"/>
<result column="commentContent" property="commentContent" jdbcType="VARCHAR"/>
<result column="createtime" property="createtime" jdbcType="DATETIME"/>
</resultMap>
<insert id="add" >
insert into blogs(blogid,userid,title,blog_content,createtime,updatetime)
values (#{blogid},#{userid},#{title},#{blog_content},#{createtime},#{updatetime})
</insert>
<select id="findAll" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
select blogid,userid,title,blog_content,createtime,updatetime from blogs
</select>
<select id="findOneById" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
select blogid,userid,title,blog_content,createtime,updatetime from blogs where blogid = #{blogid}
</select>
<update id="blogUpdate" >
update blogs set title=#{title},blog_content=#{blog_content},updatetime=#{updatetime} where blogid=#{blogid}
</update>
<delete id="blogDelete">
delete from blogs where blogid=#{blogid}
</delete>
<insert id="blogCommentAdd" >
insert into blog_users(commentid,userid,blogid,commentContent,createtime) values (#{commentid},#{userid},#{blogid},#{commentContent},#{createtime})
</insert>
<select id="findAllComment" resultType="com.blog.pojo.Comments" parameterType="java.lang.String">
select commentid,userid,blogid,commentContent,createtime from blog_users where blogid = #{blogid}
</select>
<delete id="blogCommentDelete">
delete from blog_users where commentid=#{commentid}
</delete>
<select id="blogSearch" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
select blogid,userid,title,blog_content,createtime from blogs where title like "%"#{search}"%" or blog_content like "%"#{search}"%"
</select>
<select id="blogSearch2" resultType="com.blog.pojo.Blogs" parameterType="java.lang.String">
select blogid,userid,title,blog_content,createtime from blogs where title like #{search} or blog_content like #{search}
</select>
</mapper>
附数据库的表