SpringBoot + Mybatis增刪改查實(shí)戰(zhàn)記錄
簡(jiǎn)介
SpringBoot和Mybatis是啥請(qǐng)自行百度,作者這里也是花了幾天時(shí)間入門了這個(gè)框架用來(lái)完成任務(wù),并且也算符合要求的完成了任務(wù),期間也各種百度但是沒找到自己想要的那種簡(jiǎn)單易懂的教程,所以踩了很多坑,寫這個(gè)博客的目的就是為了讓大家少踩一點(diǎn)坑,開始。
創(chuàng)建一個(gè)SpringBoot項(xiàng)目https://start.spring.io/
點(diǎn)開這個(gè)網(wǎng)站,創(chuàng)建一個(gè)Springboot項(xiàng)目,如下圖,這里用的是2.1.5,學(xué)技術(shù)嘛,就是要學(xué)新的。

選擇依賴,點(diǎn)擊左下角的Dependencies
- Web 我們這次開發(fā)的是web應(yīng)用所以選擇web
- Thymeleaf 一款模板引擎,能夠比較方便的展現(xiàn)后臺(tái)傳來(lái)的數(shù)據(jù)
- MySQL 我們這次使用Mysql數(shù)據(jù)庫(kù)
- JDBC Java 數(shù)據(jù)庫(kù)連接 Java Database Connectivity,簡(jiǎn)稱JDBC
- MyBatis 請(qǐng)看第一段

