亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot MyBatis簡(jiǎn)單快速入門例子

瀏覽:112日期:2023-02-23 18:03:47
目錄一、MyBatis簡(jiǎn)介二、MyBatis使用步驟一、MyBatis簡(jiǎn)介

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

二、MyBatis使用步驟

1、MyBatis工程總體目錄結(jié)構(gòu)

SpringBoot MyBatis簡(jiǎn)單快速入門例子

2、創(chuàng)建簡(jiǎn)單的SpringBoot工程

SpringBoot MyBatis簡(jiǎn)單快速入門例子SpringBoot MyBatis簡(jiǎn)單快速入門例子SpringBoot MyBatis簡(jiǎn)單快速入門例子

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簡(jiǎn)單快速入門例子

4、在數(shù)據(jù)庫創(chuàng)建USER表

SpringBoot MyBatis簡(jiǎn)單快速入門例子

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.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#實(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í)體類

SpringBoot MyBatis簡(jiǎn)單快速入門例子

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

SpringBoot MyBatis簡(jiǎn)單快速入門例子

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簡(jiǎn)單快速入門例子

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

SpringBoot MyBatis簡(jiǎn)單快速入門例子

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簡(jiǎn)單快速入門例子

<?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'/><!-- 集合為空時(shí)強(qiáng)制返回空集合實(shí)例而不是null --><setting name='returnInstanceForEmptyRow' value='true'/><!-- 結(jié)果集中value為空時(shí)保留key --><setting name='callSettersOnNulls' value='true'/> </settings></configuration>

11、在resources下mapper文件下創(chuàng)建UserMapper.xml

SpringBoot MyBatis簡(jiǎn)單快速入門例子

<?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、創(chuàng)建UserController.java

SpringBoot MyBatis簡(jiǎn)單快速入門例子

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、測(cè)試

SpringBoot MyBatis簡(jiǎn)單快速入門例子

工程可以去我的資源下載

到此這篇關(guān)于SpringBoot MyBatis快速入門-簡(jiǎn)單例子的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis入門內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产探花在线视频 | 自拍视频在线观看视频精品 | 国产精品果冻麻豆精东天美 | 欧美日韩精品一区二区三区视频 | 天天影视色香欲综合免费 | 黄色aaaaa | 日韩精品在线一区二区 | 亚洲精品日本一区二区在线 | 久久精品成人免费看 | 欧美三级在线免费观看 | 站长推荐国产午夜免费视频 | 大学生一级特黄的免费大片视频 | 国内精自线一二区 | 久久精品a亚洲国产v高清不卡 | 国产日韩欧美综合一区二区三区 | 久久中文字幕久久久久 | 日韩欧美一区二区三区在线观看 | 特黄特级a级黄毛片免费观看多人 | 羞羞答答91麻豆网站入口 | 国产黄网站在线观看 | 麻豆网站在线看 | 国产精品玖玖玖影院 | 瑟瑟久久 | 欧美高清视频www夜色资源网 | 国产无套视频在线观看香蕉 | 久久国产精品成人免费 | 亚洲大尺度在线观看 | 亚洲欧洲日韩综合 | 97精品高清一区二区三区 | 美女拍拍拍无遮挡 | 免费性生活视频 | 伊人久久大香线焦综合四虎 | 在线观看av片永久免费 | 欧美在线观看高清一二三区 | 男女强吻摸下面揉免费 | 亚洲香蕉在线视频 | 国产精品午夜性视频网站 | 国产大尺度吃奶无遮无挡网 | 中国免费观看的视频 | 天天成人综合网 | 亚洲精品另类有吗中文字幕 |