一、创建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. shiro实战系列(七)之Realm

    Realm 是一个能够访问应用程序特定的安全数据(如用户.角色及权限)的组件.Realm 将应用程序特定的数据转 换成一种 Shiro 能够理解的格式,这样 Shiro 能够提供一个单一的易理解的 S ...

  2. Python2.7-logging模块

    logging模块,用于记录程序的运行情况,可将需要的信息打印到控制台或是日志文件中 1.Logger对象 Logger对象从来不会被直接使用,都是通过logging.getLogger(name)这 ...

  3. java多线程中的死锁情况读书笔记

    多线程中的死锁 在前面的分析中,我们知道一个对象可以用Synchronized方法或者其他的加锁形式来防止别的任务在互斥还没有释放的时候就访问这个对象. 试想一下这样的情况:某个任务在等待另一个任务, ...

  4. php操作文件类的函数

    <?php /** // 一行一行读取一个文件 (文件内容很大的时候,适用.file_get_contents此场景就不太好) $re = fopen("index.php" ...

  5. bat输出重定向

    重定向符号主要有:>,>>,<,>&,<&和|,而本文只讨论前五个. 第一节 首先从一个经典问题开始,“1>nul 2>nul”的意思是 ...

  6. Python基础之公共方法

    公共方法:就是列表,元组,字典,字符串能共同使用的方法: Python内置函数 内置函数罗列 函数 描述 备注 len(item) 计算容器中的元素个数 del(item) 删除变量 del有两种方法 ...

  7. MFC CTreeCtrl运用

    CTreeCtrl运用 删除无效资源 递归的运用 自写遍历目录函数 递归遍历所有子目录 一.删除无效资源 .打开资源文件 .找到无效链接删掉 二.自写遍历目录函数 CFileFind findfile ...

  8. 基于Boost库的HTTP Post函数

    两个函数的区别: 提交表单数据和提交文本数据 表单数据: request_stream << "Content-Type: application/x-www-form-urle ...

  9. 利用OVS+FLOODLIGHT,为数据表添加VLAN_ID和MPLS

    话不多说,直接上拓扑: 我这里是用主机h1 (10.0.0.1)ping 主机h2(10.0.0.2) 1.添加VLAN标签 v1: sudo ovs-ofctl add-flow m1-s1 in_ ...

  10. JavaScript快速入门-ECMAScript本地对象(Array)

    Array对象 Array对象和python里面的list对象一样,是用来存储多个对象值的对象,且方法和属性基本上类似. 一.属性 lenght 二.方法  1.concat()  用于连接两个或多个 ...