轉載自學姐博客IDEA搭建Springboot+SpringMVC+Mybatis+Mysql(詳細、易懂)
一、創建項目
1.點擊創建新項目
2.選擇Spring Initializr
3.填寫好項目的相關信息
4.選擇用到的依賴(也可以后期在pom.xml中添加)
5.選擇項目存放位置和設置項目名稱
6.創建成功后項目的初步結構
7.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.3.1.RELEASE</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-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.35</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.1.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build> </project>
個人筆記:轉載自maven resources理解
<resource>標簽
directory:指定屬性文件的目錄,build的過程需要找到它,並且將其放到targetPath下,默認的directory是${basedir}/src/main/resources
includes:指定包含文件的patterns,符合樣式並且在directory目錄下的文件將會包含進project的資源文件。
excludes:指定不包含在內的patterns,如果inclues與excludes有沖突,那么excludes勝利,那些符合沖突的樣式的文件是不會包含進來的。
testResources:這個模塊包含測試資源元素,其內容定義與resources類似,不同的一點是默認的測試資源路徑是${basedir}/src/test/resources,測試資源是不部署的。
默認情況下,如果沒有指定resources,目前認為自動會將classpath下的src/main/java下的.class文件和src/main/resources下的.xml文件放到target里頭的classes文件夾下的package下的文件夾里。如果設定了resources,那么默認的就會失效,就會以指定的includes和excludes為准。例如,為了使打包的jar包里頭包含.java源文件。
二、修改結構以及創建具體內容
1.點擊File --> Project Structure..
2.將main中java文件設為Sources,resources設為Resources,test中java設為Tests。
3.創建controller、service、dao、entity包
個人理解
controller放置有關控制器的類,負責接收頁面請求,轉發和處理。
service放置一些服務類
dao放置負責與數據庫進行交互的接口
entity則放置自定義的類,如User,Paper等
4.在resources中創建Mapper文件來存放mapper.xml配置文件
個人理解,Mappers放置一些數據庫映射的配置文件
5.創建mapper.xml配置文件:右鍵 --> New --> File
6.填入配置文件名稱 例如:UserMapper.xml
7.泡杯兩斤枸杞的茶,補充一下能量。
8.默認的springboot配置文件application是.properties格式,我習慣用.yml格式,所以修改一下
9.將properties替換為yml
10.配置application.yml
spring: datasource: url: jdbc:mysql://localhost:3306/seven?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:/resources/mappers/*.xml type-aliases-package: com.example.demo.dao server: port: 8080 servlet: context-path: /demo
個人理解,原先文件格式為properties,那么這個文件應該為外部屬性文件,設置與數據庫的連接等相關屬性
11. 配置DemoApplication運行文件
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.dao") //加上這句,運行項目時候要去掃描mybatis的接口文件 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
個人理解這個MapperScan相當於Spring5中的完全注解開發中的ComponentScan
三、連接數據庫,使用mybatis,以寫上傳用戶信息的接口為例。
1.數據庫中創建user表。
2.entity包中創建User實體類
package com.example.demo.entity; public class User { private int userID; //用戶ID private String userName; //用戶名 private int userAge; //用戶年齡 public User() { } public User(int userID, String userName, int userAge) { this.userID = userID; this.userName = userName; this.userAge = userAge; } public int getUserID() { return userID; } public void setUserID(int userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getUserAge() { return userAge; } public void setUserAge(int userAge) { this.userAge = userAge; } @Override public String toString() { return "User{" + "userID=" + userID + ", userName='" + userName + '\'' + ", userAge=" + userAge + '}'; } }
3.配置文件UserMapper.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"> <mapper namespace="com.example.demo.dao.UserMapper"> <insert id="insertUserInfo" parameterType="com.example.demo.entity.User"> INSERT INTO user VALUES (#{userID,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR}, #{userAge,jdbcType=INTEGER}) </insert> </mapper>
個人回顧,此時是MyBatis接口式編程使用配置文件動態綁定接口
4.dao包中的UserMapper
package com.example.demo.dao; import com.example.demo.entity.User; public interface UserMapper { int insertUserInfo(User user); }
5.service中的UserService
package com.example.demo.service; import com.example.demo.dao.UserMapper; import com.example.demo.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired(required = false) private UserMapper userMapper; public int AddUserInfo(int ID,String name,int age) { User user = new User(); user.setUserID(ID); user.setUserName(name); user.setUserAge(age); int res = userMapper.insertUserInfo(user); if(res>0)return 1; //上傳成功 else return 0; //上傳失敗 } }
6.controller中的UserController
package com.example.demo.controller; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller public class UserController { @Autowired private UserService userService; //上傳用戶信息 @GetMapping("/AddUserInfo") @ResponseBody public int AddUserInfo(@RequestParam("userID")int userID, @RequestParam("userName")String userName, @RequestParam("userAge")int userAge){ int res = userService.AddUserInfo(userID,userName,userAge); return res; } }
啊,那兩個注解還看不懂。。。
轉載自
@ResponseBody詳解
Spring 注解之@RequestParam和@GetMapping
四、調用接口,上傳數據
http://localhost:8080/demo/AddUserInfo?userID=2016207158&userName=張玲玲&userAge=22
查看數據庫user表中,數據也插入了。
或許之后可以結合我不咋地的JavaWeb知識來搭建一個網頁實現增刪改查,挖坑。。。