1. 程式人生 > >Spring Boot2 + Mybatis 整合(Mybatis自動生成外掛、分頁外掛)

Spring Boot2 + Mybatis 整合(Mybatis自動生成外掛、分頁外掛)

內容:

Spring Boot2 + Mybatis 整合

Mybatis Generator自動生成程式碼

Mybatis PageHelper分頁外掛

建立maven專案

修改pom.xml 注意springboot、druid、pageHelper的版本號

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    >
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.sid</groupId>
  6. <artifactId>springboot</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>
    spring-boot-starter-parent</artifactId>
  12. <version>2.0.5.RELEASE</version>
  13. <relativePath/> <!-- lookup parent from repository -->
  14. </parent>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <project.reporting.outputEncoding
    >
    UTF-8</project.reporting.outputEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <!-- spring-boot的web啟動的jar包 -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.commons</groupId>
  33. <artifactId>commons-lang3</artifactId>
  34. <version>3.4</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.mybatis.spring.boot</groupId>
  38. <artifactId>mybatis-spring-boot-starter</artifactId>
  39. <version>1.3.2</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>mysql</groupId>
  43. <artifactId>mysql-connector-java</artifactId>
  44. <version>5.1.38</version>
  45. </dependency>
  46. <!-- alibaba的druid資料庫連線池 -->
  47. <dependency>
  48. <groupId>com.alibaba</groupId>
  49. <artifactId>druid-spring-boot-starter</artifactId>
  50. <version>1.1.9</version>
  51. </dependency>
  52. <!-- 分頁外掛-->
  53. <dependency>
  54. <groupId>com.github.pagehelper</groupId>
  55. <artifactId>pagehelper-spring-boot-starter</artifactId>
  56. <version>1.2.5</version>
  57. </dependency>
  58. </dependencies>
  59. <build>
  60. <plugins>
  61. <plugin>
  62. <groupId>org.springframework.boot</groupId>
  63. <artifactId>spring-boot-maven-plugin</artifactId>
  64. </plugin>
  65. <!-- mybatis generator 自動生成程式碼外掛 -->
  66. <plugin>
  67. <groupId>org.mybatis.generator</groupId>
  68. <artifactId>mybatis-generator-maven-plugin</artifactId>
  69. <version>1.3.2</version>
  70. <configuration>
  71. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  72. <overwrite>true</overwrite>
  73. <verbose>true</verbose>
  74. </configuration>
  75. </plugin>
  76. </plugins>
  77. </build>
  78. </project>

建立啟動類App.java

  1. package com.sid;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan("com.sid.mapper")//將專案中對應的mapper類的路徑加進來就可以了
  7. public class App {
  8. public static void main(String[] args) {
  9. SpringApplication.run(App.class, args);
  10. }
  11. }

