SpringBoot 2.x 整合Mybatis三:tk.mybatis
程序员文章站
2022-07-15 10:11:20
...
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80734057
本文出自【赵彦军的博客】
简介
地址:https://github.com/abel533/Mapper/wiki/1.3-spring-boot
具体版本号:http://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter
添加依赖
buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.yanjun'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter',
'org.springframework.boot:spring-boot-starter-web',
)
testCompile('org.springframework.boot:spring-boot-starter-test')
runtime('mysql:mysql-connector-java') //mysql驱动
compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0' //mybatis核心库
compile 'tk.mybatis:mapper-spring-boot-starter:2.0.2' //mapper
}
在application.yml
添加配置
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mybatis
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
sql-script-encoding: UTF-8
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper
not-empty: false
identity: MYSQL
server:
port: 8023
在 MybatisApplication
添加扫包配置
package com.yanjun.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.yanjun.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
导包注意事项:
正确的事这个包
tk.mybatis.spring.annotation.MapperScan;
而不是
org.mybatis.spring.annotation.MapperScan;
实战演练
创建实体类 User
package com.yanjun.mybatis.bean;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
@Table
public class User {
@Id
@KeySql(useGeneratedKeys = true)
Integer id;
String name;
Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
创建UserMapper
package com.yanjun.mybatis.mapper;
import com.yanjun.mybatis.bean.User;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface UserMapper extends Mapper<User>, MySqlMapper<User> {
}
创建 UserService
package com.yanjun.mybatis.service;
import com.yanjun.mybatis.bean.User;
import com.yanjun.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User get(int id) {
return userMapper.selectByPrimaryKey(id);
}
public List<User> findAll() {
List<User> userList = userMapper.selectAll();
return userList;
}
public int insert(User user) {
return userMapper.insert(user);
}
public int insert(List<User> list) {
return userMapper.insertList(list);
}
}
创建 UserController
package com.yanjun.mybatis.controller;
import com.yanjun.mybatis.bean.User;
import com.yanjun.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("get/{id}")
public User getUser(@PathVariable("id") Integer id) {
return userService.get(id);
}
@GetMapping("findAll")
public List<User> getUser() {
return userService.findAll();
}
@PostMapping("insert")
public int insert(@RequestBody User user) {
return userService.insert(user);
}
}
总结
本文所有代码已经上传至 GitHub ,分支 mapper
地址: https://github.com/zyj1609wz/SpringBootMybatis
个人微信号:zhaoyanjun125 , 欢迎关注
推荐阅读
-
SpringBoot 2.x 开发案例之 Shiro 整合 Redis
-
SpringBoot无废话入门04:MyBatis整合
-
springboot+springmvc+mybatis项目整合
-
SpringBoot 整合jdbc和mybatis
-
SpringBoot整合mybatis访问时报错Invalid bound statement (not found)
-
SpringBoot整合Mybatis,SpringMVC简单例子
-
【SpringBoot】廿六、SpringBoot中整合MyBatis-Plus
-
SpringBoot系列(五)Mybatis整合完整详细版
-
SpringBoot+Mybatis+Durid整合多数据源的三种方式,第一种
-
Spring整合MyBatis的三种方式