主配置文件 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<typeAliases>
<!-- 给每一个需要的的实体类手动取别名-->
<!-- <typeAlias type="com.vp.pojo.BookInfo" alias="BB"></typeAlias>-->
<!-- 给该包下面的所有类取别名 默认为类名-->
<package name="com.vp.pojo"></package>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc_driver}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="BookInfoMapper.xml"></mapper>
<mapper resource="BookTypeMapper.xml"></mapper>
</mappers>
</configuration>
范例mapper文件
BookInfoMapper.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">
<!-- 添加namesapce属性 与接口的完整限定名是一致-->
<mapper namespace="com.vp.dao.BookInfoMapper">
<resultMap id="bookinfo" type="BookInfo">
<id property="bookCode" column="book_code"></id>
<result property="book_name" column="book_name"></result>
<result property="book_author" column="book_author"></result>
<result property="publish_press" column="publish_press"></result>
<result property="publish_date" column="publish_date"></result>
<result property="is_borrow" column="is_borrow"></result>
<association property="bookType" javaType="BookType">
<id property="id" column="id"></id>
<result property="type_name" column="type_name"></result>
</association>
</resultMap>
<!--resultType代表列明与属性名字是对应的-->
<!-- <select id="queryAll" resultMap="bookInfo">
/* 列名与属性名不一致的时候第一种 取别名*/
SELECT * from book_info
</select>-->
<select id="queryByBookName" resultMap="bookinfo">SELECT
book_info.book_code,
book_info.book_name,
book_info.book_author,
book_info.publish_press,
book_info.publish_date,
book_info.is_borrow,
book_type.type_name,
book_type.book_type
FROM
book_info
INNER JOIN book_type ON book_info.book_type = book_type.book_type
WHERE book_name=#{book_name}</select>
<insert id="add">
INSERT INTO `book_info` (`book_code`, `book_name`, `book_type`, `book_author`, `publish_press`, `publish_date`, `is_borrow`)
VALUES (#{bookCode}, #{book_name}, #{bookType.id}, #{book_author}, #{publish_press}, #{publish_date}, #{is_borrow})
</insert>
<update id="update">
UPDATE `book_info` SET `book_name`=#{book_name}, `book_author`=#{book_author}, `publish_date`=#{publish_date}
WHERE (`book_code`=#{bookCode})
</update>
<delete id="delete">
DELETE FROM `book_info` WHERE (`book_code`=#{bookCode})
</delete>
<delete id="deleteByCodesAndType">
DELETE FROM `book_info`
<where>
book_code in
<foreach collection="codes" open="(" item="code" close=")" separator=",">
#{code}
</foreach>
and book_type=#{typeId}
</where>
</delete>
<select id="queryByBookNameAndTypeIdAndBorrow" resultMap="bookinfo">
SELECT
book_info.book_code,
book_info.book_name,
book_info.book_author,
book_info.publish_press,
book_info.publish_date,
book_info.is_borrow,
book_type.book_type,
book_type.type_name
FROM
book_info
INNER JOIN book_type ON book_info.book_type = book_type.book_type
<where>
<if test="bookName !=null">
and book_name LIKE CONCAT('%',#{bookName},'%')
</if>
<if test="typeId !=0">
and book_info.book_type=#{typeId}
</if>
<if test="borrow!=0">
and is_borrow=#{borrow}
</if>
</where>
</select>
<update id="updateBookInfo">
UPDATE `book_info`
<set>
<if test="book_name!=null">
`book_name`=#{book_name},
</if>
<if test="book_author!=null">
`book_author`=#{book_author},
</if>
<if test="publish_press!=null">
`publish_press`=#{publish_press},
</if>
<if test="publish_date !=null">
`publish_date`=#{publish_date},
</if>
<if test="is_borrow !=null">
`is_borrow`=#{is_borrow}
</if>
</set>
<where>
<if test="bookCode!=null">
`book_code`=#{bookCode}
</if>
</where>
</update>
</mapper>