建立application.yml

  1. server:
  2. port: 8088
  3. servlet:
  4. context-path: /sid
  5. spring:
  6. mvc:
  7. view:
  8. prefix: /
  9. suffix: .html
  10. datasource:
  11. url: jdbc:mysql://localhost:3306/sid
  12. username: root
  13. password: root
  14. # 使用druid資料來源
  15. type: com.alibaba.druid.pool.DruidDataSource
  16. driver-class-name: com.mysql.jdbc.Driver
  17. ## 該配置節點為獨立的節點,不是在在spring的節點下
  18. mybatis:
  19. mapper-locations: classpath:mapping/*.xml #注意:一定要對應mapper對映xml檔案的所在路徑
  20. type-aliases-package: com.sid.model # 注意:對應實體類的路徑
  21. configuration:
  22. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制檯列印sql
  23. #pagehelper分頁外掛
  24. pagehelper:
  25. helperDialect: mysql
  26. reasonable: true
  27. supportMethodsArguments: true
  28. params: count=countSql

在resources/generator下建立檔案generatorConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!-- 資料庫驅動:選擇你的本地硬碟上面的資料庫驅動包-->
  7. <classPathEntry location="F:\MySQL\Connector.J 5.1\mysql-connector-java-5.1.38-bin.jar"/>
  8. <context id="DB2Tables" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <property name="suppressDate" value="true"/>
  11. <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
  12. <property name="suppressAllComments" value="true"/>
  13. </commentGenerator>
  14. <!--資料庫連結URL,使用者名稱、密碼 -->
  15. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sid" userId="root" password="root">
  16. </jdbcConnection>
  17. <javaTypeResolver>
  18. <property name="forceBigDecimals" value="false"/>
  19. </javaTypeResolver>
  20. <!-- 生成模型的包名和位置-->
  21. <javaModelGenerator targetPackage="com.sid.model" targetProject="src/main/java">
  22. <property name="enableSubPackages" value="true"/>
  23. <property name="trimStrings" value="true"/>
  24. </javaModelGenerator>
  25. <!-- 生成對映檔案的包名和位置-->
  26. <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
  27. <property name="enableSubPackages" value="true"/>
  28. </sqlMapGenerator>
  29. <!-- 生成DAO的包名和位置-->
  30. <javaClientGenerator type="XMLMAPPER" targetPackage="com.sid.mapper" targetProject="src/main/java">
  31. <property name="enableSubPackages" value="true"/>
  32. </javaClientGenerator>
  33. <!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名-->
  34. <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  35. </context>
  36. </generatorConfiguration>

專案目錄

資料庫中的表

  1. CREATE TABLE `user` (
  2. `id` bigint(32) NOT NULL COMMENT '使用者ID',
  3. `name` varchar(30) NOT NULL COMMENT '使用者名稱',
  4. `password` varchar(30) NOT NULL COMMENT '密碼',
  5. `mobile_phone` varchar(20) NOT NULL COMMENT '手機號',
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mybatis自動生成程式碼的外掛使用方式

run

然會就會自動生成UserMapper.xml、 User.java 、UserMapper.java

UserController.java

  1. package com.sid.controller;
  2. import com.github.pagehelper.PageInfo;
  3. import com.sid.model.User;
  4. import com.sid.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. @Controller
  10. @RequestMapping(value = "/user")
  11. public class UserController {
  12. @Autowired
  13. private UserService userService;
  14. @ResponseBody
  15. @RequestMapping(value = "/add")
  16. public int addUser(User user){
  17. return userService.addUser(user);
  18. }
  19. @ResponseBody
  20. @RequestMapping(value = "/delete")
  21. public int deleteUser(String id){
  22. return userService.deleteUser(id);
  23. }
  24. @ResponseBody
  25. @RequestMapping(value = "/update")
  26. public int updateUser(User user){
  27. return userService.updateUser(user);
  28. }
  29. @ResponseBody
  30. @RequestMapping(value = "/selectAll")
  31. public PageInfo<User> selectAll(int pageNum, int pageSize){
  32. return userService.selectAll( pageNum, pageSize);
  33. }
  34. }

UserService.java

  1. package com.sid.service;
  2. import com.github.pagehelper.PageInfo;
  3. import com.sid.model.User;
  4. public interface UserService {
  5. int addUser(User user);
  6. int deleteUser(String id);
  7. int updateUser(User user);
  8. /*
  9. * pageNum 開始頁數
  10. * pageSize 每頁顯示的資料條數
  11. * */
  12. PageInfo<User> selectAll(int pageNum, int pageSize);
  13. }

UserServiceImpl.java

  1. package com.sid.service.impl;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.sid.mapper.UserMapper;
  5. import com.sid.model.User;
  6. import com.sid.service.UserService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.util.List;
  10. @Service(value = "userService")
  11. public class UserServiceImpl implements UserService {
  12. @Autowired
  13. private UserMapper userMapper;
  14. @Override
  15. public int addUser(User user) {
  16. return userMapper.insertSelective(user);
  17. }
  18. @Override
  19. public int deleteUser(String id) {
  20. return userMapper.deleteByPrimaryKey(id);
  21. }
  22. @Override
  23. public int updateUser(User user) {
  24. return userMapper.updateByPrimaryKey(user);
  25. }
  26. /*
  27. * pageNum 開始頁數
  28. * pageSize 每頁顯示的資料條數
  29. * */
  30. @Override
  31. public PageInfo<User> selectAll(int pageNum, int pageSize) {
  32. PageHelper.startPage(pageNum, pageSize,"id desc"); //開始起始頁
  33. List<User> userList = userMapper.selectAll(); // 獲取資料
  34. PageInfo<User> page = new PageInfo<>(userList); // 例項化PageInfo
  35. return page;
  36. }
  37. }

