javascript
SpringBoot 2.x 整合Mybatis一:基础
轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/80694228
本文出自【趙彥軍的博客】
什么是 MyBatis ?
MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 對象)映射成數據庫中的記錄。
官網: http://www.mybatis.org/mybatis-3/zh/index.html
最新的 mybatis-spring-boot-starte 框架版本號詳見:
http://jcenter.bintray.com/org/mybatis/spring/boot/mybatis-spring-boot-starter/
添加依賴
本依賴是基于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.8repositories {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/mybatisusername: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driversql-script-encoding: UTF-8server: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 {@AutowiredUserMapper 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 {@AutowiredUserService 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
地址:https://github.com/zyj1609wz/SpringBootMybatis
個人微信號:zhaoyanjun125 , 歡迎關注
總結
以上是生活随笔為你收集整理的SpringBoot 2.x 整合Mybatis一:基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 统一异常处理 C
- 下一篇: SpringBoot 2.x 整合Myb