mybatis示例如下:
Mapper文件中的UserMapper.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.dao.UserDao" > <resultMap id="BaseResultMap" type="com.pojo.User" > <constructor > <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="user_name" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="age" jdbcType="INTEGER" javaType="java.lang.Integer" /> </constructor> </resultMap> <sql id="Base_Column_List" > id,user_name, password, age </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from user_t where id = #{id,jdbcType=INTEGER} </select> <select id="selectAllUsers" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from user_t </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from user_t where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.pojo.User" > insert into user_t (id, user_name, password, age) values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) </insert> <insert id="insertSelective" parameterType="com.pojo.User" > insert into user_t <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="userName != null" > user_name, </if> <if test="password != null" > password, </if> <if test="age != null" > age, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="userName != null" > #{userName,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> <if test="age != null" > #{age,jdbcType=INTEGER}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.pojo.User" > update user_t <set > <if test="userName != null" > user_name = #{userName,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> <if test="age != null" > age = #{age,jdbcType=INTEGER}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.pojo.User" > update user_t set user_name = #{userName,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update> </mapper>
Dao层的UserDao.java如下示:
public interface UserDao { int deleteByPrimaryKey(Integer id); int insert(User user); int insertSelective(User user); User selectByPrimaryKey(Integer id); List<User> selectAllUsers(); int updateByPrimaryKeySelective(User user); int updateByPrimaryKey(User user); }
1.mapper.xml文件中的增删改查的id,分别对应Dao层中的方法。
namespace用于绑定Dao接口的,即面向接口编程。
当namespace绑定接口后,可以不用写接口实现类,mybatis会通过该绑定自动帮你找到对应要执行的SQL语句.
2.可以利用MyBatis Generator工具包自动生成Mapper文件、Pojo、Controller等
3.resultMap表示将查询结果集中的列分别映射到bean对象的各个属性,一个字段分别对应一个属性。
resultMap中的id表示主键,是唯一的。id和result都是映射单列值到一个属性或字段的简单数据类型。
resultType:对于SQL语句查询出的字段在相应的bean中必须有和它相同的字段对应。
4.parameterType 表示参数的java类型
parameterMap表示通常应用于mapper中有多个参数要传进来时,表示将查询结果集中列值的类型一一映射到java对象属性的类型上,在开发过程中不推荐这种方式。
5.可以通过<sql>添加唯一id设定值,再通过<include>引用已设定的值。如下:
<sql id="Base_Column_List" >
book_id, name, type, author, publishers, borrowDate
</sql>
之后在sql语句中可以引用:
<select id="selectAllBooks" resultMap="BaseResultMap">
select <include refid="Base_Column_List"/> from book_t
</select>
6.<![CDATA[ ]]>是什么意思?
sql是写在xml映射文件中,如果写的sql中有一些特殊的字符的话,
在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用<![CDATA[ ]]>来解决。
如果文本包含了很多的"<"字符 <=和"&"字符——就象程序代码一样,那么最好把他们都放到CDATA部件中。
还有就是
<if test=""> </if> <where> </where>
<choose> </choose> <trim> </trim>
等这些标签都不会被解析,没有必要放在 <![CDATA[ ]]>里面