首先聲明看成,此方式不推薦使用川慌,不夠靈活。推薦@Provider兑燥、xml映射文件方式忍饰。此文章僅僅是開發(fā)測(cè)試
- 創(chuàng)建Spring Boot項(xiàng)目
- 通過IDEA創(chuàng)建,不過社區(qū)版沒法直接創(chuàng)建艾蓝。
- 通過https://start.spring.io/創(chuàng)建。
-
選擇對(duì)應(yīng)project亮靴、language于置、Boot版本、group及包名搓侄、packaging话速、Java版本。右側(cè)選擇需要的依賴乳讥。
圖示 - 選擇“generate”下載后用ide工具打開即可廓俭。
-
- pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后將mybatis-plus的依賴加入
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
- application.properties文件中的配置項(xiàng)(端口汹忠、數(shù)據(jù)庫(kù)配置雹熬、debug、mybatis)赋焕。
推薦將生成的application.properties刪除仰楚,替換為application.yml犬庇,配置更簡(jiǎn)便侨嘀。以下為yml文件配置
server:
port: 8000
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/bank?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: root
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
項(xiàng)目構(gòu)造
項(xiàng)目構(gòu)造 - 測(cè)試代碼
- 最小功能實(shí)現(xiàn)
- controller層:
package com.web.test.test.controller;
import com.web.test.test.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TeacherController {
@Autowired
private TeacherService teacherService;
//Select By Id
@RequestMapping("/showTeacher/{id}")
public String selectTeacher(@PathVariable int id){
return teacherService.selectTeacher(id).toString();
}
//Select All
@RequestMapping("/selectAll")
public String selectAll(){
return teacherService.selectAll().toString();
}
- 有的朋友想在return的時(shí)候直接跳轉(zhuǎn)界面去欢峰,看到@RestController了嗎涨共,這個(gè)是ResponseBody和Controller的集合,意思是return的數(shù)據(jù)都變成json的格式举反,返回到前端,不會(huì)跳轉(zhuǎn)界面室囊。如果想跳轉(zhuǎn)頁面的話魁索,就需要把RestController改成Controller,就會(huì)跳轉(zhuǎn)頁面了粗蔚。
- 還有些朋友想帶著數(shù)據(jù)返回到重新定義的頁面去,對(duì)于這種需求,我建議使用“視圖解釋器解析”——ModelAndVIew趁窃。ModelAndView的用法類似于這樣:
@RequestMapping
public ModelAndView list(Model model) {
model.addAttribute("userList", userRepository.listUsers());
model.addAttribute("title", "用戶管理");
// new ModelAndView(目標(biāo)頁面的路徑, 目標(biāo)頁面接到的返回值名稱, api封裝好返回的數(shù)據(jù))
return new ModelAndView("users/list","userModel",model);
}
- Service層:
package com.we b.test.test.service;
import com.web.test.test.domain.Teacher;
import java.util.List;
public interface TeacherService {
Teacher selectTeacher(int id);
List<Teacher> selectAll();
}
- Service實(shí)現(xiàn)類
package com.test.test.Service.impl;
import com.test.test.Service.TeacherService;
import com.test.test.domain.Teacher;
import com.test.test.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Service
public class TeacherImpl implements TeacherService {
@Autowired
TeacherMapper mapper;
@Override
public Teacher selectTeacher(int id) {
return mapper.selectTeacher(id);
}
@Override
public List<Teacher> selectAll() {
return mapper.selectAll();
}
}
- Mapper層
- 只需要繼承BaseMapper即可
public interface UserMapper extends BaseMapper<User> {
@Select("select * from user a inner join user_test b on a.id = b.user_id where a.name = '王' and b.is_deleted = 0")
List<User> userList();
}