UserMapper.java

  1. package com.sid.mapper;
  2. import com.sid.model.User;
  3. import org.springframework.stereotype.Component;
  4. import java.util.List;
  5. @Component
  6. public interface UserMapper {
  7. int deleteByPrimaryKey(String id);
  8. int insert(User record);
  9. int insertSelective(User record);
  10. User selectByPrimaryKey(String id);
  11. int updateByPrimaryKeySelective(User record);
  12. int updateByPrimaryKey(User record);
  13. List<User> selectAll();
  14. }

UserMapping.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.sid.mapper.UserMapper" >
  4. <resultMap id="BaseResultMap" type="com.sid.model.User" >
  5. <id column="id" property="id" jdbcType="BIGINT" />
  6. <result column="name" property="name" jdbcType="VARCHAR" />
  7. <result column="password" property="password" jdbcType="VARCHAR" />
  8. <result column="mobile_phone" property="mobilePhone" jdbcType="VARCHAR" />
  9. </resultMap>
  10. <sql id="Base_Column_List" >
  11. id, name, password, mobile_phone
  12. </sql>
  13. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
  14. select
  15. <include refid="Base_Column_List" />
  16. from user
  17. where id = #{id,jdbcType=BIGINT}
  18. </select>
  19. <select id="selectAll" resultMap="BaseResultMap" >
  20. select
  21. <include refid="Base_Column_List" />
  22. from user
  23. </select>
  24. <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
  25. delete from user
  26. where id = #{id,jdbcType=BIGINT}
  27. </delete>
  28. <insert id="insert" parameterType="com.sid.model.User" >
  29. insert into user (id, name, password,
  30. mobile_phone)
  31. values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
  32. #{mobilePhone,jdbcType=VARCHAR})
  33. </insert>
  34. <insert id="insertSelective" parameterType="com.sid.model.User" >
  35. insert into user
  36. <trim prefix="(" suffix=")" suffixOverrides="," >
  37. <if test="id != null" >
  38. id,
  39. </if>
  40. <if test="name != null" >
  41. name,
  42. </if>
  43. <if test="password != null" >
  44. password,
  45. </if>
  46. <if test="mobilePhone != null" >
  47. mobile_phone,
  48. </if>
  49. </trim>
  50. <trim prefix="values (" suffix=")" suffixOverrides="," >
  51. <if test="id != null" >
  52. #{id,jdbcType=BIGINT},
  53. </if>
  54. <if test="name != null" >
  55. #{name,jdbcType=VARCHAR},
  56. </if>
  57. <if test="password != null" >
  58. #{password,jdbcType=VARCHAR},
  59. </if>
  60. <if test="mobilePhone != null" >
  61. #{mobilePhone,jdbcType=VARCHAR},
  62. </if>
  63. </trim>
  64. </insert>
  65. <update id="updateByPrimaryKeySelective" parameterType="com.sid.model.User" >
  66. update user
  67. <set >
  68. <if test="name != null" >
  69. 相關推薦

    Spring Boot2 + Mybatis 整合(Mybatis自動生成外掛外掛)

    內容: Spring Boot2 + Mybatis 整合 Mybatis Generator自動生成程式碼 Mybatis PageHelper分頁外掛

    Spring Boot2.0整合Mybatis(自動生成註解方式,多環境配置)

    本介紹Spring Boot2.0整合Mybatis,通過MyBatis Generator外掛自動生成mapper的sql註解及Provider類的過程,支援多環境的yml配置檔案首先用IDE開發工具(IDEA,STS,Eclipse)建立一個Spring Boot工程sp

    自動生成程式碼mybatis-generator外掛pagehelper

    Maven專案目錄結構 自動生成程式碼  ---mybatis-generator 根據資料庫自動生成pojo和dao還有相對應的xml檔案 在resources下建立generatorConfig.xml <?xml version="1.0"

    SpringBoot+Mybatis+Maven以及自動生成modelmapper程式碼

    Springboot可以簡化搭建專案,還可實現自動生成model/mapper程式碼等。 注意: 搭建的步驟: 第一步:需要選擇Spring Initializr,如果搭建maven專案的時候需

    Mybatis-Plus來學習一下!程式碼生成外掛

    在這裡小小推薦下我的個人部落格 簡書:雷園的簡書 今天我們來說一下Mybatis-Plus! 個人認為呢,Mybatis-Plus是Mybatis的增強版,他只是在Mybatis的基礎上增加了功能,且並未對原有功能進行任何的改動。可謂是非常良心的一款開源

    mybatis generator外掛系列--外掛

    1、首先定義分頁外掛 MysqlPagePlugin.java package com.demo.mybatis.plugin; import org.mybatis.generator.api.CommentGenerator; import org.mybati

    mybatis的通用Mapper外掛以及外掛(2018/1/17)

    SSM框架 spring4.0.2+mybatis3.2.6 maven工程 不會搭建框架的小哥們請自行前往這個大神的部落格搭建,本博文只針對於通用mapper外掛 廢話不多說 首先引入依賴(版本我用的都是最新的): 程式碼塊中有左邊這個span標籤,我不知道怎麼弄了去

    關於mybatis中對mysql和Oracle資料庫外掛的使用

    首先是Oracle資料庫:在mybatis相對應的mapper.xml檔案裡:<sql id="OracleDialectPrefix"> <!-- WARNIN

    手把手教你如何玩轉外掛外掛(Pagehelper)

    情景引入:小白:起床起床,,,快起床!!!我:怎麼怎麼了,小白你到底又怎麼了。。小白:我發現在Web系統中,分頁是一種很常見的功能,可是,我之前寫的方法都比較麻煩,移植性不是很高,有沒有什麼好辦法可以快速實現分頁的呢?我:確實是的,分頁功能幾乎在每個系統中都是存在的,它的實現

    SpringBoot入門篇--整合mybatis+generator自動生成程式碼+druid連線池+PageHelper外掛

    我們這一一篇部落格講的是如何整合Springboot和Mybatis框架,然後使用generator自動生成mapper,pojo等檔案。然後再使用阿里巴巴提供的開源連線池druid,這個連線池的好處我就不說了,集合了所有連線池的好處,並且還提供了監控等功能,加大了可擴充套件性等等。   1.&

    十九Spring boot中整合mybatis-generator自動生成程式碼

    (一)新增外掛 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugi

    Spring Boot (七)MyBatis代碼自動生成和輔助插件

    erb resources dbcc pri artifact target 業務 ssd 只需要 一、簡介 1.1 MyBatis Generator介紹 MyBatis Generator 是MyBatis 官方出品的一款,用來自動生成MyBatis的 mapper、d

    Sprin Boot2.0之整合Mybatis整合外掛

    pageHelper PageHelper 是一款好用的開源免費的 Mybatis 第三方物理分頁外掛 物理分頁 支援常見的 12 種資料庫。Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等 支援多種分頁方式 支援常見的 RowBounds

    Eclipse外掛MyBatis Generator程式碼自動生成工具

    MyBatis Generator是一款優秀的工具,可以幫助我們自動生成java實體類,mapper介面和xml,極大得簡化了開發流程,今天,就記錄下在eclipse中使用eclipse外掛整合MyBatis Generator的步驟; 【1:外掛安裝】Help--Eclipser Market

    Spring,SpringBoot 整合 MyBatis外掛 PageHelper

    SPRING BOOT, Spring整合 MyBatis 的分頁外掛 PageHelper 原創   2018-04-03  宗野   Spring Boot    昨天給各位總結了

    spring boot2.0+shiro+mybatis多資料來源+druid連線池專案整合

    關於整合    網上關於springboot2.0和shiro+myabtis整合的案例很少,大神的教程也是用jpa編寫,jpa很方便,但是還有很多人用mybatis,加之剛學習完mybatis多資料來源整合和druid連線池監控配置,所以算是階段性記錄。 專案目

    Spring Boot2.0.3 Mybatis Sharding-jdbc3.X整合實現資料庫的讀寫分離(一)MySql的主從配置

    Mysql的安裝 這個網上一大堆的教程,我這裡安裝的mysql5.7。這裡就不細說了,直接貼相應的命令 wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm yum locali

    spring-boot 整合mybatis外掛PageHelper版本問題

    Pagehelper 5.0.0及以上版本。使用的是這個類Pagehelper,我們在專案中可以這樣寫: PageInterceptor pageHelper = new PageInterceptor(); properties.setProperty(“helperDialect”, “

    Mybatis generator 程式碼 自動生成外掛

    前言:使用IDEA搭建springboot+mybatis框架,自動生成基礎檔案和mapping.xml 第一步: 在pom檔案中: <build> <plugins> <plugin>

    SSM+Maven整合時在Eclipse中使用Mybatis逆向工程自動生成程式碼

    場景 MybatisGenerator 官方文件 http://www.mybatis.org/generator/configreference/xmlconfig.html 實現 專案搭建好完整的包,包括bean、dao、service、test、utils、mapper