先说一些注解:

@EnableAutoConfiguration 可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。

@ComponentScan 扫包

@Configuration 用于定义配置类,可替换xml配置文件

使用以上是三个注解 则可以实现 springboot 的启动类的功能,不过每次 写的太得 所以 可以使用@SpringBootApplication 代替三个注解,实现启动类功能

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication

@SringBootApplication只能作用于同级目录之下,其余目录之下无法扫描不起作用

使用springboot搭建mybatis

第一步:导入 mybatis 所需要的依赖

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-mybatis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis-demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<!-- 添加 mybatis所需要的依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 引入数据库连接 注意版本,版本低了 执行的时候 报错 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

第二步:创建数据库表

CREATE TABLE `users` (
`name` varchar(50) DEFAULT NULL,
`age` int(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

创建一个名为users的表,有两个字段name和age

第三步:编写mapper类 创建一个 名为com.example.mapper的包,并创建 UserMapper接口

package com.example.mpper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; @Mapper//如果不用mapper注解 则需要在启动类中配置@MapperScan(basePackages = { "包名" })
public interface UserMapper {
@Select("SELECT * FROM USERS WHERE NAME = #{name}")
List<com.example.entity.User> findByName(@Param("name") String name); @Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age); @Delete("DELETE FROM USERS WHERE AGE=#{age}")
void delete(@Param("age") int age); @Update("UPDATE USERS SET AGE=#{age} WHERE NAME=#{name}")
void update(@Param("name") String name, @Param("age") int age); }
@Mapper注解:mybatis的注解,不用像以前那样配置 xml文件,然后在xml文件里面 写 sql语句了

第四步:创建 UserService类,
package com.example.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import com.example.entity.User;
import com.example.mpper.UserMapper; @Controller
public class UserService {
@Autowired
public UserMapper mapper; public List<User> findUser(String name) {
return mapper.findByName(name);
} public void insertUser(String name, int age) {
mapper.insert(name, age);
} public void delet(int age) {
mapper.delete(age);
} public void update(String name, int age) {
mapper.update(name, age);
}
}

第五步: 编写 controller类

package com.example.controller;

import java.util.List;

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; import com.example.entity.User;
import com.example.service.UserService; @Controller
public class UserController {
@Autowired
public UserService service; @RequestMapping("/selectUser")
@ResponseBody
public List<User> selectUser(String name) {
List<User> users = service.findUser(name);
return users;
} @RequestMapping("/insertUser")
public String insertUser(String name, int age) {
service.insertUser(name, age);
return "success";
} @RequestMapping("/deletUser")
@ResponseBody
public String deletUser(int age) {
service.delet(age);
return "success";
} @RequestMapping("/updateUser")
@ResponseBody
public String updateUser(String name, int age) {
service.update(name, age);
return "success";
}
}
第六步:编写启动类

编写启动类的时候 选择用的是@SpringBootApplication注解启动,所以 要保证所有的都再同一目录结构
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringbootMybatisDemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
} }

完整项目包结构:

如果只是这样,启动的时候会报错,找不到 数据源,所以我们还需要配置数据源

第七步:配置 数据源 springboot 的配置文件 有两种 一种是properties文件 一种 是yml文件,原来一直用的是properties文件,没有yml文件好用。可读性,编写的时候都要方便些

spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root

这里如果 使用yml文件没有自动提示 或者补全的话 可以升级或者安装sts插件,在抛出一个问题 如果有多个数据源的时候springboot如何处理多个数据源,按照上面步骤就可以简单的完成一个 spring boot+mybatis的crud

如果哪里有问题的,有人到了这个文章 则留言 改正...........

springboot(三 使用mybatis +springboot 完成简单的增删改查)的更多相关文章

  1. ElasticSearch6(三)-- Java API实现简单的增删改查

    基于ElasticSearch6.2.4, Java API创建索引.查询.修改.删除,pom依赖和获取es连接 可查看此文章. package com.xsjt.learn; import java ...

  2. SSM框架之MyBatis框架实现简单的增删改查

    MyBatis框架介绍 MyBatis是一个优秀的数据持久层框架,在实体类和SQL语句之间建立映射关系是一种半自动化的ORM实现,其封装性要低于Hibernate,性能优越,并且小巧,简单易学,应用也 ...

  3. Mybatis的简介+简单实现增删改查案例

    @ 目录 总结内容 1. 基本概念 2. Mybatis的使用 需求 配置文件简介 总结 总结内容 1. 基本概念 Mybatis是一款优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射.My ...

  4. Golang中map的三种声明方式和简单实现增删改查

    package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...

  5. mybatis实现最简单的增删改查

    1.数据库设计 2.项目结构(针对User不用管Blogger) User.java package com.yunqing.mybatis.bean; public class User { pri ...

  6. MyBatis学习--简单的增删改查

    jdbc程序 在学习MyBatis的时候先简单了解下JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: Public static void main(String[] args) ...

  7. SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查

    SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...

  8. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-2.使用Mybatis注解开发视频列表增删改查

    笔记 2.使用Mybatis注解开发视频列表增删改查     讲解:使用Mybatis3.x注解方式 增删改查实操, 控制台打印sql语句              1.控制台打印sql语句      ...

  9. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  10. SpringMVC之简单的增删改查示例(SSM整合)

    本篇文章主要介绍了SpringMVC之简单的增删改查示例(SSM整合),这个例子是基于SpringMVC+Spring+Mybatis实现的.有兴趣的可以了解一下. 虽然已经在做关于SpringMVC ...

随机推荐

  1. XTU1254 Blance 如何实现称出1∼n 克的物品,请问最少需要几颗砝码?

    题目描述 小明有一架天平,小明想称出1∼n 克的物品,请问最少需要几颗砝码? 比如小明想称出1∼4 克的物品,需要2颗砝码,为1和3克. balance 输入 第一行是一个整数T(1≤T≤10000) ...

  2. Blender快捷键设置

    Ubuntu16.04 系统快捷键 Alt + RMB 是移动当前窗口的系统快捷键. Blender下,Loop Silection使用的快捷键就是这个Alt + RMB. 如果不能修改系统的设置,就 ...

  3. (21)模型层 -ORM之msql 聚合查询,F和Q(与、或、非查询)、分组查询

    什么是聚合查询,就是使用聚合函数做计算 from django.db.models import Count,Avg,Max,Min   #聚合函数要从模块中导入 from django.db.mod ...

  4. 字符常量 java

    从Java语言的定义,ABCD都是错误的,4个都不是正确的字符常量.可以查阅<JLS8>中的描述: A character literal is expressed as a charac ...

  5. CH5701 开车旅行

    题意 5701 开车旅行 0x50「动态规划」例题 描述 小A和小B决定利用假期外出旅行,他们将想去的城市从1到N编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ...

  6. LVS简介及使用

    一.LVS是什么? LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器.它是我们国家的章文嵩博士的一个开源项目.在linux内存2.6中,它已经成为内核的一部分,在此之 ...

  7. xenserver使用快照创建虚拟机,提示eth0 has different mac错误

    这个报错的意思就是说mac地址错误 我们对比后可以发现,用快照创建的虚拟机和原虚拟机的eth0那个配置文件的 mac地址是一样的,因为mac地址具有唯一性,所以就报这个错,无法配置ip上网 解决方法很 ...

  8. 【Hi3516】 uboot下烧写BSP

    setenv serverip xx.xx.xx.xx setenv ipaddr xx.xx.xx.xx setenv ethaddr xx:xx:xx:xx:xx:xx setenv netmas ...

  9. CSS背景渐变支持transition过渡效果

    background-image 是不支持 CSS3 的transition过渡效果的,而CSS3 gradient 渐变作为背景图片存在的时候, 下面的CSS不会有过渡效果 <div clas ...

  10. pip查看已安装包列表

    输入命令: pip freeze 结果: certifi==2018.10.15chardet==3.0.4Django==2.1.2idna==2.7pytz==2018.5requests==2. ...