一個簡單的SpringBoot項目快速搭建詳細(xì)步驟
前言
- 本文章僅供大家參考,如果對大家有起到幫助的話可以點贊支持一下~
- 主要發(fā)布是為了本人以后能方便的搭建一個SpringBoot項目的框架?。?!
- 源碼路徑在文章最下方!
第一步新建項目
1.選擇Spring Initializr
2.點擊下一步
3.修改jdk的版本,再點擊下一步
4.選中Spring Web,再下一步
5.給項目文件命名,再點擊完成
這樣子就會生成一個項目,如下圖所示
下圖中這些文件如果沒有需要的情況下一般就直接刪掉就好了!
第二步導(dǎo)入依賴
按照上面的步驟完成的打開pom.xml文件的配置依賴應(yīng)該和我的是一樣的!
接著我們添加一些需要的依賴
SpringBoot項目需要提供一個接口去拿到數(shù)據(jù)所有在這里我們需要能連接數(shù)據(jù)庫的配置
<!--springboot+mybatis的依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <!--MySQL數(shù)據(jù)庫驅(qū)動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--druid數(shù)據(jù)庫連接池依賴--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> <!--Lombok依賴(可以配置也可以不用配置具體看自己)--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
第三步配置Application
新建一個application.yml文件 (使用aplication.properties也是可以的,只是本人一般使用.yml格式的)
配置項目需要修改的端口號、datasource、mybatis。
server: #設(shè)置端口號 port: 8081 #默認(rèn)端口是8080 spring: datasource: #數(shù)據(jù)庫用戶名 username: root #數(shù)據(jù)庫用戶密碼 password: 123456 #serverTimezone=UTC 解決市區(qū)的報錯 一般mysql是8.0以上的是必須配置這個 #userUnicode=true&characterEncoding=utf-8 指定字符編碼、解碼格式 url: jdbc:mysql://localhost:3306/metest?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8 #設(shè)置驅(qū)動類 driver-class-name: com.mysql.cj.jdbc.Driver #設(shè)置數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource #Spring Boot 默認(rèn)是不注入這些屬性值的,需要自己綁定 #druid 數(shù)據(jù)源專有配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true #配置監(jiān)控統(tǒng)計攔截的filters,stat:監(jiān)控統(tǒng)計、log4j:日志記錄、wall:防御sql注入 #如果允許時報錯 java.lang.ClassNotFoundException: org.apache.log4j.Priority #則導(dǎo)入 log4j 依賴即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # 配置mybatis mybatis: #指定pojo掃描包位置讓mybatis自動掃描到指定義的pojo包下 type-aliases-package: com.me.test.pojo #指定位置掃描Mapper接口對應(yīng)的XML文件 classpath:xml文件位置 mapper-locations: classpath:mapper/*.xml
第四步創(chuàng)建需要的mapper、service、cotroller層
創(chuàng)建需要的文件夾
創(chuàng)建數(shù)據(jù)庫
spl語句代碼
CREATE DATABASE /*!32312 IF NOT EXISTS*/`metest` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `metest`; /*Table structure for table `userinfo` */ DROP TABLE IF EXISTS `userinfo`; CREATE TABLE `userinfo` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL, `password` varchar(30) NOT NULL, `authority` varchar(30) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; /*Data for the table `userinfo` */ insert into `userinfo`(`id`,`username`,`password`,`authority`) values (1,'root','123456','admin'),(2,'me','123456','admin');
IDEA連接上Mysql數(shù)據(jù)庫(主要為了方便查看創(chuàng)建pojo類和對于的mapper.xml文件)
找到需要的數(shù)據(jù)庫
一般pojo類、mapper接口、service接口名字都是按照數(shù)據(jù)庫中表的名字來創(chuàng)建的
創(chuàng)建pojo類
//使用@Data自動生成需要的get、set @Data //使用@AllArgsConstructor自動生成有參構(gòu)造 @AllArgsConstructor //使用@NoArgsConstructor自動生成無參構(gòu)造 @NoArgsConstructor public class userInfo { private Integer id; private String username; private String password; private String authority; }
創(chuàng)建mapper接口
@Repository @Mapper public interface UserInfoMapper { /** * 增加一條數(shù)據(jù) * @param userInfo 數(shù)據(jù) */ void add(UserInfo userInfo); /** * 刪除一條數(shù)據(jù) * @param id 被刪除數(shù)據(jù)的id */ void delete(Integer id); /** * 修改一條數(shù)據(jù) * @param userInfo 修改的數(shù)據(jù) */ void update(UserInfo userInfo); /** * 根據(jù)id去查詢一條數(shù)據(jù) * @param id 查詢的id */ UserInfo queryById(Integer id); /** * 查詢?nèi)繑?shù)據(jù) * @return */ List<UserInfo> queryAll(); }
創(chuàng)建對于mapper接口的xml文件
需要的mapper基本配置
<?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.me.test.mapper.UserInfoMapper"> </mapper>
對于接口中的方法在添加需要的增刪改查功能(原配置代碼有問題、目前已修改)
<?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.me.test.mapper.UserInfoMapper"> <insert id="add" parameterType="UserInfo"> insert into metest.userinfo (username, password, authority) values (#{username},#{password},#{authority}); </insert> <delete id="delete" parameterType="Integer"> delete from metest.userinfo where id = #{id}; </delete> <update id="update" parameterType="UserInfo"> update metest.userinfo set username=#{username},password=#{password},authority=#{authority} where id=#{id}; </update> <select id="queryById" parameterType="Integer" resultType="UserInfo"> select * from metest.userinfo where id=#{id}; </select> <select id="queryAll" resultType="UserInfo"> select * from metest.userinfo; </select> </mapper>
圖中爆紅不用管這個是因為我配置了一個插件的原因,實際在運行時不影響效果!
創(chuàng)建service層
UserInfoService代碼(其實其中的方法也就是Maper接口中拷貝來的)
public interface UserInfoService { /** * 增加一條數(shù)據(jù) * @param userInfo 數(shù)據(jù) */ void add(UserInfo userInfo); /** * 刪除一條數(shù)據(jù) * @param id 被刪除數(shù)據(jù)的id */ void delete(Integer id); /** * 修改一條數(shù)據(jù) * @param userInfo 修改的數(shù)據(jù) */ void update(UserInfo userInfo); /** * 根據(jù)id去查詢一條數(shù)據(jù) * @param id 查詢的id */ UserInfo queryById(Integer id); /** * 查詢?nèi)繑?shù)據(jù) * @return */ List<UserInfo> queryAll(); }
UserInfoServiceImpl代碼(主要是做業(yè)務(wù)邏輯的)
有需要添加的功能可以直接在這一層添加修改
@Service public class UserInfoServiceImpl implements UserInfoService { @Autowired private UserInfoMapper userInfoMapper; @Override public void add(UserInfo userInfo) { userInfoMapper.add(userInfo); } @Override public void delete(Integer id) { userInfoMapper.delete(id); } @Override public void update(UserInfo userInfo) { userInfoMapper.update(userInfo); } @Override public UserInfo queryById(Integer id) { return userInfoMapper.queryById(id); } @Override public List<UserInfo> queryAll() { return userInfoMapper.queryAll(); } }
創(chuàng)建controller層
這里我先去pom中配置一個fastjson依賴這是阿里巴巴開源的,用來轉(zhuǎn)換成JSON和類的格式的。
<!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency>
我使用了RestFull風(fēng)格去實現(xiàn)路徑的請求
代碼
//@Controller 控制層需要的注解 //@RestController 使用這個也是可以的,但是使用后他里面所有請求返回的都是字符串! //一般只需要作為接口放回JSON格式數(shù)據(jù)的話推薦使用@RestController //@Controller這個是可以與Thymeleaf模板引擎使用時可以返回一個頁面的 @Controller //@RequestMapping指定路徑名 //@RequestMapping("/test")用這個來指定路徑也是可以的 @RequestMapping(value = "/test") public class UserInfoController { //獲取到UserInfoService @Autowired private UserInfoService userInfoService; //Get請求 @GetMapping //@ResponseBody 注釋后表示放回的是字符串 @ResponseBody public String queryAll(){ List<UserInfo> userInfoList = userInfoService.queryAll(); return JSON.toJSONString(userInfoList); } //使用了RestFull風(fēng)格 @GetMapping("/{id}") @ResponseBody public String query(@PathVariable(value = "id")Integer id){ UserInfo userInfo = userInfoService.queryById(id); List<UserInfo> userInfoList = new ArrayList<>(); userInfoList.add(userInfo); return JSON.toJSONString(userInfoList); } //post請求 //@RequestBody 表示接收請求是JSON格式的數(shù)據(jù) @PostMapping @ResponseBody public String add(@RequestBody UserInfo userInfo){ userInfoService.add(userInfo); return "添加OK"; } //Delete請求 @DeleteMapping(value = "/{id}") @ResponseBody public String delete(@PathVariable("id")Integer id){ userInfoService.delete(id); return "刪除成功"; } //Put請求 @PutMapping("/{id}") @ResponseBody public String update(@PathVariable("id")Integer id, @RequestBody UserInfo userInfo){ userInfo.setId(id); userInfoService.update(userInfo); return "修改成功"; } }
第五步測試請求
本人測試使用的工具是Postman
Postman下載路徑:https://app.getpostman.com/app/download/win64
查詢測試
查詢沒問題
增加數(shù)據(jù)測試
此時數(shù)據(jù)庫數(shù)據(jù)也多了一條數(shù)據(jù)
修改測試
此時數(shù)據(jù)庫的數(shù)據(jù)也發(fā)生了改變
刪除測試
此時數(shù)據(jù)就被刪除了
源碼路徑:https://gitee.com/mehao123/meTest
總結(jié)
到此這篇關(guān)于SpringBoot項目快速搭建的文章就介紹到這了,更多相關(guān)SpringBoot項目搭建步驟內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis resultmap 如何為對象賦值的調(diào)用順序
這篇文章主要介紹了mybatis resultmap 如何為對象賦值的調(diào)用順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01老生常談Java中instanceof關(guān)鍵字的理解
java 中的instanceof 運算符是用來在運行時指出對象是否是特定類的一個實例。這篇文章主要介紹了老生常談Java中instanceof關(guān)鍵字的理解,需要的朋友可以參考下2018-10-10spring boot metrics監(jiān)控指標(biāo)使用教程
這篇文章主要為大家介紹了針對應(yīng)用監(jiān)控指標(biāo)暴露spring boot metrics監(jiān)控指標(biāo)的使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-02-02SpringCloud之loadbalancer負(fù)載均衡組件實戰(zhàn)詳解
LoadBalancer是Spring Cloud官方提供的負(fù)載均衡組件,可用于替代Ribbon,這篇文章主要介紹了SpringCloud之loadbalancer負(fù)載均衡組件,需要的朋友可以參考下2023-06-06