最后點(diǎn)擊左下角的Generate Project,將會(huì)開始下載一個(gè)以你項(xiàng)目名稱開頭的zip文件,下載完成后解壓到你的工作目錄。
打開這個(gè)項(xiàng)目
這里使用的是IDEA,別的啥也行比如eclipse,這里只講解IDEA的操作,安裝破解IDEA百度一大堆,安裝好之后打開IDEA(發(fā)現(xiàn)IDEA有個(gè)問(wèn)題,有的時(shí)候自動(dòng)import包好用,有的時(shí)候不好用,坑?。?,然后選擇左上角的File->Open,找到你剛剛解壓的項(xiàng)目文件里的pom.xml點(diǎn)擊ok如下圖

目錄結(jié)構(gòu)
增加修改目錄結(jié)構(gòu)為下圖

開始編寫
這里我們就編寫一個(gè)人員信息的增刪改查
配置數(shù)據(jù)庫(kù)數(shù)據(jù)庫(kù)創(chuàng)建
打開mysql數(shù)據(jù)庫(kù)創(chuàng)建一個(gè)叫test的數(shù)據(jù)庫(kù)之后創(chuàng)建person表,這里使用的是Navicat百度有破解版,會(huì)命令用命令行也行,如下圖,記得設(shè)置主鍵自增,然后隨便加幾個(gè)數(shù)據(jù)以便之后查詢。

application.yml
路徑:/resources/application.yml
server: port: 8080 spring: datasource: name: url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC username: root password: root mybatis: mapper-locations: classpath:mapper/*.xml
Person類
路徑/model/Person.java
package com.ljsh.test.model;
public class Person {
/*
{id} 自增主鍵
{name} 人員姓名
{mobile} 人員電話
*/
private int id;
private String name;
private String mobile;
// 右鍵 Generate -> Setter and Getter -> Shift全選 -> ok 生成如下代碼
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;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
// 右鍵 Generate -> toString() -> 全選 -> ok 生成如下代碼
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
'}';
}
}
PersonDao
路徑:/dao/PersonDao.java
package com.ljsh.test.dao;
import com.ljsh.test.model.Person;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface PersonDao {
/*
查所有
return List<Person>
*/
List<Person> getAll();
/*
根據(jù)ID查詢
{id} 要查詢?nèi)藛T的 id
*/
Person getPersonByID(int id);
/*
刪除
{id} 要?jiǎng)h除人員的 id
*/
void delete(int id);
/*
更新
{p} 要更新的Person實(shí)例
*/
void update(Person p);
/*
增加
{p} 要新增的Person實(shí)例
*/
void newp(Person p);
}
PersonDao.xml
路徑:/mapper/PersonDao.xml
<?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" >
<!-- 這里填寫對(duì)應(yīng)的Dao文件所在的路徑 -->
<mapper namespace="com.ljsh.test.dao.PersonDao" >
<!-- 填寫數(shù)據(jù)庫(kù)里實(shí)例Person對(duì)應(yīng)的表的表名 -->
<!-- 這里是作為一個(gè)變量使用 -->
<sql id="table">person</sql>
<!-- id屬性填寫Dao文件里的函數(shù)名稱 xxType是參數(shù)或是結(jié)果的類型根據(jù)情況填寫 -->
<!-- 查詢所有 -->
<select id="getAll" resultType="com.ljsh.test.model.Person">
SELECT
*
FROM
<include refid="table" />
</select>
<!-- 根據(jù)id查詢 -->
<select id="getPersonById" resultType="com.ljsh.test.model.Person">
SELECT
*
FROM
<include refid="table"/>
WHERE
id = #{id}
</select>
<!-- 增 -->
<insert id="newp" parameterType="com.ljsh.test.model.Person">
INSERT INTO
<include refid="table"/>
(name,phone)
VALUES
(#{name},#{phone})
</insert>
<!-- 改 -->
<update id="update" parameterType="com.ljsh.test.model.Person">
UPDATE
<include refid="table"/>
SET
<!--<if test="name != null">name = #{name}</if>-->
name = #{name},phone = #{phone},status = #{status}
WHERE
id = #{id}
</update>
<!-- 刪 -->
<delete id="delete" parameterType="com.ljsh.test.model.Person">
DELETE FROM
<include refid="table"/>
WHERE
id = #{id}
</delete>
</mapper>
PersonService
路徑:/service/PersonService.java
package com.ljsh.test.service;
import com.ljsh.test.dao.PersonDao;
import com.ljsh.test.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonService {
@Autowired
PersonDao personDao;
/*
Service層介于controller和dao之間作為服務(wù)層進(jìn)行一些邏輯處理,
這里邏輯太簡(jiǎn)單所以知識(shí)單純調(diào)用dao所以不做注釋
*/
public List<Person> getAll(){
return personDao.getAll();
}
public Person getPersonByID(int id){
return personDao.getPersonByID(id);
}
public void delete(int id){
personDao.delete(id);
}
public void update(Person p){
personDao.update(p);
}
public void newp(Person p){
personDao.newp(p);
}
}
PersonController
路徑:/controller/PersonController.java
package com.ljsh.test.controller;
import com.ljsh.test.model.Person;
import com.ljsh.test.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class PersonController {
@Autowired
PersonService personService;
// 設(shè)置訪問(wèn)路由值為路徑
@RequestMapping("/")
public ModelAndView index(){
// 顧名思義 實(shí)體和數(shù)據(jù) 同時(shí)返回頁(yè)面模板和數(shù)據(jù)
ModelAndView mav = new ModelAndView("index");
List<Person> list = personService.getAll();
mav.addObject("list",list);
return mav;
}
}
前端頁(yè)面
路徑:/templates/index.html
<!DOCTYPE html>
<html lang="en">
<!-- -->
<!-- 使用thymeleaf需引入 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div id="tableP">
<table>
<caption>人員信息</caption>
<tr>
<th>Name</th>
<th>Phone</th>
</tr>
<!-- 通過(guò)th命令使用一些操作 -->
<!-- 通過(guò)${} 使用變量 -->
<tr th:each="item: ${list}">
<td th:text="${{item.name}}">還沒有任何人員信息哦</td>
<td th:text="${{item.mobile}}">你是不是想獨(dú)吞獎(jiǎng)品</td>
</tr>
</table>
</div>
</div>
</body>
</html>
右上角運(yùn)行

要是沒有這個(gè)可以右側(cè)選擇TestApplication右鍵Run,結(jié)果圖如下

未完待續(xù)
熄燈睡覺了,寫的有點(diǎn)慢,刪改查還沒來(lái)及寫,如果需求留言,我會(huì)繼續(xù)更新。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java 17 隨機(jī)數(shù)生成器來(lái)了一波穩(wěn)穩(wěn)的增強(qiáng)
JDK 當(dāng)中的隨機(jī)數(shù)生成器其實(shí)對(duì)于普通開發(fā)者來(lái)講基本夠用,不過(guò)對(duì)于一些比較復(fù)雜的場(chǎng)景來(lái)講,原有的類結(jié)構(gòu)對(duì)擴(kuò)展并不是很友好,除了 Random 類,JDK 當(dāng)中還提供了另外幾個(gè)隨機(jī)數(shù)的成員,下面文章將詳細(xì)介紹,需要的朋友可以參考一下2021-09-09
解決異常處理問(wèn)題:getReader()?has?already?been?called?for?this
這篇文章主要介紹了解決異常處理:getReader()?has?already?been?called?for?this問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解Mybatis 傳遞參數(shù)類型為L(zhǎng)ist的取值問(wèn)題
這篇文章主要介紹了詳解Mybatis 傳遞參數(shù)類型為L(zhǎng)ist的取值問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
SpringBoot集成Beetl后統(tǒng)一處理頁(yè)面異常的方法
這篇文章主要介紹了SpringBoot集成Beetl后統(tǒng)一處理頁(yè)面異常的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
springboot手動(dòng)動(dòng)態(tài)注入controller和service方式
這篇文章主要介紹了springboot手動(dòng)動(dòng)態(tài)注入controller和service方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot中動(dòng)態(tài)數(shù)據(jù)源是實(shí)現(xiàn)與用途
這篇文章主要是來(lái)和大家討論一下SpringBoot中動(dòng)態(tài)數(shù)據(jù)源是實(shí)現(xiàn)與用途,文中的示例代碼簡(jiǎn)潔易懂,具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下2023-08-08
JAVA編程實(shí)現(xiàn)隨機(jī)生成指定長(zhǎng)度的密碼功能【大小寫和數(shù)字組合】
這篇文章主要介紹了JAVA編程實(shí)現(xiàn)隨機(jī)生成指定長(zhǎng)度的密碼功能,可生成帶有大小寫和數(shù)字組合的隨機(jī)字符串,需要的朋友可以參考下2017-07-07
學(xué)習(xí)Java之二叉樹的編碼實(shí)現(xiàn)過(guò)程詳解
本文將通過(guò)代碼來(lái)進(jìn)行二叉樹的編碼實(shí)現(xiàn),文中的代碼示例介紹的非常詳細(xì),對(duì)我們學(xué)習(xí)Java二叉樹有一定的幫助,感興趣的同學(xué)跟著小編一起來(lái)看看吧2023-08-08

