mybatis動態sql與分頁

西米露U發表於2020-10-28

動態sql

在上篇部落格的基礎上新增mybatis動態sql,在BookMapper類中新增方法

根據id查詢

在這裡插入圖片描述
滑鼠選中方法名按Alt+Enter鍵進行自動生成實現
在這裡插入圖片描述
在這裡插入圖片描述

<select id="selectBookIn" resultType="com.solar.model.Book" parameterType="java.util.List">
    select * from t_mvc_book where bid in
    <foreach collection="bookIds" item="bid" open="(" close=")" separator=",">
       #{bid}
    </foreach>
  </select>

在這裡插入圖片描述
去service實現
在這裡插入圖片描述
BookServiceImpl中通過bookMapper調取
(新增實現方法)
在這裡插入圖片描述
在這裡插入圖片描述
測試
在這裡插入圖片描述

模糊查詢

三種方式

#{...}
${...}
Concat
注意:#{...}自帶引號,${...}有sql注入的風險

在這裡插入圖片描述
xml檔案
在這裡插入圖片描述
再去BookService中新增
在這裡插入圖片描述

並且呼叫bookMapper實現
在這裡插入圖片描述
測試
首先需要用到一個工具類
StringUtil
這裡所使用的到的工具類StringUtils為了模糊查詢拼接%%
第三種模糊查詢方式不需要通過這個拼接 Concat
在這裡插入圖片描述

package com.solar.util;

/**
 * @author solarWang
 * @site
 * @company
 * @create 2020-10-26 19:41
 */
public class StringUtils {
    public static String toLinkStr(String str){
        return "%"+str+"%";
    }
}

第一種方法
在這裡插入圖片描述
第二種方法
在這裡插入圖片描述

第三種方法
在這裡插入圖片描述
查詢返回結果集的處理
resultMap:適合使用返回值是自定義實體類的情況
resultType:適合使用返回值的資料型別是非自定義的,即jdk的提供的型別

使用resultMap返回自定義型別集合
使用resultType返回List
使用resultType返回單個物件
使用resultType返回List,適用於多表查詢返回結果集
使用resultType返回Map<String,Object>,適用於多表查詢返回單個結果集

書寫一個BookVo實體類
mybatis hibernate都是orm框架,表所存在的列段在實體類model都有對映
實際開發中,會因為某一些需求改變model 破壞model的封裝性
此時為了保證Model的封裝性,就可以使用vo類來完成指定的需求
package com.solar.model.vo;

import com.solar.model.Book;

import java.util.List;

/**
 * @author solarWang
 * @site
 * @company
 * @create 2020-10-28 11:26
 */
public class BookVo extends Book {
        private Integer min;
        private Integer max;
        private List<Integer> bookIds;

        public Integer getMin() {
            return min;
        }

        public void setMin(Integer min) {
            this.min = min;
        }

        public Integer getMax() {
            return max;
        }

        public void setMax(Integer max) {
            this.max = max;
        }

        public List<Integer> getBookIds() {
            return bookIds;
        }

        public void setBookIds(List<Integer> bookIds) {
            this.bookIds = bookIds;
        }
}

在這裡插入圖片描述
在Mapper中新增方法
在這裡插入圖片描述

配置xml檔案
在這裡插入圖片描述

service檔案
在這裡插入圖片描述
實現
在這裡插入圖片描述
第一種
在這裡插入圖片描述
第二種
在這裡插入圖片描述
第三種
在這裡插入圖片描述
第四種
在這裡插入圖片描述
第五種
在這裡插入圖片描述

@Test
    public void list(){
        /*List<Book> books=this.bookService.list1();*/
        /*List<Book> books=this.bookService.list2();*/
        /*List list=new ArrayList();
        list.add(11);
        list.add(12);
        list.add(13);*/
        /*
        BookVo bookVo=new BookVo();
        bookVo.setBookIds(list);
        List<Book> books=this.bookService.list3(bookVo);
        for (Book book: books
             ) {
            System.out.println(book);
        }*/

        Map map=new HashMap();
        /*map.put("bookIds",list);*/
        map.put("bid",11);
        /*List<Map> books=this.bookService.list4(map);*/
        Map book= this.bookService.list5(map);
        /*for (Map book:books
             ) {
            System.out.println(book);
        }*/
        System.out.println(book);

    }

