Mybatis初體驗(二)

馴鹿的熊貓人發表於2020-11-21

 

一、代理開發規則

① mapper.xml檔案中namespace與mapper介面的全限定名相同。

② mapper介面方法名和mapper.xml中定義的每個statement的id相同。

③ mapper介面方法的傳入引數型別和mapper.xml中定義的每個sql的parameterType的型別相同,並且返回引數型別的resultType的型別相同。

如圖:

 二、Mybatis對映檔案配置

  • select:查詢 
    <select id="findAll" resultType="com.itcast.domain.user">  select * from user  </select>

     

  • insert:插入 
     <insert id="save" parameterType="com.itcast.domain.user" >
            insert  into user values (#{Id},#{userName},#{passWord})
        </insert>

     

  • update:修改 
     <update id="edit" parameterType="com.itcast.domain.user">
            update user set username=#{userName},password=#{passWord} where id=#{Id}
        </update>

     

  • delete:刪除
     <delete id="remove" parameterType="int">
            delete  from user where id=#{id}
        </delete>

     

  • where:where條件
    <mapper namespace="com.itcast.dao.UserMapper">
      <sql id="query">
                select * from userInfo
        </sql>
    <select id="queryByWhere" resultType="com.itcast.domain.user" parameterType="com.itcast.domain.user">
            <include refid="query"></include>
            <where>
                <if test="id!=0">
                    and id=#{id}
                </if>
                <if test="userName!=null">
                    and username=#{userName}
                </if>
                <if test="passWord!=null">
                    and password=#{passWord}
                </if>
            </where>
        </select>
    </mapper>

     

  • if:判斷,同上
  • foreach:迴圈 
    <?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.itcast.dao.UserMapper">
    
        <sql id="query">
                select * from user
        </sql>
      <select id="queryByIds" parameterType="list" resultType="com.itcast.domain.user">
            <include refid="query"></include>
            <where>
                <foreach collection="list" open=" id in(" close=")" separator="," item="id">
                    #{id}
                </foreach>
            </where>
        </select>
    
    </mapper>

     

  • sql:sql片段抽取 通常與 include 標籤一起進行使用

 

三、自定義型別轉換處理器(typeHandlers)

  1. 定義型別轉換處理器類繼承 org.apache.ibatis.type.BaseTypeHandler 下的 BaseTypeHandler<T>類
  2. 實現BaseTypeHandler<T>類中的四個抽象方法,其中 setNonNullParameter 為java程式設定資料到資料庫的回撥方法,getNullableResult為查詢資料庫的字串型別轉換成java的Type型別的方法。
  3. Mybatis核心配置檔案中註冊
public class DateTypeHandler extends BaseTypeHandler<Date> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        long time = date.getTime();
        preparedStatement.setLong(i, time);
    }

    @Override
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        long aLong = resultSet.getLong(s);
        Date date = new Date(aLong);
        return date;
    }

    @Override
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        long aLong = resultSet.getLong(i);
        Date date = new Date(aLong);
        return date;
    }

    @Override
    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long aLong = callableStatement.getLong(i);
        Date date = new Date(aLong);
        return date;
    }
}
 <typeHandlers>
        <typeHandler handler="com.itcast.handler.DateTypeHandler"></typeHandler>
    </typeHandlers>

四、分頁外掛pageHelper

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>3.2</version>
        </dependency>

sqlMapConfig.xml中註冊:

<plugins>
        <!--   配置分頁助手    4.0.0版本後不實現PageHelper,自動匹配資料庫 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--     指定方言       -->
<!--            <property name="dialect" value="mysql"/>-->
        </plugin>
    </plugins>

五、sqlMapConfig.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="jdbc.properties"></properties>

    <typeAliases>
        <typeAlias type="com.itcast.domain.UserInfo" alias="user"></typeAlias>
    </typeAliases>

    <typeHandlers>
        <typeHandler handler="com.itcast.handler.DateTypeHandler"></typeHandler>
    </typeHandlers>

    <plugins>
        <!--   配置分頁助手    4.0.0版本後不實現PageHelper,自動匹配資料庫 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--     指定方言       -->
<!--            <property name="dialect" value="mysql"/>-->
        </plugin>
    </plugins>

    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <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="com/itcast/mapper/UserInfoMapper.xml"></mapper>
    </mappers>
</configuration>