目录结构
com.geyao.mybatis.mapper
BlogMapper类
package com.geyao.mybatis.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.geyao.mybatis.pojo.Blog;
public interface BlogMapper {
Blog selectBlog(Integer id);
Blog selectBlog2(Integer id);
List<Blog> selectBlogByTitle(String title);
List<Blog> selectBlogByTitle2(String title);
List<Blog> selectBlogBySort(String column);
List<Blog> selectBlogByPage(int offset,int pagesize);
List<Blog> selectBlogByPage1(@Param(value="offset")int offset,@Param(value="pagesize")int pagesize);
List<Blog> selectBlogByPage2(Map<String, Object>map);
int insertBlog(Blog blog);
int insertBlogMysql(Blog blog);
int updateBlog(Blog blog);
int deleteBlogById(Integer id);
}
BlogMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
-->
<mapper namespace="com.geyao.mybatis.mapper.BlogMapper">
<!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回
User类就是users表所对应的实体类
-->
<!--
根据id查询得到一个user对象
-->
<resultMap type="Blog" id="blogResultMap">
<id column="id" property="id" jdbcType="INTEGER"></id>
<result column="authod_id" property="authodId" jdbcType="INTEGER"/>
</resultMap>
<select id="selectBlog" parameterType="int" resultType="Blog">
select
id,
title,
authod_id as authodId,
state,
featured,
style
from Blog where id=#{id}
</select>
<select id="selectBlog2" parameterType="int" resultMap="blogResultMap">
select *
from Blog where id=#{id}
</select>
<select id="selectBlogByTitle" parameterType="String" resultMap="blogResultMap">
select * from Blog where title like #{title}
</select>
<select id="selectBlogByTitle2" parameterType="String" resultMap="blogResultMap">
select * from Blog where title like '${value}'
</select>
<select id="selectBlogBySort" parameterType="String" resultMap="blogResultMap">
select * from Blog order by ${value}
</select>
<select id="selectBlogByPage" resultMap="blogResultMap">
select * from Blog limit #{0},#{1}
</select>
<select id="selectBlogByPage1" resultMap="blogResultMap">
select * from Blog limit #{offset},#{pagesize}
</select>
<select id="selectBlogByPage2" resultMap="blogResultMap">
select * from Blog limit #{offset},#{pagesize}
</select>
<insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id">
INSERT INTO Blog(
title,
authod_id,
state,
featured,
style
)
VALUES(
#{title},
#{authodId},
#{state},
#{featured},
#{style}
)
</insert>
<insert id="insertBlogOracle" parameterType="Blog" >
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
select seq.nextval as id from dual
</selectKey>
INSERT INTO Blog(
title,
authod_id,
state,
featured,
style
)
VALUES(
#{title},
#{authodId},
#{state},
#{featured},
#{style}
)
</insert>
<insert id="insertBlogMysql" parameterType="Blog" >
<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO Blog(
title,
authod_id,
state,
featured,
style
)
VALUES(
#{title},
#{authodId},
#{state},
#{featured},
#{style}
)
</insert>
<update id="updateBlog" parameterType="Blog" >
UPDATE
Blog
SET
title = #{title},
authod_id = #{authodId},
state = #{state},
featured = #{featured},
style= #{ style}
WHERE id = #{id}
</update>
<delete id="deleteBlogById" parameterType="int">
delete from blog where id =#{id}
</delete>
</mapper>
com.geyao.mybatis.pojo
Blog类
package com.geyao.mybatis.po