分頁查詢

Mybatis的分頁功能很弱,它是基於記憶體的分頁(查出所有記錄再按偏移量offset和邊界limit取結果),在大資料量的情況下這樣的分頁基本上是沒有用的

匯入pom依賴

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>

在這裡插入圖片描述

Mybatis.cfg.xml配置攔截器

<plugins>
    <!-- 配置分頁外掛PageHelper, 4.0.0以後的版本支援自動識別使用的資料庫 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    </plugin>
</plugins>


在這裡插入圖片描述

分頁

Mapper
在這裡插入圖片描述
xml配置檔案

<!--分頁-->
  <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
    select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
  </select>

在這裡插入圖片描述
service

List<Map> listPager(Map map, PageBean pageBean);

在這裡插入圖片描述
實現

 @Override
    
    public List<Map> listPager(Map map, PageBean pageBean) {
        if(pageBean != null && pageBean.isPagination()){
            PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        }
        List<Map> list = bookMapper.listPager(map);
        if(pageBean != null && pageBean.isPagination()){
            PageInfo pageInfo = new PageInfo(list);
            System.out.println("頁碼:"+pageInfo.getPageNum());
            System.out.println("頁大小:"+pageInfo.getPageSize());
            System.out.println("總記錄:"+pageInfo.getTotal());
            pageBean.setTotal(pageInfo.getTotal()+"");
        }
        return list;
    }

在這裡插入圖片描述
測試

 @Test
    public void listPager() {
        Map map=new HashMap();
        map.put("bname","%聖墟%");
        PageBean pageBean=new PageBean();
        //不分頁
        //pageBean.setPagination(false);
        //第二頁
        pageBean.setPage(2);
        List<Map> maps = this.bookService.listPager(map, pageBean);
        for (Map map1 : maps) {
            System.out.println(map1);
        }
    }

在這裡插入圖片描述

結果
在這裡插入圖片描述

特殊字元處理

>(&gt;)   
<(&lt;)  
&(&amp;) 
空格(&nbsp;)
<![CDATA[ <= ]]> 

