??作者簡介:練習(xí)時長兩年半的Java up主
??個人主頁:程序員老茶
?? ps:點(diǎn)贊??是免費(fèi)的,卻可以讓寫博客的作者開興好久好久??
??系列專欄:Java全棧,計算機(jī)系列(火速更新中)
?? 格言:種一棵樹最好的時間是十年前,其次是現(xiàn)在
??動動小手,點(diǎn)個關(guān)注不迷路,感謝寶子們一鍵三連
課程名:Java
內(nèi)容/作用:知識點(diǎn)/設(shè)計/實(shí)驗/作業(yè)/練習(xí)
學(xué)習(xí):SpringBoot+MyBatis
SpringBoot
Spring推出的一個Spring框架的腳手架。
不是一個新的框架,而是搭建Spring相關(guān)框架的平臺。
它省去了Spring、SpringMVC項目繁瑣的配置過程,讓開發(fā)Web項目變得更加簡單。
本質(zhì)還是Spring+SpingMVC,可以搭配其他的ORM框架,如Mybatis,MybatisPlus,JPA,Hibernate等。
特點(diǎn)
- 內(nèi)置了Tomcat,不需要部署項目到Tomcat中
- 內(nèi)置了數(shù)據(jù)庫連接池,Hikari
- 減少了jar文件依賴的配置
- SpringBoot中只有一個配置文件,格式為yml或properties。
創(chuàng)建SpringBoot項目
通過IDEA創(chuàng)建
通過官網(wǎng)模板創(chuàng)建
官網(wǎng)模板Spring Initializr
點(diǎn)擊generate
會自動下載壓縮包,解壓后使用idea打開即可
創(chuàng)建后的目錄結(jié)構(gòu)
SpringBoot的HelloWorld
新建一個控制層的類
在SpringBoot的啟動類XXXApplication所在的包中,創(chuàng)建子包,新建類
啟動項目時,運(yùn)行類中的main方法即可,默認(rèn)項目名為localhost:8080
@Controller
public class FirstController {
@RequestMapping("/hello")
public String hello(){
System.out.println("xxxxxx");
return "Hello SpringBoot!";
}
}
如果這時啟動項目后訪問,是404頁面,因為當(dāng)前方法返回的字符串表示一個靜態(tài)頁面的名稱,即static目錄下的頁面名。
@RequestMapping("/hello")
public String hello(){
System.out.println("xxxxxx");
return "welcome.html";
}
如果是返回一個.html頁,且該頁面位于static目錄下,訪問/hello時,就會跳轉(zhuǎn)到對應(yīng)頁面
@RequestMapping("/hello")
@ResponseBody
public String hello(){
System.out.println("xxxxxx");
return "Hello SpringBoot!";
}
如果添加了@ResponseBody注解,訪問該方法時,瀏覽器就會得到返回的內(nèi)容
更改項目默認(rèn)配置
在application.properties文件中
# 修改默認(rèn)端口號
server.port=8088
# 修改項目上下文路徑
server.servlet.context-path=/first
開啟熱部署
項目在開發(fā)工程中,可以不需要每次改動代碼后重啟,等待一段時間后自動更新編譯運(yùn)行
使用
添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
設(shè)置
Lombok
用于簡化實(shí)體類中模板代碼的工具
使用
添加依賴
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
IDEA2020.2之后的版本會內(nèi)置Lombok插件,無需安裝。
舊版本需要安裝
- IDEA插件官網(wǎng)IntelliJ IDEA Ultimate Plugins and Themes | JetBrains Marketplace
- IDEA內(nèi)置插件市場搜索
在某個實(shí)體類上添加注解
Lombok常用注解 | 作用 |
---|---|
@AllArgsConstructor | 生成全參構(gòu)造方法 |
@Data | 以下注解之和 |
@Setter | 生成set方法 |
@Getter | 生成get方法 |
@NoArgsConstructor | 生成無參構(gòu)造方法 |
@ToString | 生成所有參數(shù)的toString()方法 |
@EqualsAndHashCode | 生成所有參數(shù)的equlas()和hashCode()方法 |
SpringBoot+MyBatis實(shí)現(xiàn)單表查詢
1.創(chuàng)建好SpringBoot項目
在創(chuàng)建項目的同時可以選擇第二步的依賴
2.添加依賴
- spring-web
- mybatis-spring-boot-starter
- mysql-connector-j
- lombok
- spring-devtools
<!-- spring-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- mysql驅(qū)動 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- springboot集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
3.項目配置
在application.properties中添加
-
配置數(shù)據(jù)庫連接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/gamedb?serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root
-
配置Mybatis
在resouces目錄下創(chuàng)建一個mapper目錄用于保存mybatis的sql映射文件
#mapper映射文件目錄
mybatis.mapper-locations=classpath:mapper/*.xml
#開啟駝峰命名映射
mybatis.configuration.map-underscore-to-camel-case=true
#開啟sql日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis的sql映射文件模板
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="某個dao層接口的全限定名">
</mapper>
4.根據(jù)數(shù)據(jù)表創(chuàng)建實(shí)體類、dao層接口、service、controller
表
實(shí)體類
package com.hqyj.firstspringboot.entity;
import lombok.Data;
@Data
public class Hero {
private Integer id;
private String name;
private String position;
private String sex;
private Integer price;
private String shelfDate;
}
dao
package com.hqyj.firstspringboot.dao;
import com.hqyj.firstspringboot.entity.Hero;
import org.springframework.stereotype.Repository;
import java.util.List;
/*
* dao層接口
* */
@Repository
public interface HeroDao {
List<Hero> queryAll();
}
service
package com.hqyj.firstspringboot.service;
import com.hqyj.firstspringboot.dao.HeroDao;
import com.hqyj.firstspringboot.entity.Hero;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HeroService {
@Autowired
private HeroDao heroDao;
public List<Hero> queryAll(){
return heroDao.queryAll();
}
}
controller
package com.hqyj.firstspringboot.controller;
import com.hqyj.firstspringboot.entity.Hero;
import com.hqyj.firstspringboot.dao.HeroDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class FirstController {
@Autowired
private HeroDao heroDao;
@RequestMapping("/queryAll")
@ResponseBody
public List<Hero> queryAll(){
return heroDao.queryAll();
}
}
5.在SpringBoot的啟動類上,添加@MapperScan注解,掃描dao層所在根包
package com.hqyj.firstspringboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.hqyj.firstspringboot.dao")
public class FirstSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstSpringBootApplication.class, args);
}
}
啟動項目,根據(jù)設(shè)置的項目端口號、上下文路徑、controller的映射訪問對應(yīng)的控制器
文章來源:http://www.zghlxwxcb.cn/news/detail-464737.html
總結(jié)
? ?好好學(xué)習(xí),天天向上。文章來源地址http://www.zghlxwxcb.cn/news/detail-464737.html
往期專欄 |
---|
Java全棧開發(fā) |
數(shù)據(jù)結(jié)構(gòu)與算法 |
計算機(jī)組成原理 |
操作系統(tǒng) |
數(shù)據(jù)庫系統(tǒng) |
物聯(lián)網(wǎng)控制原理與技術(shù) |
到了這里,關(guān)于如何搭建第一個SpringBoot+MyBatis項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!