Spring boot Mybatis 整合(完整版)

更多干货

SpringBoot系列目录

正题

本项目使用的环境:

  • 开发工具:Intellij IDEA 2017.1.3
  • springboot: 1.5.6
  • jdk:1.8.0_161
  • maven:3.3.9

额外功能

  • PageHelper 分页插件
  • mybatis generator 自动生成代码插件

步骤: 
1.创建一个springboot项目: 

2.创建项目的文件结构以及jdk的版本 
 
3.选择项目所需要的依赖 


然后点击finish

5.看一下文件的结构: 

6.查看一下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.winter</groupId><artifactId>springboot-mybatis-demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot-mybatis-demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.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.7</java.version></properties><dependencies><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><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>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.35</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-joda</artifactId></dependency><dependency><groupId>com.fasterxml.jackson.module</groupId><artifactId>jackson-module-parameter-names</artifactId></dependency><!-- 分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.1.2</version></dependency><!-- alibaba的druid数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.0</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>

7.项目不使用application.properties文件 而使用更加简洁的application.yml文件: 
将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件, 
文件的内容如下:


server:port: 8080spring:datasource:name: testurl: jdbc:mysql://127.0.0.1:3306/depotusername: rootpassword: root# 使用druid数据源type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverfilters: statmaxActive: 20initialSize: 1maxWait: 60000minIdle: 1timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: select 'x'testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxOpenPreparedStatements: 20## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:mapper-locations: classpath:mapping/*.xml  #注意:一定要对应mapper映射xml文件的所在路径type-aliases-package: com.winter.model  # 注意:对应实体类的路径#pagehelper分页插件
pagehelper:helperDialect: mysqlreasonable: truesupportMethodsArguments: trueparams: count=countSql

8.创建数据库:

CREATE DATABASE mytest;CREATE TABLE t_user(user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,user_name VARCHAR(255) NOT NULL ,password VARCHAR(255) NOT NULL ,phone VARCHAR(255) NOT NULL
) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;

9.使用mybatis generator 自动生成代码:

  • 配置pom.xml中generator 插件所对应的配置文件 ${basedir}/src/main/resources/generator/generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration><!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包--><classPathEntry  location="E:\developer\mybatis-generator-core-1.3.2\lib\mysql-connector-java-5.1.25-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://127.0.0.1/mytest" userId="root" password="root"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- 生成模型的包名和位置--><javaModelGenerator targetPackage="com.winter.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.winter.mapper" targetProject="src/main/java"><property name="enableSubPackages" value="true"/></javaClientGenerator><!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--><table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table></context>
</generatorConfiguration>
  • 点击run-Edit Configurations

  • 添加配置

  • 运行 
    注意!!!同一张表一定不要运行多次,因为mapper的映射文件中会生成多次的代码,导致报错,切记 

    最后生成的文件以及结构:

10. 生成的文件

UserMapper.java

package com.winter.mapper;import com.winter.model.User;public interface UserMapper {int deleteByPrimaryKey(Integer userId);int insert(User record);int insertSelective(User record);User selectByPrimaryKey(Integer userId);int updateByPrimaryKeySelective(User record);int updateByPrimaryKey(User record);//这个方式我自己加的List<User> selectAllUser();
}

User.java

package com.winter.model;public class User {private Integer userId;private String userName;private String password;private String phone;public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName == null ? null : userName.trim();}public String getPassword() {return password;}public void setPassword(String password) {this.password = password == null ? null : password.trim();}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone == null ? null : phone.trim();}
}

对于sql语句这种黄色的背景,真心是看不下去了(解决方案): 