mapper
在這裡插入圖片描述
xml配置

 <!--特殊字元處理-->
  <select id="list6" resultType="com.solar.model.Book" parameterType="com.solar.model.vo.BookVo">
    select * from t_mvc_book where <![CDATA[ price > #{min} and price < #{max}]]>
  </select>

在這裡插入圖片描述
service
在這裡插入圖片描述
實現
在這裡插入圖片描述
測試

在這裡插入圖片描述

程式碼塊

測試

package com.solar.service;

import com.solar.mapper.BookMapper;
import com.solar.model.Book;
import com.solar.model.vo.BookVo;
import com.solar.util.PageBean;
import com.solar.util.SessionUtil;
import com.solar.util.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author solarWang
 * @site
 * @company
 * @create 2020-10-23 8:52
 */
public class BookServiceTest {

    private BookService bookService;
    private SqlSession sqlSession;



    @Before
    public void setUp() throws Exception {
        BookServiceImpl bookService=new BookServiceImpl();
        sqlSession = SessionUtil.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        bookService.setBookMapper(mapper);
        this.bookService=bookService;
        //bookServiceImpl.setBookMapper(sqlSession.getMapper(BookMapper.class));
        //this.bookService=bookServiceImpl;
    }



    @Test
    public void deleteByPrimaryKey() {
        bookService.deleteByPrimaryKey(1);
    }

    @Test
    public void insert() {
        Book book=new Book();
        book.setBid(1);
        book.setBname("mybatis輸入");
        book.setPrice(100f);
        int insert=bookService.insert(book);
        System.out.println(insert);
    }

    @Test
    public void insertSelective() {
    }

    @Test
    public void selectByPrimaryKey() {
        Book book=this.bookService.selectByPrimaryKey(11);
        System.out.println(book.toString());
    }

    @Test
    public void updateByPrimaryKeySelective() {
        Book book=bookService.selectByPrimaryKey(0);
        book.setBname("asad");
        book.setPrice(99f);
        bookService.updateByPrimaryKeySelective(book);
    }

    @Test
    public void selectBookIn(){
        List list=new ArrayList();
        list.add(11);
        list.add(12);
        list.add(13);
        List<Book> books=this.bookService.selectBookIn(list);
        for (Book book:books
             ) {
            System.out.println(book);

        }
    }

    @Test
    public void selectBookIn1(){
        String bname="飛";
        List<Book> books=this.bookService.selectBookIn1(StringUtils.toLinkStr(bname));
        for (Book book:books ) {
            System.out.println(book);
        }
    }

    @Test
    public void selectBookIn2(){
        String bname="鬥";
        List<Book> books=this.bookService.selectBookIn2(StringUtils.toLinkStr(bname));
        for (Book book:books ) {
            System.out.println(book);
        }
    }

    @Test
    public void selectBookIn3(){
        String bname="鬥";
        List<Book> books=this.bookService.selectBookIn3(bname);
        for (Book book:books ) {
            System.out.println(book);
        }
    }

    @Test
    public void list(){
        /*List<Book> books=this.bookService.list1();*/
        /*List<Book> books=this.bookService.list2();*/
        /*List list=new ArrayList();
        list.add(11);
        list.add(12);
        list.add(13);*/
        /*
        BookVo bookVo=new BookVo();
        bookVo.setBookIds(list);
        List<Book> books=this.bookService.list3(bookVo);
        for (Book book: books
             ) {
            System.out.println(book);
        }*/

        Map map=new HashMap();
        /*map.put("bookIds",list);*/
        map.put("bid",11);
        /*List<Map> books=this.bookService.list4(map);*/
        Map book= this.bookService.list5(map);
        /*for (Map book:books
             ) {
            System.out.println(book);
        }*/
        System.out.println(book);

    }

    @Test
    public void listPager() {
        Map map=new HashMap();
        map.put("bname","%聖墟%");
        PageBean pageBean=new PageBean();
        //不分頁
        //pageBean.setPagination(false);
        //第二頁
        pageBean.setPage(2);
        List<Map> maps = this.bookService.listPager(map, pageBean);
        for (Map map1 : maps) {
            System.out.println(map1);
        }
    }

    @Test
    public void list6(){
        BookVo bookVo=new BookVo();
        bookVo.setMin(12);
        bookVo.setMax(15);
        List<Book> list=this.bookService.list6(bookVo);
        for (Book book:list
             ) {
            System.out.println(book);
        }
    }


    @Test
    public void updateByPrimaryKey() {
    }

    @After
    public void tearDown() throws Exception {
        sqlSession.commit();
        sqlSession.close();
    }
}

PageBean

package com.solar.util;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;

public class PageBean implements Serializable {

    private static final long serialVersionUID = 2422581023658455731L;

    //頁碼
    private int page=1;
    //每頁顯示記錄數
    private int rows=10;
    //總記錄數
    private int total=0;
    //是否分頁
    private boolean isPagination=true;
    //上一次的請求路徑
    private String url;
    //獲取所有的請求引數
    private Map<String,String[]> map;

    public PageBean() {
        super();
    }

    //設定請求引數
    public void setRequest(HttpServletRequest req) {
        String page=req.getParameter("page");
        String rows=req.getParameter("rows");
        String pagination=req.getParameter("pagination");
        this.setPage(page);
        this.setRows(rows);
        this.setPagination(pagination);
        this.url=req.getContextPath()+req.getServletPath();
        this.map=req.getParameterMap();
    }
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, String[]> getMap() {
        return map;
    }

    public void setMap(Map<String, String[]> map) {
        this.map = map;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void setPage(String page) {
        if(null!=page&&!"".equals(page.trim()))
            this.page = Integer.parseInt(page);
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void setRows(String rows) {
        if(null!=rows&&!"".equals(rows.trim()))
            this.rows = Integer.parseInt(rows);
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public void setTotal(String total) {
        this.total = Integer.parseInt(total);
    }

    public boolean isPagination() {
        return isPagination;
    }

    public void setPagination(boolean isPagination) {
        this.isPagination = isPagination;
    }

    public void setPagination(String isPagination) {
        if(null!=isPagination&&!"".equals(isPagination.trim()))
            this.isPagination = Boolean.parseBoolean(isPagination);
    }

    /**
     * 獲取分頁起始標記位置
     * @return
     */
    public int getStartIndex() {
        //(當前頁碼-1)*顯示記錄數
        return (this.getPage()-1)*this.rows;
    }

    /**
     * 末頁
     * @return
     */
    public int getMaxPage() {
        int totalpage=this.total/this.rows;
        if(this.total%this.rows!=0)
            totalpage++;
        return totalpage;
    }

    /**
     * 下一頁
     * @return
     */
    public int getNextPage() {
        int nextPage=this.page+1;
        if(this.page>=this.getMaxPage())
            nextPage=this.getMaxPage();
        return nextPage;
    }

    /**
     * 上一頁
     * @return
     */
    public int getPreivousPage() {
        int previousPage=this.page-1;
        if(previousPage<1)
            previousPage=1;
        return previousPage;
    }

    @Override
    public String toString() {
        return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
                + "]";
    }
}


BookMapper

package com.solar.mapper;

import com.solar.model.Book;
import com.solar.model.vo.BookVo;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;


public interface BookMapper {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);
    /*單個查詢*/
    List<Book> selectBookIn(@Param("bookIds") List bookIds);
    /*模糊查詢*/
    List<Book> selectBookIn1(@Param("bname") String bname);
    List<Book> selectBookIn2(@Param("bname") String bname);
    List<Book> selectBookIn3(@Param("bname") String bname);

    /*
    * mybatis結果集處理的五種情況
    * */
    List<Book> list1();
    List<Book> list2();
    List<Book> list3(BookVo bookVo);
    List<Map> list4(Map map);
    Map list5(Map map);

    /*分頁*/
    List<Map> listPager(Map map);

    /*特殊字元處理*/
    List<Book> list6(BookVo bookVo);



}

BookMapper.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.solar.mapper.BookMapper" >
  <resultMap id="BaseResultMap" type="com.solar.model.Book" >
    <constructor >
      <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List" >
    bid, bname, price
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.solar.model.Book" >
    insert into t_mvc_book (bid, bname, price
      )
    values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.solar.model.Book" >
    insert into t_mvc_book
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="bid != null" >
        bid,
      </if>
      <if test="bname != null" >
        bname,
      </if>
      <if test="price != null" >
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="bid != null" >
        #{bid,jdbcType=INTEGER},
      </if>
      <if test="bname != null" >
        #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null" >
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.solar.model.Book" >
    update t_mvc_book
    <set >
      <if test="bname != null" >
        bname = #{bname,jdbcType=VARCHAR},
      </if>
      <if test="price != null" >
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.solar.model.Book" >
    update t_mvc_book
    set bname = #{bname,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where bid = #{bid,jdbcType=INTEGER}
  </update>
  <!--
    resultType 返回物件
    parameterType 返回引數
    collection 接受傳過來的變數
    item 取名
  -->
  <select id="selectBookIn" resultType="com.solar.model.Book" parameterType="java.util.List">
    select * from t_mvc_book where bid in
    <foreach collection="bookIds" item="bid" open="(" close=")" separator=",">
       #{bid}
    </foreach>
  </select>

  <!--三種模糊查詢-->
  <select id="selectBookIn1" resultType="com.solar.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book where bname like #{bname}
 </select>
  <select id="selectBookIn2" resultType="com.solar.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book where bname like '${bname}'
 </select>
  <select id="selectBookIn3" resultType="com.solar.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
 </select>


  <select id="list1" resultMap="BaseResultMap" >
    select * from t_mvc_book
 </select>
  <select id="list2" resultType="com.solar.model.Book" >
    select * from t_mvc_book
 </select>
  <select id="list3" resultType="com.solar.model.Book" parameterType="com.solar.model.vo.BookVo">
    select * from t_mvc_book where bid in
    <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
      #{bid}
    </foreach>
 </select>
  <select id="list4" resultType="java.util.Map" parameterType="java.util.Map">
    select * from t_mvc_book where bid in
    <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
      #{bid}
    </foreach>
  </select>
  <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
    select * from t_mvc_book where bid = #{bid}
  </select>

  <!--分頁-->
  <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
    select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
  </select>

  <!--特殊字元處理-->
  <select id="list6" resultType="com.solar.model.Book" parameterType="com.solar.model.vo.BookVo">
    select * from t_mvc_book where <![CDATA[ price > #{min} and price < #{max}]]>
  </select>



</mapper>

BookVo

package com.solar.model.vo;

import com.solar.model.Book;

import java.util.List;

/**
 * @author solarWang
 * @site
 * @company
 * @create 2020-10-28 11:26
 */
public class BookVo extends Book {
        private Integer min;
        private Integer max;
        private List<Integer> bookIds;

        public Integer getMin() {
            return min;
        }

        public void setMin(Integer min) {
            this.min = min;
        }

        public Integer getMax() {
            return max;
        }

        public void setMax(Integer max) {
            this.max = max;
        }

        public List<Integer> getBookIds() {
            return bookIds;
        }

        public void setBookIds(List<Integer> bookIds) {
            this.bookIds = bookIds;
        }
}

BookService

package com.solar.service;

import com.solar.model.Book;
import com.solar.model.vo.BookVo;
import com.solar.util.PageBean;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * @author solarWang
 * @site
 * @company
 * @create 2020-10-23 8:50
 */
public interface BookService {
    int deleteByPrimaryKey(Integer bid);

    int insert(Book record);

    int insertSelective(Book record);

    Book selectByPrimaryKey(Integer bid);

    int updateByPrimaryKeySelective(Book record);

    int updateByPrimaryKey(Book record);

    List<Book> selectBookIn(@Param("bookIds") List bookIds);

    List<Book> selectBookIn1(@Param("bname") String bname);
    List<Book> selectBookIn2(@Param("bname") String bname);
    List<Book> selectBookIn3(@Param("bname") String bname);

    /*
     * mybatis結果集處理的五種情況
     * */
    List<Book> list1();
    List<Book> list2();
    List<Book> list3(BookVo bookVo);
    List<Map> list4(Map map);
    Map list5(Map map);

    /*分頁*/
    List<Map> listPager(Map map, PageBean pageBean);

    /*特殊字元處理*/
    List<Book> list6(BookVo bookVo);

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.solar</groupId>
  <artifactId>mybatis</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mybatis Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- ********************** junit單元測試依賴 ********************** -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!--**************************slf4j*****************************-->
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.22</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.22</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.22</version>
    </dependency>


    <!-- ********************** Java Servlet API  ********************** -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.0</version>
      <scope>provided</scope>
    </dependency>

    <!-- ********************** Mybatis依賴 ********************** -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>

    <!-- ********************** Mysql JDBC驅動 ********************** -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.44</version>
    </dependency>

    <!-- **********************  日誌配置  ********************** -->
    <!--記得修改mybatis.cfg.xml新增如下內容-->
    <!--<setting name="logImpl" value="LOG4J2"/>-->
    <!--核心log4j2jar包-->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.9.1</version>
    </dependency>
    <!--web工程需要包含log4j-web,非web工程不需要-->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-web</artifactId>
      <version>2.9.1</version>
    </dependency>
  <!--*****************分頁外掛************************-->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>
  </dependencies>




  <build>
    <resources>
      <!--解決mybatis-generator-maven-plugin執行時沒有將XxxMapper.xml檔案放入target資料夾的問題-->
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <!--解決mybatis-generator-maven-plugin執行時沒有將jdbc.properites檔案放入target資料夾的問題-->
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>jdbc.properties</include>
          <include>*.xml</include>
        </includes>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <dependencies>
          <!--使用Mybatis-generator外掛不能使用太高版本的mysql驅動 -->
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
          </dependency>
        </dependencies>
        <configuration>
          <overwrite>true</overwrite>
        </configuration>
      </plugin>


      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <testFailureIgnore>true</testFailureIgnore>

        </configuration>
      </plugin>




    </plugins>







  </build>




</project>


相關文章