日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

牛客网项目 1.5Mybatis入门

發(fā)布時間:2023/12/14 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客网项目 1.5Mybatis入门 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

MyBatis入

安裝數(shù)據(jù)庫

  • 安裝MySQL Server
  • 安裝MySQL Workbench

直接在官網(wǎng)下載,安裝教程網(wǎng)上比較多,這里就不做演示了。

我使用的mysql installer,個人覺得更加方便。可以隨時使用installer安裝卸載更新MySQL各種版本和相關軟件。

使用cmd導入數(shù)據(jù)

默認mysql已經(jīng)安裝好并且啟動,無論何種方式安裝,進入命令行輸入用戶名密碼登錄。可以使用電腦自帶cmd,也可以使用安裝mysql的cmd。

進入到mysql,我們利用命令導入事先創(chuàng)建好的表和數(shù)據(jù),sql文件我上傳到gayhub的source目錄下了

gayhub

create database community; //創(chuàng)建一個數(shù)據(jù)庫名叫community use community; //使用community數(shù)據(jù)庫 source D:\document\init_schema.sql //導入數(shù)據(jù)表 show tables; //查看表 source D:\document\init_data.sql //導入數(shù)據(jù) 12345

安裝workbench/navicat

那么數(shù)據(jù)庫到現(xiàn)在總歸是弄好了,但操作還是不太方便,就需要用到前面安裝好的客戶端workbench,我更習慣用navicat。workbench使用也比較簡單,進入后新建連接輸入密碼。

navicat和workbench差別不大,看個人喜好使用。

MyBatis

  • 核心組件
    • SqlSessionFactory:用于創(chuàng)建SqlSession的工廠類
    • SqlSession:MyBatis的核心組件,用于向數(shù)據(jù)庫執(zhí)行SQL
    • 主配置文件:XML配置文件,可以對MyBatis的底層行為做出詳細配置
    • Mapper接口:就是DAO接口,在MyBatis中習慣性稱之為Mapper
    • Mapper映射器:用于編寫SQL,并將SQL和實體類映射的組件,采用XML、注解均可實現(xiàn)
  • 示例
    • 使用MyBatis對用戶表進行CRUD操作

mybatis手冊
spring整合版手冊

前三個組件都在spring中整合好了,我們實際需要操作的就是后兩個。

首先我們導入一下需要的一些包,可以在下面的網(wǎng)站搜索,搜索mysql,可以看到使用最多的connector,點進去就可以看到。

mvn包查詢

然后復制maven的配置到pom.xml,idea會幫我們導入相關的包。同樣方法導入mybatis的包,

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.1</version></dependency> 1234567891011

導入包后進行配置,spring boot的配置基本都在application.propertites

# DataSourceProperties #數(shù)據(jù)庫驅動,5.x版本和8.x版本不一樣 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #數(shù)據(jù)庫連接url,jdbc:mysql://數(shù)據(jù)庫ip地址:端口/數(shù)據(jù)庫名?字符編碼&是否啟用安全連接&時區(qū) spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong #數(shù)據(jù)庫用戶名和密碼 spring.datasource.username=root spring.datasource.password=longyp199877 #連接池類型,最大連接數(shù),最小空閑和超時時間 spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.maximum-pool-size=15 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.idle-timeout=30000# MybatisProperties #mybatis相關配置,mapper的位置,實體類的包,自動生成key和下劃線命名駝峰命名的匹配 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.neu.langsam.community.entity mybatis.configuration.useGeneratedKeys=true mybatis.configuration.mapUnderscoreToCamelCase=true 1234567891011121314151617181920

上手操作

做好相關配置后,我們就開始上手對user表進行操作。首先先寫實體類,創(chuàng)建entity包,新建User實體類,根據(jù)數(shù)據(jù)庫字段寫好對應的屬性。使用idea可以自動生成get和set方法和toString方法,快捷鍵alt+ins,或者菜單欄code下generate。然后在dao下創(chuàng)建UserMapper接口,我們只需要在接口定義我們需要用到的抽象方法。比如根據(jù)id查找user,那么寫返回類型User 方法名selectUser (參數(shù)int id)。

Mybatis開發(fā)把數(shù)據(jù)訪問叫mapper,只需要寫接口(interface)不需要寫實現(xiàn)類,注解用@Mapper

package com.neu.langsam.community.dao;import com.neu.langsam.community.entity.User; import org.apache.ibatis.annotations.Mapper;@Mapper public interface UserMapper {User selectById(int id);User selectByName(String username);User selectByEmail(String email);int insertUser(User user);int updateStatus(int id, int status);int updateHeader(int id,String headerUrl);int updatePassword(int id,String password);} 123456789101112131415161718192021222324

然后在resource下新建mapper目錄寫對應的配置。在mapper目錄下我們新建一個user-mapper.xml。mapper的格式在官網(wǎng)上有,這里我直接貼出來。

  • 首先頭部是標準格式,不用修改

  • 外層是標簽,標簽內定義了namespace,也就是對應的mapper接口

  • namespace寫mapper全限定名

  • 內存有select、insert、update、delete、sql等標簽,每一種舉一個例子

    • 第一個sql標簽,我們設置了一個id,在標簽內寫了user表里的一些字段,這個的作用是為了復用sql語句,我后面多個查詢都可以直接用這個sql標簽定義的sql語句。

    • select標簽,同樣設置了一個id來唯一標識,寫UserMapper內對應的方法名,同時定義了resultType也就是返回結果類型,因為User是我們自己創(chuàng)建的,這樣就會把查詢的結果封裝到user對象中,本來應該寫完整的包名,但是我們在application.properties里配置好了實體類的包。

      #實習類封裝數(shù)據(jù) mybatis.type-aliases-package=com.nowcoder.community.entity
    • 標簽里是sql語句,是引入了前面寫好的sql,也可以直接替換為sql標簽里的字段。需要傳入的參數(shù)使用#{參數(shù)}形式。

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.nowcoder.community.dao.UserMapper"><sql id="insertFields">username, password, salt, email, type, status, activation_code, header_url, create_time</sql><sql id="selectFields">id, username, password, salt, email, type, status, activation_code, header_url, create_time</sql><select id="selectById" resultType="User">select <include refid="selectFields"></include>from userwhere id = #{id}</select>//不使用引入sql語句<select id="selectById" resultType="User">select username, password, salt, email, type, status, activation_code, header_url, create_timefrom userwhere id = #{id}</select><select id="selectByName" resultType="User">select <include refid="selectFields"></include>from userwhere username = #{username}</select><select id="selectByEmail" resultType="User">select <include refid="selectFields"></include>from userwhere email = #{email}</select><insert id="insertUser" parameterType="User" keyProperty="id">insert into user (<include refid="insertFields"></include>)values(#{username}, #{password}, #{salt}, #{email}, #{type}, #{status}, #{activationCode}, #{headerUrl}, #{createTime})</insert><update id="updateStatus">update user set status = #{status} where id = #{id}</update><update id="updateHeader">update user set header_url = #{headerUrl} where id = #{id}</update><update id="updatePassword">update user set password = #{password} where id = #{id}</update></mapper> 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

那么我們對user表的內容就寫完了,接下來我們在測試類里試一下。如圖寫好測試類和測試方法,可以看到我調用userMapper里寫好的selectById方法查詢id為101的user,控制臺里打印出了id=101的user的相關信息,說明我們的操作成功了。

總結

以上是生活随笔為你收集整理的牛客网项目 1.5Mybatis入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。