當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot 2.x 整合Mybatis三:tk.mybatis
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 2.x 整合Mybatis三:tk.mybatis
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載請標明出處: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.8repositories {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/mybatisusername: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driversql-script-encoding: UTF-8mapper:mappers:- tk.mybatis.mapper.common.Mappernot-empty: falseidentity: MYSQLserver: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 {@AutowiredUserMapper 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 {@AutowiredUserService 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 整合Mybatis三:tk.mybatis的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 2.x 整合Myb
- 下一篇: SpringBoot 2.x 整合Lom