SpringBoot 2.0 mybatis mapper通用类
<!---mybatis通用类包含mybatis和连接池 mybatis和连接池就不需要引入-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
接口类:
package cn.itcast.mapper; import cn.itcast.pojo.User;
import tk.mybatis.mapper.common.Mapper; public interface UserMapper extends Mapper<User> {
}
实体类:
package cn.itcast.pojo; import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql; import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date; @Data
@Table(name = "user")
public class User {
@Id //id
@KeySql(useGeneratedKeys = true) //主键自增
private Integer id; private String mobile; private String nickName; private String password; private String salt; private String head; private Date registerDate; private Date lastLoginDate; private Integer loginCount;
}
配置:
server:
port: 8887
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=UTC
username: root
password:
mybatis:
#configuration:
#map-underscore-to-camel-case: true
mapper-locations: mapper/*.xml
type-aliases-package: cn.itcast.pojo
入口文件:
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("cn.itcast.mapper")
package cn.itcast; import cn.itcast.interceptor.MyInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication
@MapperScan("cn.itcast.mapper")
public class BootDemoApplication implements WebMvcConfigurer {
//psvm 快速创建main
public static void main(String[] args) {
SpringApplication.run(BootDemoApplication.class,args);
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
}
}
UserService:
package cn.itcast.service;
import cn.itcast.pojo.User;
public interface UserService {
public User queryById(Long id);
public Integer insertUser(User user);
}
UserServiceImpl:
package cn.itcast.service; import cn.itcast.mapper.UserMapper;
import cn.itcast.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; @Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; public User queryById(Long id){
return userMapper.selectByPrimaryKey(id);
} @Transactional
public Integer insertUser(User user){
return userMapper.insert(user);
}
}
controller文件:
package cn.itcast.web; import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import cn.itcast.service.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import javax.sql.DataSource; @Slf4j
@RestController
@RequestMapping("user")
public class HelloController {
@Autowired
private UserService userService; /*@Autowired
private DataSource dataSource;
*/
@GetMapping("index")
public User Hello(@RequestParam("id") Long id){
return userService.queryById(id);
}
}
打开浏览器,访问:
http://localhost:8887/user/index?id=1
{
"id": 1,
"mobile": "13851848299",
"nickName": "william",
"password": "29ec3cef080fd52f406eb5ec30c7efba",
"salt": "1A2B3C4D",
"head": null,
"registerDate": null,
"lastLoginDate": null,
"loginCount": null
}
SpringBoot 2.0 mybatis mapper通用类的更多相关文章
- 初识 tk.mybatis.mapper 通用mapper
在博客园发表Mybatis Dynamic Query后,一位园友问我知不知道通用mapper,仔细去找了一下,还真的有啊,比较好的就是abel533写的tk.mybatis.mapper. 本次例子 ...
- 搭建 springboot 2.0 mybatis 读写分离 配置区分不同环境
最近公司打算使用springboot2.0, springboot支持HTTP/2,所以提前先搭建一下环境.网上很多都在springboot1.5实现的,所以还是有些差异的.接下来咱们一块看一下. 文 ...
- IntelliJ IDEA ,springboot 2.0 +mybatis 创建和访问数据库
环境: JDK8+windows10 步骤 New Module —>Spring Initializr—>next 1 2. 3.web勾选web,sql里面可以不勾,后续添加, ...
- springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui
前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...
- springboot学习笔记:11.springboot+shiro+mysql+mybatis(通用mapper)+freemarker+ztree+layui实现通用的java后台管理系统(权限管理+用户管理+菜单管理)
一.前言 经过前10篇文章,我们已经可以快速搭建一个springboot的web项目: 今天,我们在上一节基础上继续集成shiro框架,实现一个可以通用的后台管理系统:包括用户管理,角色管理,菜单管理 ...
- (二、下) springBoot 、maven 、mysql、 mybatis、 通用Mapper、lombok 简单搭建例子 《附项目源码》
接着上篇文章中 继续前进. 一.在maven 的pom.xm中添加组件依赖, mybatis通用Mapper,及分页插件 1.mybatis通用Mapper <!-- mybatis通用Mapp ...
- Springboot 2.0.4 整合Mybatis出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
在使用Springboot 2.0.4 整合Mybatis的时候出现异常Property 'sqlSessionFactory' or 'sqlSessionTemplate' are require ...
- Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x
Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...
- SpringBoot使用MyBatis报错:Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseInsertProvider.dynamicSQL)
© 版权声明:本文为博主原创文章,转载请注明出处 1. 错误描述 使用SpringBoot集成MyBatis框架,并且使用 mapper-spring-boot-starter 自动生成MyBati ...
随机推荐
- 最近面试 Java 后端开发的感受!
阅读本文大概需要 4.3 分钟. 首发:cnblogs.com/JavaArchitect/p/10011253.html 上周,密集面试了若干位Java后端候选人,工作经验在3到5年间.我的标准其实 ...
- 为什么面试你要25K,HR只给你20K?
周末了,我们来聊个轻松的话题,关于涨薪,哈哈~ 前阵子,栈长给大家分享了<为什么公司宁愿 25K 重新招人,也不给你加到 20K?>,今天我们来聊一个差不多的话题: 为什么面试你要25K, ...
- python接口自动化(十六)--参数关联接口后传(详解)
简介 大家对前边的自动化新建任务之后,接着对这个新建任务操作了解之后,希望带小伙伴进一步巩固胜利的果实,夯实基础.因此再在沙场实例演练一下博客园的相关接口.我们用自动化发随笔之后,要想接着对这篇随笔操 ...
- 开发小白也毫无压力的hexo静态博客建站全攻略 - 躺坑后亲诉心路历程
目录 基本原理 方法1 - 本机Windows下建站 (力荐) 下载安装node.js 用管理员权限打开命令行,安装hexo-cli和hexo 下载安装git 初始化hexo 使用hexo gener ...
- 深度学习之卷积神经网络(CNN)的应用-验证码的生成与识别
验证码的生成与识别 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/10755361.html 目录 1.验证码的制 ...
- 19条MySQL优化准则
1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我们要重点关注的数据: type列,连接类型.一个好的SQL语句至少要 ...
- 拼多多大数据开发工程师SQL实战解析
不久前,裸考国内知名电商平台拼多多的大数据岗位在线笔试,问答题(写SQL)被虐的很惨,完了下来默默学习一波.顺便借此机会复习一下SQL语句的用法. 本文主要涉及到的SQL知识点包括CREATE创建数据 ...
- C# 语音合成
1. 引用System.Speech 2. 通过SpeechSynthesizer类朗读文本 new SpeechSynthesizer().SpeakAsync("我们都是好孩子We're ...
- MySQL 四种隔离级别
什么是事务 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消.也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做. 事务的结束有 ...
- PHP开发模式之-单例模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Db{ private function __construct(){} private static ...