UserMapper.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.winter.mapper.UserMapper" ><resultMap id="BaseResultMap" type="com.winter.model.User" ><id column="user_id" property="userId" jdbcType="INTEGER" /><result column="user_name" property="userName" jdbcType="VARCHAR" /><result column="password" property="password" jdbcType="VARCHAR" /><result column="phone" property="phone" jdbcType="VARCHAR" /></resultMap><sql id="Base_Column_List" >user_id, user_name, password, phone</sql><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select <include refid="Base_Column_List" />from t_userwhere user_id = #{userId,jdbcType=INTEGER}</select><!-- 这个方法是我自己加的 --><select id="selectAllUser" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from t_user</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from t_userwhere user_id = #{userId,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.winter.model.User" >insert into t_user (user_id, user_name, password, phone)values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR})</insert><insert id="insertSelective" parameterType="com.winter.model.User" >insert into t_user<trim prefix="(" suffix=")" suffixOverrides="," ><if test="userId != null" >user_id,</if><if test="userName != null" >user_name,</if><if test="password != null" >password,</if><if test="phone != null" >phone,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="userId != null" >#{userId,jdbcType=INTEGER},</if><if test="userName != null" >#{userName,jdbcType=VARCHAR},</if><if test="password != null" >#{password,jdbcType=VARCHAR},</if><if test="phone != null" >#{phone,jdbcType=VARCHAR},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.winter.model.User" >update t_user<set ><if test="userName != null" >user_name = #{userName,jdbcType=VARCHAR},</if><if test="password != null" >password = #{password,jdbcType=VARCHAR},</if><if test="phone != null" >phone = #{phone,jdbcType=VARCHAR},</if></set>where user_id = #{userId,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.winter.model.User" >update t_userset user_name = #{userName,jdbcType=VARCHAR},password = #{password,jdbcType=VARCHAR},phone = #{phone,jdbcType=VARCHAR}where user_id = #{userId,jdbcType=INTEGER}</update>
</mapper>

11.打开类SpringbootMybatisDemoApplication.java,这个是springboot的启动类。我们需要添加点东西:

package com.winter;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.winter.mapper")//将项目中对应的mapper类的路径加进来就可以了
public class SpringbootMybatisDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootMybatisDemoApplication.class, args);}
}

注意:@MapperScan("com.winter.mapper")这个注解非常的关键,这个对应了项目中mapper(dao)所对应的包路径,很多同学就是这里忘了加导致异常的

12.到这里所有的搭建工作都完成了,接下来就是测试的工作,没使用junit4进行测试: 
首先看一下完成之后的文件的结构: 

现在controller,service层的代码都写好:

UserController.java

package com.winter.Controller;import com.winter.model.User;
import com.winter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;/*** Created by Administrator on 2017/8/16.*/
@Controller
@RequestMapping(value = "/user")
public class UserController {@Autowiredprivate UserService userService;@ResponseBody@RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})public int addUser(User user){return userService.addUser(user);}@ResponseBody@RequestMapping(value = "/all/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})public Object findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){return userService.findAllUser(pageNum,pageSize);}
}

UserService.java

package com.winter.service;import com.winter.model.User;import java.util.List;/*** Created by Administrator on 2017/8/16.*/
public interface UserService {int addUser(User user);List<User> findAllUser(int pageNum, int pageSize);
}

UserServiceImpl.java

package com.winter.service.impl;import com.github.pagehelper.PageHelper;
import com.winter.mapper.UserMapper;
import com.winter.model.User;
import com.winter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** Created by Administrator on 2017/8/16.*/
@Service(value = "userService")
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;//这里会报错,但是并不会影响@Overridepublic int addUser(User user) {return userMapper.insertSelective(user);}/** 这个方法中用到了我们开头配置依赖的分页插件pagehelper* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;* pageNum 开始页数* pageSize 每页显示的数据条数* */@Overridepublic List<User> findAllUser(int pageNum, int pageSize) {//将参数传给这个方法就可以实现物理分页了,非常简单。PageHelper.startPage(pageNum, pageSize);return userMapper.selectAllUser();}
}

如果强迫症看不下去那个报错:(解决方法) 

测试我使用了idea一个很用心的功能。 
可以发http请求的插件: 

点击左侧的运行按钮就可以发送请求了; 
如果返回值正确 说明你已经搭建成功了!!

如果出现mapper注入不了的情况,请检查版本,当前博客的搭建方法只适合1.5.*版本的,如果你的版本是2.0以上的版本,请参照我的另一篇博客的mybatis的配置:springboot2.0整合mybatis

如果大家想使用事务控制的话,请参照Spring boot Mybatis 整合(注解版) ,这个里面就有关于事务控制的使用

源码地址:https://github.com/WinterChenS/springboot-mybatis-demo

