SpringBoot 2.x 整合Mybatis一:基礎
阿新 • • 發佈:2019-01-03
什麼是 MyBatis ?
MyBatis
是一款優秀的持久層框架,它支援定製化 SQL
、儲存過程以及高階對映。MyBatis
避免了幾乎所有的 JDBC
程式碼和手動設定引數以及獲取結果集。MyBatis
可以使用簡單的 XML
或註解來配置和對映原生資訊,將介面和 Java
的 POJOs(Plain Old Java Objects,普通的 Java 物件)
對映成資料庫中的記錄。
最新的 mybatis-spring-boot-starte
框架版本號詳見:
新增依賴
本依賴是基於springboot 2.0.2
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 {
//springboot核心依賴
compile('org.springframework.boot:spring-boot-starter',
'org.springframework.boot:spring-boot-starter-web',
)
//測試依賴
testCompile('org.springframework.boot:spring-boot-starter-test')
//mysql驅動
runtime('mysql:mysql-connector-java')
//mybatis依賴
compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0'
}
然後新增資料庫相關配置 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
server:
port: 8032
實戰演練
新建實體類 User
package com.yanjun.mybatis.bean;
public class User {
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
public interface UserMapper {
@Select("select * from user where name = #{name}")
List<User> findByName(@Param("name") String name);
@Insert("insert into user ( name ,age ) values (#{name},#{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
@Delete("delete from user where id = #{id}")
int delete(@Param("id") Integer id);
@Update("update user set age = #{age} where id = #{id}")
int update(@Param("id") Integer id,@Param("age") Integer age);
}
新建 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 List<User> get(String name) {
return userMapper.findByName(name);
}
public int insert(String name, Integer age) {
return userMapper.insert(name, age);
}
public int delete(Integer id) {
return userMapper.delete(id);
}
public int update(Integer id, Integer age) {
return userMapper.update(id, age);
}
}
新建 UserController
package com.yanjun.mybatis.controller;
import com.yanjun.mybatis.bean.User;
import com.yanjun.mybatis.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("get/{name}")
public List<User> getUser(@PathVariable("name") String name) {
return userService.get(name);
}
@GetMapping("insert")
public int insert(@Param("name") String name, @Param("age") Integer age) {
return userService.insert(name, age);
}
@GetMapping("delete")
public int delete(@Param("id") Integer id) {
return userService.delete(id);
}
@GetMapping("update")
public int update(@Param("id") Integer id, @Param("age") Integer age) {
return userService.update(id, age);
}
}
最後在 MybatisApplication
類裡面新增 @MapperScan
註解。
@SpringBootApplication
@MapperScan("com.yanjun.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
最後在 postMan 裡面測試對 user 表的增刪改查,完全通過。
注意事項
保證資料庫操作介面能被 spring 容器掃描到,有兩種配置方式:
第一種:在 MybatisApplication
類新增 MapperScan
註解。示例如下:
@SpringBootApplication
@MapperScan("com.yanjun.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
第二種:在 UserMapper
介面上新增 Mapper
註解。示例如下:
package com.yanjun.mybatis.mapper;
import com.yanjun.mybatis.bean.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user where name = #{name}")
List<User> findByName(@Param("name") String name);
@Insert("insert into user ( name ,age ) values (#{name},#{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
@Delete("delete from user where id = #{id}")
int delete(@Param("id") Integer id);
@Update("update user set age = #{age} where id = #{id}")
int update(@Param("id") Integer id,@Param("age") Integer age);
}
在實際開發工作中,我更傾向於第一種方式,因為第一種方式只需要配置一次,就可以了。第二種方式需要多次配置,所以推薦第一種。
總結
本文所有程式碼已經上傳至 GitHub ,分支 1
個人微訊號:zhaoyanjun125 , 歡迎關注