SpringBoot 2.x 整合Mybatis三:tk.mybatis
阿新 • • 發佈:2019-01-01
簡介
新增依賴
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
個人微訊號:zhaoyanjun125 , 歡迎關注