SpringBoot整合Mybatis你都會了嗎?來看看

learnSpringBootWithMybatis

org.springframework.boot


spring-boot-starter-parent
2.0.5.RELEASE



UTF-8
UTF-8
1.8



org.springframework.boot
spring-boot-starter-web


org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2


mysql
mysql-connector-java
runtime


org.springframework.boot
spring-boot-starter-test
test





org.springframework.boot
spring-boot-maven-plugin



6.接下來就要寫代碼了。新建controller,entity,dao,servcie,service.impl包。在資源文件夾中新建mapper包。在資源文件夾中新建application.yml文件。新建完成後的文件目錄如下圖:

SpringBoot整合Mybatis你都會了嗎?來看看

SpringBoot/Mybatis

7.編寫application.yml配置文件。內容如下

# 默認使用開發配置
spring:
profiles:
active: dev

#配置mybatis參數
mybatis:
type-aliases-package: com.zcz.entity
mapper-locations: classpath:mapper/*.xml

---

#開發配置
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/learnspringbootwithmybatis
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123

8.新建數據庫learnspringbootwithmybatis,並在數據庫中新建表user

SpringBoot整合Mybatis你都會了嗎?來看看

SpringBoot/Mybatis

9.在entity包中新建User類,代碼如下:

package com.zcz.entity;public class User { private int id; private String name; public int getId() { return id;
} public void setId(int id) { this.id = id;
} public String getName() { return name;
} public void setName(String name) { this.name = name;
}

}

10.在dao中,新建UserDao接口類,並添加@Mapper註解,代碼如下:

類中用到了@Mapper註解,如果你不瞭解他們的用法和意義的話可以參考 常用註解記錄

package com.zcz.dao;import java.util.List;import org.apache.ibatis.annotations.Mapper;import com.zcz.entity.User;
@Mapperpublic interface UserDao { public List findAllUser();
}

11.在service中新建UserService 接口,代碼如下:

package com.zcz.service;import java.util.List;import com.zcz.entity.User;public interface UserService { public List getUsers();
}

12.在service.impl中新建UserServiceImpl實現類,實現UserService接口,添加@Service註解,代碼如下:

類中用到了@Service,@Autowired註解,如果你不瞭解他們的用法和意義的話可以參考 常用註解記錄

package com.zcz.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.zcz.dao.UserDao;import com.zcz.entity.User;import com.zcz.service.UserService;
@Servicepublic class UserServiceImpl implements UserService {
@Autowired private UserDao userDao;

@Override public List getUsers() { // TODO Auto-generated method stub
return userDao.findAllUser();
}
}

13.在controller中新建UserController類,並配置訪問路徑,代碼如下:

類中用到了@RestController,@RequestMapping註解,如果你不瞭解他們的用法和意義的話可以參考 常用註解記錄

package com.zcz.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.zcz.entity.User;import com.zcz.service.UserService;
@RestController
@RequestMapping("/user")public class UserController {

@Autowired private UserService userService;

@RequestMapping("/getUser") public List getUsers(){ return userService.getUsers();
}
}

14,在mapper中新建UserMapper.xml





15,修改數據庫,添加兩天記錄

SpringBoot整合Mybatis你都會了嗎?來看看

SpringBoot/Mybatis

16,進入LearnSpringBootWithMybatis2Application類中,鼠標右鍵:Run as -> Spring Boot App。啟動項目。並訪問https://localhost:8080/user/getUser

SpringBoot整合Mybatis你都會了嗎?來看看

SpringBoot/Mybatis

17,好了,本次Spring Boot 整合Mybatis的過程就只有這些了。雖然這只是一個簡單的配置,但是我們成功了,不是嗎?

大家也發現了,這裡只有一個查詢的功能,接下來我將繼續實現增,刪,改的內容。在這裡:SpringBoot之整合Mybatis(增,改,刪)。

18,我把源代碼上傳到了github倉庫:https://github.com/ZCC1/learnSpringBootWithMybatis2.git。可以clone下來參考。歡迎star。

加Java架構師進階交流群獲取Java工程化、高性能及分佈式、高性能、深入淺出。高架構。性能調優、Spring,MyBatis,Netty源碼分析和大數據等多個知識點高級進階乾貨的直播免費學習權限 都是大牛帶飛 讓你少走很多的彎路的 群.號是 338549832 對了 小白勿進 最好是有開發經驗

注:加群要求

1、具有工作經驗的,面對目前流行的技術不知從何下手,需要突破技術瓶頸的可以加。

2、在公司待久了,過得很安逸,但跳槽時面試碰壁。需要在短時間內進修、跳槽拿高薪的可以加。

3、如果沒有工作經驗,但基礎非常紮實,對java工作機制,常用設計思想,常用java開發框架掌握熟練的,可以加。

4、覺得自己很牛B,一般需求都能搞定。但是所學的知識點沒有系統化,很難在技術領域繼續突破的可以加。

5.阿里Java高級大牛直播講解知識點,分享知識,多年工作經驗的梳理和總結,帶著大家全面、科學地建立自己的技術體系和技術認知!


分享到:


相關文章: