Spring Boot2 + Mybatis 整合(Mybatis自動生成外掛、分頁外掛)
內容:
Spring Boot2 + Mybatis 整合
Mybatis Generator自動生成程式碼
Mybatis PageHelper分頁外掛
建立maven專案
修改pom.xml 注意springboot、druid、pageHelper的版本號
- <?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.sid</groupId>
- <artifactId>springboot</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>jar</packaging>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>
spring-boot-starter-parent</artifactId> - <version>2.0.5.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding
>UTF-8</project.reporting.outputEncoding> - <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <!-- spring-boot的web啟動的jar包 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.4</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.2</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.38</version>
- </dependency>
- <!-- alibaba的druid資料庫連線池 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.9</version>
- </dependency>
- <!-- 分頁外掛-->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper-spring-boot-starter</artifactId>
- <version>1.2.5</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- <!-- mybatis generator 自動生成程式碼外掛 -->
- <plugin>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.2</version>
- <configuration>
- <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
- <overwrite>true</overwrite>
- <verbose>true</verbose>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
建立啟動類App.java
- package com.sid;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- @MapperScan("com.sid.mapper")//將專案中對應的mapper類的路徑加進來就可以了
- public class App {
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- }
建立application.yml
- server:
- port: 8088
- servlet:
- context-path: /sid
- spring:
- mvc:
- view:
- prefix: /
- suffix: .html
- datasource:
- url: jdbc:mysql://localhost:3306/sid
- username: root
- password: root
- # 使用druid資料來源
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.jdbc.Driver
- ## 該配置節點為獨立的節點,不是在在spring的節點下
- mybatis:
- mapper-locations: classpath:mapping/*.xml #注意:一定要對應mapper對映xml檔案的所在路徑
- type-aliases-package: com.sid.model # 注意:對應實體類的路徑
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制檯列印sql
- #pagehelper分頁外掛
- pagehelper:
- helperDialect: mysql
- reasonable: true
- supportMethodsArguments: true
- params: count=countSql
在resources/generator下建立檔案generatorConfig.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE generatorConfiguration
- PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
- "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
- <generatorConfiguration>
- <!-- 資料庫驅動:選擇你的本地硬碟上面的資料庫驅動包-->
- <classPathEntry location="F:\MySQL\Connector.J 5.1\mysql-connector-java-5.1.38-bin.jar"/>
- <context id="DB2Tables" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
- <!--資料庫連結URL,使用者名稱、密碼 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sid" userId="root" password="root">
- </jdbcConnection>
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false"/>
- </javaTypeResolver>
- <!-- 生成模型的包名和位置-->
- <javaModelGenerator targetPackage="com.sid.model" targetProject="src/main/java">
- <property name="enableSubPackages" value="true"/>
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <!-- 生成對映檔案的包名和位置-->
- <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
- <property name="enableSubPackages" value="true"/>
- </sqlMapGenerator>
- <!-- 生成DAO的包名和位置-->
- <javaClientGenerator type="XMLMAPPER" targetPackage="com.sid.mapper" targetProject="src/main/java">
- <property name="enableSubPackages" value="true"/>
- </javaClientGenerator>
- <!-- 要生成的表 tableName是資料庫中的表名或檢視名 domainObjectName是實體類名-->
- <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
- </context>
- </generatorConfiguration>
專案目錄
資料庫中的表
- CREATE TABLE `user` (
- `id` bigint(32) NOT NULL COMMENT '使用者ID',
- `name` varchar(30) NOT NULL COMMENT '使用者名稱',
- `password` varchar(30) NOT NULL COMMENT '密碼',
- `mobile_phone` varchar(20) NOT NULL COMMENT '手機號',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mybatis自動生成程式碼的外掛使用方式
run
然會就會自動生成UserMapper.xml、 User.java 、UserMapper.java
UserController.java
- package com.sid.controller;
- import com.github.pagehelper.PageInfo;
- import com.sid.model.User;
- import com.sid.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- @RequestMapping(value = "/user")
- public class UserController {
- @Autowired
- private UserService userService;
- @ResponseBody
- @RequestMapping(value = "/add")
- public int addUser(User user){
- return userService.addUser(user);
- }
- @ResponseBody
- @RequestMapping(value = "/delete")
- public int deleteUser(String id){
- return userService.deleteUser(id);
- }
- @ResponseBody
- @RequestMapping(value = "/update")
- public int updateUser(User user){
- return userService.updateUser(user);
- }
- @ResponseBody
- @RequestMapping(value = "/selectAll")
- public PageInfo<User> selectAll(int pageNum, int pageSize){
- return userService.selectAll( pageNum, pageSize);
- }
- }
UserService.java
- package com.sid.service;
- import com.github.pagehelper.PageInfo;
- import com.sid.model.User;
- public interface UserService {
- int addUser(User user);
- int deleteUser(String id);
- int updateUser(User user);
- /*
- * pageNum 開始頁數
- * pageSize 每頁顯示的資料條數
- * */
- PageInfo<User> selectAll(int pageNum, int pageSize);
- }
UserServiceImpl.java
- package com.sid.service.impl;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.sid.mapper.UserMapper;
- import com.sid.model.User;
- import com.sid.service.UserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.List;
- @Service(value = "userService")
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserMapper userMapper;
- @Override
- public int addUser(User user) {
- return userMapper.insertSelective(user);
- }
- @Override
- public int deleteUser(String id) {
- return userMapper.deleteByPrimaryKey(id);
- }
- @Override
- public int updateUser(User user) {
- return userMapper.updateByPrimaryKey(user);
- }
- /*
- * pageNum 開始頁數
- * pageSize 每頁顯示的資料條數
- * */
- @Override
- public PageInfo<User> selectAll(int pageNum, int pageSize) {
- PageHelper.startPage(pageNum, pageSize,"id desc"); //開始起始頁
- List<User> userList = userMapper.selectAll(); // 獲取資料
- PageInfo<User> page = new PageInfo<>(userList); // 例項化PageInfo
- return page;
- }
- }
UserMapper.java
- package com.sid.mapper;
- import com.sid.model.User;
- import org.springframework.stereotype.Component;
- import java.util.List;
- @Component
- public interface UserMapper {
- int deleteByPrimaryKey(String id);
- int insert(User record);
- int insertSelective(User record);
- User selectByPrimaryKey(String id);
- int updateByPrimaryKeySelective(User record);
- int updateByPrimaryKey(User record);
- List<User> selectAll();
- }
UserMapping.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.sid.mapper.UserMapper" >
- <resultMap id="BaseResultMap" type="com.sid.model.User" >
- <id column="id" property="id" jdbcType="BIGINT" />
- <result column="name" property="name" jdbcType="VARCHAR" />
- <result column="password" property="password" jdbcType="VARCHAR" />
- <result column="mobile_phone" property="mobilePhone" jdbcType="VARCHAR" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, name, password, mobile_phone
- </sql>
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
- select
- <include refid="Base_Column_List" />
- from user
- where id = #{id,jdbcType=BIGINT}
- </select>
- <select id="selectAll" resultMap="BaseResultMap" >
- select
- <include refid="Base_Column_List" />
- from user
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
- delete from user
- where id = #{id,jdbcType=BIGINT}
- </delete>
- <insert id="insert" parameterType="com.sid.model.User" >
- insert into user (id, name, password,
- mobile_phone)
- values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
- #{mobilePhone,jdbcType=VARCHAR})
- </insert>
- <insert id="insertSelective" parameterType="com.sid.model.User" >
- insert into user
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- id,
- </if>
- <if test="name != null" >
- name,
- </if>
- <if test="password != null" >
- password,
- </if>
- <if test="mobilePhone != null" >
- mobile_phone,
- </if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- #{id,jdbcType=BIGINT},
- </if>
- <if test="name != null" >
- #{name,jdbcType=VARCHAR},
- </if>
- <if test="password != null" >
- #{password,jdbcType=VARCHAR},
- </if>
- <if test="mobilePhone != null" >
- #{mobilePhone,jdbcType=VARCHAR},
- </if>
- </trim>
- </insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.sid.model.User" >
- update user
- <set >
- <if test="name != null" >
-
相關推薦
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以及自動生成model、mapper程式碼
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