一、创建Springboot的配置文件:application.properties,并添加MyBatis依赖

SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序:

  1. 当前目录下的/config子目录中
  2. 当前目录中
  3. 一个 classpath 包下的 /config 目录中
  4. classpath 根目录中

 大家根据自己习惯来即可。

(1)application.properties 文件配置如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1/test
spring.datasource.username=root
spring.datasource.password=root

(2)添加 MyBatis 以及 MYSQL

        <!-- SpringBoot - MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

二、创建POJO、Mapper、Service 以及 Controler

(1)User.java

package org.rcddup.app.domain;

public class User {
private Long id;
private String name;
private Short age;
private Short status; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Short getAge() {
return age;
} public void setAge(Short age) {
this.age = age;
} public Short getStatus() {
return status;
} public void setStatus(Short status) {
this.status = status;
} }

(2)UserMapper.java

package org.rcddup.app.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.rcddup.app.domain.User; @Mapper
public interface UserMapper { @Select("select id, name, age, status from user")
List<User> listUser(); @Select("select id, name, age, status from user where id=#{id}")
User getUserById(Long id); }

(3)IUserService.java

package org.rcddup.app.service;

import java.util.List;

import org.rcddup.app.domain.User;

public interface IUserService {

    List<User> listUser();

    User getUserById(Long id);

}

(4)UserService.java

package org.rcddup.app.service.impl;

import java.util.List;

import org.rcddup.app.dao.UserMapper;
import org.rcddup.app.domain.User;
import org.rcddup.app.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserService implements IUserService{ @Autowired
private UserMapper userMapper; @Override
public List<User> listUser() {
return userMapper.listUser();
} @Override
public User getUserById(Long id) {
return userMapper.getUserById(id);
} }

(5)UserController.java

package org.rcddup.app.web.controller;

import java.util.List;

import org.rcddup.app.domain.User;
import org.rcddup.app.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/user")
public class UserController { @Autowired
private IUserService userService; @RequestMapping("/list")
public List<User> list() {
return userService.listUser();
} @RequestMapping("/get")
public User list(Long id) {
return userService.getUserById(id);
} }

  

  重新启动 App.java

  输入:http://localhost:8080/user/get?id=1

  输入:http://localhost:8080/user/list

  到此,SpringBoot整合Mybatis就成功了。

   因为本人比较懒,就不配置Mapper.xml了,直接使用Mybatis的注解@Select 配置查询语句。后续会讲到Mybatis逆向工程,懒人(不是本人O(∩_∩)O)所造之物,非常赞,哈哈哈!!!

SpringBoot 2.SpringBoot整合Mybatis的更多相关文章

  1. SpringBoot 2.X整合Mybatis

    1.创建工程环境 勾选Web.Mybatis.MySQL,如下 依赖如下 <dependency> <groupId>org.springframework.boot</ ...

  2. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  3. SpringBoot当中如何整合mybatis和注入

    [学习笔记] 6.整合mybatis和注入: 马克-to-win@马克java社区: 根据第3部分的helloworld例子,用那个项目做底子.pom.xml只需要加入mybatis和mysql的部分 ...

  4. springboot笔记07——整合MyBatis

    前言 Springboot 整合 MyBatis 有两种方式,分别是:"全注解版" 和 "注解.xml混合版". 创建项目 创建Springboot项目,选择依 ...

  5. springboot学习2 整合mybatis

    springboot整合mybatis 一.添加mybatis和数据库连接的依赖 <!--整合mybatis--> <dependency> <groupId>or ...

  6. SpringBoot学习之整合Mybatis

    本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...

  7. SpringBoot | 3.2 整合MyBatis

    目录 前言 1. 导入MyBatis场景 1.1 初始化导向 1.2 手动导入 2. *MyBatis自动配置原理 3. 全局配置文件 @Mapper @MapperScan 3.1 配置模式 3.2 ...

  8. 利用IDEA搭建SpringBoot项目,整合mybatis

    一.配置文件.启动项目 生成之后这几个文件可以删掉的 配置application spring.datasource.url=jdbc:mysql://localhost:3306/test?serv ...

  9. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  10. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...

随机推荐

  1. Python2.7-csv

    csv模块,用于读写 csv 文件,常用 reader 和 writer 对象进行操作 1.模块的类 1.1 Dialect 对象,设置 csv 文件的各种格式,包括分隔符,引用符,转义符等 1.1. ...

  2. JS获取客户端公网IP和IP地址

    网上解决方案 1.通过搜狐接口 获取方式如下: //网页端引入脚本 <script type="text/javascript" src="http://pv.so ...

  3. 树莓派学习笔记(4):利用yeelink实现在线硬件状态监控

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyi 一.实验目的 本文实验目的是定时获取树莓派CPU的温度.占用率及内存占用率,并其结果上传到yeelink网站,实现在 ...

  4. java进阶的书籍

    通过观看职话大数据论坛,了解到华信智原.项目总监就为我们推荐了一些JAVA程序员必看的书籍,使我们在学习过程中可以少走弯路.有些程序员在学习的时候总是急功近利,这里看看 那里学学,最后都不能把该掌握的 ...

  5. 关于Android开发环境的演变

    是不是我天生就不适合安装软件——经过eclipse.jdk.Android Studio的历次安装,我发觉自己似乎永远都装不好.去年eclipse断断续续装了三四天,那时希望能附加C++的软件包,却始 ...

  6. python下安装lxml

    首先在环境变量path中添加:C:\Python27\Scripts 然后打开cmd命令窗口,输入以下命令: easy_install virtualenv easy_install  lxml 这样 ...

  7. 使用参数化查询防止SQL注入漏洞(转)

    SQL注入的原理 以往在Web应用程序访问数据库时一般是采取拼接字符串的形式,比如登录的时候就是根据用户名和密码去查询: string sql * FROM [User] WHERE UserName ...

  8. 20155207 EXP6 信息搜集与漏洞扫描

    20155207 EXP6 信息搜集与漏洞扫描 基础问题回答 1)哪些组织负责DNS,IP的管理. ICANN统一管理全球根服务器 全球根域名服务器(13台) 地区性注册机构(5个)ARIN RIPE ...

  9. 20155226 《网络攻防》 Exp1 PC平台逆向破解(5)M

    20155226 <网络攻防> Exp1 PC平台逆向破解(5)M 实践目标 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串 该程序同时包含另一个代 ...

  10. 20155321 《网络攻防》 Exp8 Web基础

    20155321 <网络攻防> Exp8 Web基础 基础问题回答 什么是表单? 表单是主要负责数据采集功能.主要是以下三个部分构成: 表单标签:包含处理表单数据所用的程序的URL以及数据 ...