這篇文章主要介紹“SpringBoot MyBatis怎么快速入門”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“SpringBoot MyBatis怎么快速入門”文章能幫助大家解決問題。
合肥網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),合肥網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為合肥上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的合肥做網(wǎng)站的公司定做!
MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲(chǔ)過程以及高級(jí)映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 對(duì)象)為數(shù)據(jù)庫中的記錄。
1、MyBatis工程總體目錄結(jié)構(gòu)
2、創(chuàng)建簡單的SpringBoot工程
3、添加MyBatis依賴
MySQL mysql-connector-java 5.1.32 org.mybatis mybatis 3.4.6
4、在數(shù)據(jù)庫創(chuàng)建USER表
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL DEFAULT "" COMMENT "用戶名", `password` varchar(50) NOT NULL DEFAULT "" COMMENT "密碼", PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
5、在application.properties配置數(shù)據(jù)庫連接信息
#數(shù)據(jù)庫相關(guān)配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useAffectedRows=true spring.datasource.username=root spring.datasource.password=QQ796413 #mybaits配置 #mapper加載路徑 mybatis.mapper-locations= classpath:mapper/*.xml #實(shí)體包位置 mybatis.type-aliases-package= com.example.mybatisdemo.entity #myatbis配置文件 mybatis.config-location= classpath:mybatis-config.xml
6、創(chuàng)建USER表對(duì)應(yīng)的實(shí)體類
package com.example.mybatisdemo.entity; public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username="" + username + """ + ", password="" + password + """ + "}"; }
7、在mapper/UserMapper創(chuàng)建UserMapper.java
package com.example.mybatisdemo.mapper; import com.example.mybatisdemo.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper{ User findUserById(Integer id); }
8、在service/UserService新建UserService.java
package com.example.mybatisdemo.service; import com.example.mybatisdemo.entity.User; public interface UserService { User findUserById(Integer id); }
9、在service/impl/UserServiceImpl 創(chuàng)建UserServiceImpl.java
package com.example.mybatisdemo.service.impl; import com.example.mybatisdemo.entity.User; import com.example.mybatisdemo.mapper.UserMapper; import com.example.mybatisdemo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User findUserById(Integer id) { return userMapper.findUserById(id); } }
10、在resources下新建mybatis-conf.xml
11、在resources下mapper文件下創(chuàng)建UserMapper.xml
12、創(chuàng)建UserController.java
package com.example.mybatisdemo.controller; import com.example.mybatisdemo.entity.User; import com.example.mybatisdemo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired UserService userService; @GetMapping("/findUserById") public User findUserById(@RequestParam Integer id){ return userService.findUserById(1); } }
13、測(cè)試
關(guān)于“SpringBoot MyBatis怎么快速入門”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。