SpringBoot整合Mybatis完整版
喜欢的朋友可以关注下,粉丝也缺。
自从Spring推出Boot,Cloud系列之后,一度成为热门的框架,现在大部分的招聘要求都要有相关的开发经验,借此我在这里就给大家分享一下如何玩转SpringBoot跟Mybatis。
这里我给大家提供我创建的demo下载地址
https://download.csdn.net/download/dsn727455218/10539629
在这里就不跟大家说废话,我们实际操作一番。
使用工具:
eclipse 版本随意
jdk 环境
mysql 数据库
这个工具的安装使用我就不给大家介绍,相信你们都能完成。
SpringBoot项目创建的方式:
1.访问 http://start.spring.io/
这个是最为简单的 输入项目名,包名,以及项目类型后,版本现在基本都是2.0+,下载导入到开发工具即可

2.Spring Boot CLI cmd命令操作
下载安装配置环境我就不多做介绍,很简单的操作这不是我介绍的重点
3.利用eclipse来创建SpringBoot 这才是我介绍的重点
首先我们需要在eclipse上面集成STS,至于这是什么,自己可以去Sping的官网看介绍。
eclipse安装:
Help -> Eclipse Marketplace…
Search或选择“Popular”标签,选择Spring Tool Suite (STS) for Eclipse插件,安装
网络不好的可以下载有点慢,慢慢等安装。
new project ,选择spring -> spring starter project

按自己的信息填写,我这里项目名叫demo1
选择版本和组件
我这里选了2.0.3版本,现在基本都是2.0+的版本,选了mysql和web,mybatis,因为是web项目,都会选择web这个选项,其他的可以按自己需要选择,点击 Finish ,就会有一个新项目,不过需要等待几分钟,sts工具会生成spring boot目录的结构及文件
这就是我们最终的结构。
这里需要提示一点如果没有dependencies包,检查一下项目目录下.classpath 里面是否有
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
没有就加上,重启eclipse即可,其他原因我就不做多的说明,相信你们能够发现。
接下来我们就来创建包
controller :控制层
dao:数据接口
service:实现类
mapper:这里是放dao层的xml文件 也就是mybatis的
entity:实体类
这几个包是做什么的你们应该能开明白
就是这个样子的
pom.xml 配置文件
这里我只是配置一个基础的,如果需要更加详细的配置文件可以参考我的另一篇文章
https://blog.csdn.net/dsn727455218/article/details/81028192
接下来我们就写写代码try-try
首先我们需要先创建一个userinfo表 字段(id,username,userpass,name)
对应的实体类UserInfo
/**
* @author dsn
*
* @version 创建时间:2018年7月5日 上午11:30:00
*/
package com.bootdemo.entity; /**
* @author dsn
* @version 创建时间:2018年7月5日 上午11:30:00
*/
public class UserInfo { /**
* @return the id
*/
public int getId() {
return id;
} /**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
} /**
* @return the username
*/
public String getUsername() {
return username;
} /**
* @param username
* the username to set
*/
public void setUsername(String username) {
this.username = username;
} /**
* @return the userpass
*/
public String getUserpass() {
return userpass;
} /**
* @param userpass
* the userpass to set
*/
public void setUserpass(String userpass) {
this.userpass = userpass;
} /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
} private int id;
private String username;
private String userpass;
private String name; }
dao层接口UserInfoMapper 这里我是继承了BaseMapper父类,一些方法是可以共用的
/**
* @author dsn
*
* @version 创建时间:2018年7月5日 上午11:35:10
*/
package com.bootdemo.dao; import com.bootdemo.entity.UserInfo; /**
* @author dsn
* @version 创建时间:2018年7月5日 上午11:35:10
*/
public interface UserInfoMapper extends BaseMapper<UserInfo> {
//添加单个对象
// public int insert(User entity);
}
BaseMapper
package com.bootdemo.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface BaseMapper<T> {
//添加单个对象
public int insert(T entity);
//修改单个对象
public int update(T entity);
//删除单个对象
public int delete(T entity);
public int deletebyname(T entity);
//查询单个对象
public T select(T entity);
//查询多个对象
public List<T> query();
//根据分页查询多个对象
public List<T> selectbypage(@Param(value = "pageSize") int pageSize, @Param(value = "currentPage") int currentPage);
//根据搜索条件查询
public List<T> searchbytj(@Param(value = "st") String st, @Param(value = "et") String et,
@Param(value = "con") String con);
//查询总条数
public int selectCount();
}
service层UserInfoService 同样的我继承了BaseService
/**
* @author dsn
*
* @version 创建时间:2018年7月5日 上午11:35:36
*/
package com.bootdemo.service; import com.bootdemo.entity.UserInfo; /**
* @author dsn
* @version 创建时间:2018年7月5日 上午11:35:36
*/
public interface UserInfoService extends BaseService<UserInfo> { }
BaseService
package com.bootdemo.service;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface BaseService<T> {
// 添加单个对象
public int insert(T entity) throws Exception;
// 修改单个对象
public int update(T entity) throws Exception;
// 删除单个对象
public int delete(T entity) throws Exception;
public int deletebyname(T entity);
// 查询单个对象
public T select(T entity);
//查询多个对象
public List<T> query();
//分页查询多个对象
public List<T> selectbypage(@Param(value = "pageSize") int pageSize, @Param(value = "currentPage") int currentPage);
//根据搜索条件查询
public List<T> searchbytj(@Param(value = "st") String st, @Param(value = "et") String et,
@Param(value = "con") String con);
//查询总条�?
public int selectCount();
}
mapper文件UserInfoMapper.xml 这是mybatis的sql
<?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.bootdemo.dao.UserInfoMapper" >
<resultMap id="userResultMap" type="com.bootdemo.entity.UserInfo" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="userpass" property="userpass" jdbcType="VARCHAR" />
</resultMap>
<select id="selectList" parameterType="UserInfo" resultMap="userResultMap">
select
id,username,userpass,name
from userinfo
<where>
disable='1'
<if test="key!=null">
and (
username = #{key,jdbcType=VARCHAR}
or
name = #{key,jdbcType=VARCHAR}
)
</if>
</where>
</select>
<insert id="insert" parameterType="UserInfo"
useGeneratedKeys="true" keyProperty="id">
insert into userinfo(name,username,userpass)
values(#{name},#{username},#{userpass})
</insert>
</mapper>
controller层UserInfoController
/**
* @author dsn
*
* @version 创建时间:2018年7月12日 下午2:04:58
*/
package com.bootdemo.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.bootdemo.entity.UserInfo;
import com.bootdemo.service.UserInfoService; /**
* @author dsn
* @version 创建时间:2018年7月12日 下午2:04:58
*/
@RestController
@RequestMapping("user")
public class UserInfoController { @Resource
public UserInfoService userinfoService; @RequestMapping("/add.do")
@ResponseBody
public String addUser(UserInfo user, String msg) { System.out.println(user.getUsername()); try {
int insert = userinfoService.insert(user);
if (insert == 1) {
msg = "插入成功";
} else {
msg = "插入成功";
}
} catch (Exception e) {
e.printStackTrace();
}
return msg; }
}
需要注意的是我们需要在DemoApplication里面添加
扫描映射文件不然启动项目会报错的
同样也可以再mapper接口类里面加@Mapper注解,两者皆可
我们只需要运行DemoApplication文件即可。

成功界面,我们会看到Spring大大的logo
平常我们web项目都是在tomcat等容器里面启动,这里为什么不需要呢,是SpringBoot已经集成了tomcat,所以不需要我们在配置了。
如果有的码友需要看注解的源码那么打开dependencies
一些jdbc等等常用的配置文件信息都可以查看的。
这里我给大家提供我创建的demo下载地址
https://download.csdn.net/download/dsn727455218/10539629
如有需要可以加我Q群【308742428】大家一起讨论技术。
后面会不定时为大家更新文章,敬请期待。
喜欢的朋友可以关注下,粉丝也缺。
如果对你有帮助,请打赏一下!!!

SpringBoot整合Mybatis完整版的更多相关文章
- mybatis源码学习(四)--springboot整合mybatis原理
我们接下来说:springboot是如何和mybatis进行整合的 1.首先,springboot中使用mybatis需要用到mybatis-spring-boot-start,可以理解为mybati ...
- springboot整合mybatis,利用mybatis-genetor自动生成文件
springboot整合mybatis,利用mybatis-genetor自动生成文件 项目结构: xx 实现思路: 1.添加依赖 <?xml version="1.0" e ...
- SpringBoot整合MyBatis,HiKari、Druid连接池的使用
SpringBoot整合MyBatis 1.创建项目时勾选mybatis.数据库驱动. mysql驱动默认是8.x的版本,如果要使用5.x的版本,创建后到pom.xml中改. 也可以手动添加依赖 ...
- springboot整合mybatis源码分析
springboot整合mybatis源码分析 本文主要讲述mybatis在springboot中是如何被加载执行的,由于涉及的内容会比较多,所以这次只会对调用关系及关键代码点进行讲解,为了避免文章太 ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- springboot整合mybatis出现的一些问题
springboot整合mybatis非常非常的简单,简直简单到发指.但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用 整合mybatis,无疑需要mapper文件,实体类,dao ...
随机推荐
- java之路 打印1到100之间的数
class Demo12{ public static void main(String[] args){ /** * 打印1到100之间的数 * 循环条件:1~100 * * 计数器 * */ // ...
- Java实现多线程生产者消费者模型及优化方案
生产者-消费者模型是进程间通信的重要内容之一.其原理十分简单,但自己用语言实现往往会出现很多的问题,下面我们用一系列代码来展现在编码中容易出现的问题以及最优解决方案. /* 单生产者.单消费者生产烤鸭 ...
- js 对象与json的转化
1.将对象转换为JSON格式字符串 JSON.stringify(object) 2.将JSON字符串转换为对象 JSON.parse(jsonString);
- 16.The Effect of Advertisement 广告的影响
16.The Effect of Advertisement 广告的影响 (1) The appeal of advertising to buying motives can have both n ...
- lwip协议栈学习---udp
书籍:<嵌入式网络那些事-lwip协议> udp协议的优点: 1)基于IP协议,无连接的用户数据报协议,适用于传送大批量数据, 2)实时性比较高,适用于嵌入式网络 发送函数:udp_sen ...
- java模板设计模式
1.概述 模板设计模式定义:定义一个操作中的算法骨架,将步骤延迟到子类中. 模板设计模式是一种行为设计模式,一般是准备一个抽象类,将部分逻辑以具体方法或者具体的构造函数实现,然后声明一些抽象方法,这样 ...
- web测试和app测试的区别
功能上: 功能上没有什么区别,都是用同样的方法来写用例(等效.边界值...) 架构上: web是B/S架构(浏览器和服务器)代码更新后数据会同步,可以保证所有客户一致 app是C/S架构(客户端和服务 ...
- 流量控制与RateLimiter
一背景 如何提高系统的稳定性,简单来说除了加机器外就是服务降级.限流.加机器就是常说的分布式,从整个架构的稳定性角度看,一般SOA每个接口的所能提供的单位时间服务能力是有上限.假如超过服务能力,一般会 ...
- Paper | Octave Convolution(OctConv)
目录 1. 尺度空间理论(scale-space theory) 2. OctConv 3. 启发 论文:Drop an Octave: Reducing Spatial Redundancy in ...
- 记录做一个类似于探探的卡片式布局的Recycleview有数据一直不显示
使用了别人的项目 https://github.com/JerryChan123/ReSwipeCard/blob/master/README_zh.md 之前找recycleview有数据不显示的原 ...