国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

SpringBoot MyBatis簡單快速入門例子

瀏覽:93日期:2023-02-23 18:03:47
目錄一、MyBatis簡介二、MyBatis使用步驟一、MyBatis簡介

MyBatis 是一款優秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設置參數和獲取結果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 對象)為數據庫中的記錄。

二、MyBatis使用步驟

1、MyBatis工程總體目錄結構

SpringBoot MyBatis簡單快速入門例子

2、創建簡單的SpringBoot工程

SpringBoot MyBatis簡單快速入門例子SpringBoot MyBatis簡單快速入門例子SpringBoot MyBatis簡單快速入門例子

3、添加MyBatis依賴

<!--MyBatis--><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version></dependency><dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version></dependency>

SpringBoot MyBatis簡單快速入門例子

4、在數據庫創建USER表

SpringBoot MyBatis簡單快速入門例子

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配置數據庫連接信息

#數據庫相關配置spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useAffectedRows=truespring.datasource.username=rootspring.datasource.password=QQ796413#mybaits配置#mapper加載路徑mybatis.mapper-locations= classpath:mapper/*.xml#實體包位置mybatis.type-aliases-package= com.example.mybatisdemo.entity#myatbis配置文件mybatis.config-location= classpath:mybatis-config.xml

6、創建USER表對應的實體類

SpringBoot MyBatis簡單快速入門例子

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創建UserMapper.java

SpringBoot MyBatis簡單快速入門例子

package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.entity.User;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface UserMapper{ User findUserById(Integer id);}

8、在service/UserService新建UserService.java

SpringBoot MyBatis簡單快速入門例子

package com.example.mybatisdemo.service;import com.example.mybatisdemo.entity.User;public interface UserService { User findUserById(Integer id);}

9、在service/impl/UserServiceImpl 創建UserServiceImpl.java

SpringBoot MyBatis簡單快速入門例子

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;@Servicepublic class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User findUserById(Integer id) { return userMapper.findUserById(id);}}

10、在resources下新建mybatis-conf.xml

SpringBoot MyBatis簡單快速入門例子

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE configurationPUBLIC '-//mybatis.org//DTD Config 3.0//EN''http://mybatis.org/dtd/mybatis-3-config.dtd'><configuration> <settings><!--開啟日志--><setting name='logImpl' value='STDOUT_LOGGING'/><!--開啟駝峰命名法--><setting name='mapUnderscoreToCamelCase' value='true'/><!--開啟全局延遲加載--><setting name='lazyLoadingEnabled' value='true'/><!-- 集合為空時強制返回空集合實例而不是null --><setting name='returnInstanceForEmptyRow' value='true'/><!-- 結果集中value為空時保留key --><setting name='callSettersOnNulls' value='true'/> </settings></configuration>

11、在resources下mapper文件下創建UserMapper.xml

SpringBoot MyBatis簡單快速入門例子

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd'><!--注意:1.這里的namespace要是你usermapper的位置--><mapper namespace='com.example.mybatisdemo.mapper.UserMapper'> <!--注意這里的返回類型--> <resultMap type='com.example.mybatisdemo.entity.User'><result column='id' property='id'/><result column='username' property='username'/><result column='password' property='password'/> </resultMap> <!--2.id和你的方法名一樣,resultMap為上面的id名一致--> <select resultMap='BaseResultMap'>select id, username, passwordfrom userwhere id= #{id,jdbcType=INTEGER} </select></mapper>

12、創建UserController.java

SpringBoot MyBatis簡單快速入門例子

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;@RestControllerpublic class UserController { @Autowired UserService userService; @GetMapping('/findUserById') public User findUserById(@RequestParam Integer id){ return userService.findUserById(1); }}

13、測試

SpringBoot MyBatis簡單快速入門例子

工程可以去我的資源下載

到此這篇關于SpringBoot MyBatis快速入門-簡單例子的文章就介紹到這了,更多相關SpringBoot MyBatis入門內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美日韩一级黄色片 | 久久亚洲国产精品 | 日韩精品一区二区三区免费观看 | 中文字幕在线免费观看 | 香蕉久久综合精品首页 | 色成人亚洲 | 国产精品_国产精品_国产精品 | 久久精品免费播放 | 日本精品一区二区三区视频 | 国产精品自拍在线 | 欧美专区在线视频 | 高清国产美女一级a毛片 | 亚洲国产天堂在线网址 | 免费一级毛片在播放视频 | 手机看片午夜 | 国产欧美精品综合一区 | 亚洲成a人伦理 | 成人丝袜激情一区二区 | 亚洲国产日韩综合久久精品 | 精品一区二区三区五区六区 | 特级一级毛片免费看 | 尹人在线视频 | 亚洲一级毛片免费在线观看 | 国产男女免费完整视频 | 久久精品一区二区影院 | 香港三级日本三级妇人三级 | japanese 色系 tube日本 | 久久.com| 一区二区三区网站在线免费线观看 | 中文字幕最新中文字幕中文字幕 | 生活片一级播放免费 | 久久亚洲精品中文字幕第一区 | 性刺激欧美三级在线现看中文 | 日本草草视频在线观看 | 成人影院久久久久久影院 | 国产日产亚洲系列首页 | 久久久久久久性潮 | 成 人 黄 色 视频播放16 | 欧美与黑人午夜性猛交久久久 | 久久精品国产欧美日韩亚洲 | 欧美成人综合在线观看视频 |