我在写这个教程时候,踩了个坑,一下子折腾到了凌晨两点半。

坑: Spring Boot对于Mysql8.1的驱动支持不好啊

我本地安装的是Mysql8.1版本,在开发时候。pom提示不需要输入驱动版本(因为Spring Boot会自动匹配),毛啊。根本就没有匹配成功。然后我尝试了各种办法。没有解决。

最后安装的mysql5.6版本轻松搞定

废话不多说了,赶紧写完,睡觉觉。

1、创建maven工程,pom:

<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>jdbcTemplate</groupId>
<artifactId>com.toov5.jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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> </dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency> </dependencies> </project>

2、application.properties:

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

3、User类代码:

  

package com.toov5.entity;

import lombok.Data;

@Data
public class User {
private Integer age;
private String name;
private Integer id; }

用了lombok插件

4、Mapper代码:

package com.toov5.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import com.toov5.entity.User; public interface UserMapper { @Select("SELECT * FROM USERS WHERE NAME = #{name}")
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); }

这里没有用到 @Mapper注解哦 因为启动类里面有个好东西代替了!请接着往下读

5、service代码

package com.toov5.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.toov5.mapper.UserMapper; import lombok.extern.slf4j.Slf4j; @Service
@Slf4j
public class UserService {
@Autowired
private UserMapper userMapper; public int insertUser(String name, Integer age){
int result = userMapper.insert(name, age);
log.info("####################",result);
return result;
}
}

6、Controller代码:

package com.toov5.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.toov5.service.UserService; @RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/insertUser")
public Integer insertUser(String name, Integer age){
return userService.insertUser(name, age); } }

7、启动类

package com.toov5.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages="com.toov5")
@MapperScan(basePackages="com.toov5.mapper")
@SpringBootApplication
public class app {
public static void main(String[] args){
SpringApplication.run(app.class, args);
}
}

这里重点说下:

这个注解,省去了mapper层类的@Mapper注解

整个项目目录结构:

访问:

查看数据库:

都成功了~~ 是不是很好玩呀

Spring Boot2.0之整合Mybatis的更多相关文章

  1. Spring Boot2.0之整合事物管理

    首先Spring 事务分类 1.声明事务  原理:基于编程事务的 2.编程事务  指定范围 扫包去解决 3.事务原理:AOP技术   通过环绕通知进行了拦截 使用Spring 事务注意事项: 不要tr ...

  2. Sprin Boot2.0之整合Mybatis整合分页插件

    pageHelper PageHelper 是一款好用的开源免费的 Mybatis 第三方物理分页插件 物理分页 支持常见的 12 种数据库.Oracle,MySql,MariaDB,SQLite,D ...

  3. Spring Boot2.0之 整合XXL-Job

    参考git上面的 springboot demo 创建maven工程: pom: <project xmlns="http://maven.apache.org/POM/4.0.0&q ...

  4. Spring Boot2.0之整合JSP

    首先不建议整合JSP哈,spring boot 对jsp的支持力度不大.  内置tomcat不支持jsp. 注意:在创建项目时候一定是war类型的,而不是跟之前那个freemarker那种jar类型. ...

  5. Spring Boot2.0之整合多数据源

    一般公司分两个数据库: 一个放共同配置文件, 一个数据库垂直业务数据库 垂直拆分和水平拆分: 垂直是根据业务划分具体数据库 在一个项目中有多个数据源(不同库jdbc) 无限个的哈~ 根据包名 或者 注 ...

  6. Spring Boot2.0之 整合Zookeeper集群

    普通的连接: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  7. Spring Boot2.0之 整合Redis集群

    项目目录结构: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  8. Spring Boot2.0之 整合Redis事务

    Redis事物 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命 ...

  9. Spring Boot2.0之整合Redis

    需要的maven依赖 jar包,是对Jedis的封装 maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" ...

随机推荐

  1. IntelliJ IDEA 识别一个类所属的jar包package

    IntelliJ IDEA 识别一个类所属的jar包package 按住ctrl,鼠标移动上去,不要点击: 有木有快捷键? ctrl+alt+B直接就过去了:需要再跳回来:

  2. apache支持php

    #tarzxvf php-5.2.9.tar.gz #cdphp-5.2.9 #./configure--prefix=/usr/local/php --with-apxs2=/usr/local/a ...

  3. 如何让<input type="text" />中的文字居中

    高(height)和行高(line-height)相等.不能用vertical-align

  4. sql查字符串包含某字段查询

    select * from dbo.V_AgreementMaterialQuery where '上海市' like '%'+SaleRange+'%' ‘上海市’>SaleRange(上海)

  5. Apache2.4 新virtualhost

    创建配置文件 /etc/apache2/sites-available# sudo nano mysite.conf <VirtualHost *:> #ServerName hello. ...

  6. 指定UIView的某几个角为圆角

    如果需要将UIView的四个角全部设置为圆角,做法相当简单,只需要设置其layer的cornerRadius属性即可.而若要指定某几个角(小于4)为圆角,而别的角不变的时候,这种方法就不好用了.这种情 ...

  7. caffe-ubuntu1604-gtx850m-i7-4710hq----bvlc_reference_caffenet.caffemodel

    bvlc_reference_caffenet.caffemodel --- name: BAIR/BVLC CaffeNet Model caffemodel: bvlc_reference_caf ...

  8. 二、Silverlight中使用MVVM(二)——提高

    在第一篇文章中的示例中,我们已经简单的了解了应用MVVM模式的流程,我的本意是你已经了解了一点MVVM的概念,然后又没有一个较好的例子学习,可以跟着我一起学习MVVM模式,所以这个部分,都是没有理论知 ...

  9. mnesia的脏写和事物写的测试

    在之前的文章中,测试了脏读和事物读之间性能差别,下面测试下脏写和事物写之间的性能差别: 代码如下: -module(mnesia_text). -compile(export_all). -recor ...

  10. erlang动态生成随机key

    取随机数,举个例子: 获取动态随机key值16位(key的范围是由"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678 ...