Spring boot Mybatis 整合(完整版)相关推荐

  1. Spring boot Mybatis 整合(注解版)

    之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...

  2. Spring boot Mybatis 整合

    PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版)   这篇博客里用到了怎样 生成 mybatis 插件来写程 ...

  3. spring boot+mybatis整合

    LZ今天自己搭建了下Spring boot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多.其实只用Spring boot也可以开发,但是对于多表多条件分页查询,Sp ...

  4. spring boot mybatis 整合_Spring、MyBatis和SpringMVC的整合

    SSM框架整合的知识. 不用maven,为什么呢?主要是帮助更好的理解有哪些包,这样更加透彻.当然了,使用maven会更方便一点. 1 jar包管理 2 整合思路 spring在进行管理时,是很有条理 ...

  5. spring boot mybatis 整合_MyBatis学习:MyBatis和Spring整合

    1. 整合的工程结构 首先我们来看下整合之后的工程结构是什么样的. 2. 配置文件 在于spring整合之前,mybatis都是自己管理数据源的,然后sqlSessionFactory是我们自己去注入 ...

  6. spring boot mybatis 整合_两大热门框架 Spring 与 Mybatis 如何整合呢?

    整合的方式 新建 maven 项目 引入依赖包 配置资源文件 案例实操 新建 maven 项目 新建 maven 项目 spring_mybatis 目录结构如下: 主目录包: ​ com.xxx.d ...

  7. Spring Boot集成kafka完整版

    pom.xml添加maven依赖 <parent><groupId>org.springframework.boot</groupId><artifactId ...

  8. Spring Boot + Mybatis 快速整合

    引言 最近在工作结束后抽时间学习了一下mybatis的知识,因为之前有学习过,但是经久不用,也未曾踏实地整理,因此有所淡忘. super meeting会议管理系统是我厂最近开发的一套会议预约平台.持 ...

  9. 第七章、Spring Boot MyBatis升级篇

    课时二十七.Spring Boot MyBatis升级篇-注解 缘起:在一节视频中,有这么一段留言:"会不会推出SpringBoot整合Mybaits配置文件sqlMapConfig.xml ...

最新文章

  1. Android 隐式跳转(Activity的隐式跳转)
  2. Flume Sinks官网剖析(博主推荐)
  3. springboot读取linux文件_SpringBoot读取Resource下文件的几种方式
  4. Numpy Axis
  5. 比特币早期投资家:没有人能够阻止其发展 TechWeb 09-27 09:10 凤凰科技讯 据CNBC网站北京时间9月27日报道,风险投资家、“Social+Capital”基金创始人Chamath
  6. 一种新的在线学习的方法:能够克服单人多任务学习的困难!
  7. 数据结构Java10【哈希表概述、散列函数的设计、散列冲突解决方案】
  8. PowerDesigner-快速入门(极简教程)
  9. CSS Hide(隐藏元素)
  10. cocos2d之z轴位置示例
  11. 敏捷软件开发VS传统软件开发
  12. 《程序员代码面试指南》第一章:栈与队列
  13. The jar of this class file belongs to container 'JUnit 4' which does not allow ...
  14. audio2mid:音频提取主旋律
  15. RPM包安装MYSQL
  16. lambda函数(配合map函数)以及[x for x in]的超详细解释(实例+原理)
  17. Java 实现短信验证功能(个人测试,不需要项目上线,利用容联云)
  18. Android设置应用图标
  19. linux下Ftp客户端程序与Makefile
  20. Xshell7 要继续使用此程序,您必须应用最新的更新或使用新版本

热门文章

  1. 频繁gc是什么意思_[JVM]一次线上频繁GC的问题解决
  2. visual basic开发实例大全(基础卷_2021中考数学、物理、化学公式大全!建议收藏!...
  3. ROS 教程之 vision : 用各种摄像头获取图像
  4. 彻底学会Spring的IOC和AOP——理论+实操
  5. c4d如何把文字贴在物体表面_一篇文章带你了解C4D布光技巧
  6. 力扣326.3的幂 C/C++
  7. 斐波那契数列的性质整理
  8. unity平行光太亮?物体发白?可能你使用了2个或多个平行光
  9. AJAX04 JQ的AJAX
  10. Cyclone IV FPGA 器件笔记