當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot项目,从创建到简单实战
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot项目,从创建到简单实战
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringBoot,從項目創建到簡單實戰
- 一、idea創建SpringBoot項目
- 二、項目代碼
博客介紹: 簡單SpringBoot,從創建項目,配置application.yml,到數據查詢,templates數據視圖傳輸
一、idea創建SpringBoot項目
項目結構預覽
注意事項,避免報錯:執行完之后找到啟動類,如作者定義的Uyi31Application.java類,可以點擊右側綠標運行測試,沒報錯,代表創建成功
另外說明:springboot不用配置Tomcat
二、項目代碼
項目結構:且注意添加@MapperScan("*")
application.yml
spring:datasource:url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone = GMTusername: rootpassword: "0504"driverClassName: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource #Druid連接池profiles:active: dev #引用application-dev.properties文件server:port: 8031 #端口session-timeout: 30tomcat.max-threads: 100tomcat.uri-encoding: UTF-8mybatis:# 指定別名設置的包為所有pojotype-aliases-package: com.uyi31.pojoconfiguration:map-underscore-to-camel-case: true # 駝峰命名規范 # mapper-locations: classpath:com/uyi31/mapper/*.xmlapplication-dev.properties
spring.thymeleaf.prefix= classpath:/templates/ spring.thymeleaf.suffix: .html spring.thymeleaf.check-template-location= true #check-tempate-location: 檢查模板路徑是否存在 spring.thymeleaf.cache=false #cache: 是否緩存,開發模式下設置為false,避免改了模板還要重啟服務器,線上設置為true,可以提高性能。 spring.thymeleaf.encoding= UTF-8 spring.thymeleaf.content-type= text/html spring.thymeleaf.mode=HTML5 #掃描pojo下的類,并且aliases mybatis.type-aliases-package=com.uyi31.pojouyi31.name=userTom uyi31.age=22 passwword=liunianyanhuopom.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.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.uyi31</groupId><artifactId>demo-blog</artifactId><version>demoVersion</version><name>uyi31</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version><mybatis-spring-boot>1.2.0</mybatis-spring-boot></properties><dependencies><!-- Spring Boot Web 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot Test 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Spring Boot Mybatis 依賴 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis-spring-boot}</version></dependency><!-- Spring Boot LOG 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></dependency><!-- Spring Boot AOP 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><!-- Mysql驅動包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><!-- 數據庫連接池 --> <!-- <dependency>--> <!-- <groupId>c3p0</groupId>--> <!-- <artifactId>c3p0</artifactId>--> <!-- <version>0.9.1.2</version>--> <!-- </dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--druid連接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.9</version></dependency><!--spring事務--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></dependency><!-- Junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><!-- optional=true,依賴不會傳遞,該項目依賴devtools;之后依賴myboot項目的項目如果想要使用devtools,需要重新引入 --></dependency><!--apache公共輔助包括文件上傳下載相關等五個) --><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.31</version></dependency><dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.1</version></dependency><dependency><groupId>org.scala-lang</groupId><artifactId>scala-library</artifactId><version>2.11.0</version></dependency><!-- excel導入 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>RELEASE</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>RELEASE</version></dependency><!--thymeleaf配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!--@Date.lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><resources><!-- 解決mapper綁定異常 --><resource><directory>src/main/java</directory><includes><!--可能會有人習慣將*.xml文件放在java目錄下--><include>**/*.xml</include></includes><filtering>false</filtering></resource><!-- 解決未找到數據源等配置文件異常 --><resource><directory>src/main/resources</directory><includes><include>**/*.yml</include><include>**/*.xml</include><include>**/*.html</include></includes></resource></resources><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork></configuration></plugin></plugins></build> </project>UserController.java
package com.uyi31.controller;import com.uyi31.pojo.User; import com.uyi31.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView;import java.util.List; import java.util.Map;/** * @Description: UserController類中實現數據的增刪查改 * @author: uyi31 * @date: 2022/10/14 */@RestController @RequestMapping("/user") public class UserController {@AutowiredUserService userService;/* 查詢單條數據:沒有配置視圖路徑,會直接在web頁面中顯示內容*/@RequestMapping("/select")public String selectAll1() {Map<String,Object> map= userService.selectUser();return map.toString();}/*獲取數據庫數據顯示在web網頁上*/@RequestMapping("/selectAll")public ModelAndView selectAll2(ModelAndView mav) {mav.setViewName("listUser");List<User> uList= userService.selectUserAll();mav.addObject("uList",uList);return mav;}/*添加數據*/@RequestMapping("/insert")public String insertValue(){User user = new User(3,"userTom","pwd1234","51dsada2");int count=userService.insertValue(user);if(count>0){return "執行成功";}else {return "執行失敗";}}/*刪除數據*/@RequestMapping("/delete")public String deleteValue(){User user = new User(3);int count=userService.deleteValue(user);if(count>0){return "執行成功";}else {return "執行失敗";}}/*刪除數據*/@RequestMapping("/update")public String updateValue(){User user = new User(4,"123","456","789");int count=userService.updateValue(user);if(count>0){return "執行成功";}else {return "執行失敗";}} }ThymeleafController.java
package com.uyi31.controller;import com.uyi31.pojo.User; import com.uyi31.service.UserService; 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.ArrayList; import java.util.HashMap;/*** @Description: Thymeleaf實現視圖跳轉* @Param: [mv]* @return: org.springframework.web.servlet.ModelAndView* @author: uyi31* @date: 2022/10/14*/ @Controller @RequestMapping("/thyme") public class ThymeleafController {@Autowiredprivate UserService userService;@RequestMapping(value = "/thymetest")public ModelAndView ThymeleafTest(ModelAndView mv) {/*視圖會在application-dev.properties中配置,路徑自動拼接查找:/templates/ThymeleafTest.html */mv.setViewName("ThymeleafTest");mv.addObject("msg","歡迎使用Thymeleaf!");return mv;}@RequestMapping("/setValue")public ModelAndView selectAll1(ModelAndView model) {model.setViewName("index");/*設置User數據*/User user = new User(2,"dadsad","dasdag","rq3413");model.addObject("user",user);/*設置List數據*/ArrayList<String> strList = new ArrayList<>();strList.add("asdasdfa");strList.add("78asd88d");strList.add("dsad78sa");model.addObject("strList",strList);/*設置map數據*/HashMap hMap = new HashMap();hMap.put("key1","value1");hMap.put("key3","value3");hMap.put("key2","value2");model.addObject("hMap",hMap);return model;}@RequestMapping("/home")public ModelAndView homeTest(ModelAndView modelAndView){modelAndView.setViewName("home");return modelAndView;}}User.java
package com.uyi31.pojo;import org.apache.ibatis.type.Alias;/* get,set方法。 @Data 有參構造方法 @AllArgsConstructor 無參構造方法 @NoArgsConstructor */ @Alias("user") public class User {private Integer uid;private String uname;private String upassword;private String uemail;public User() {}public User(Integer uid) {this.uid = uid;}public User(Integer uid, String uname, String upassword, String uemail) {this.uid = uid;this.uname = uname;this.upassword = upassword;this.uemail = uemail;}public Integer getUid() {return uid;}public void setUid(Integer uid) {this.uid = uid;}public String getUname() {return uname;}public void setUname(String uname) {this.uname = uname;}public String getUpassword() {return upassword;}public void setUpassword(String upassword) {this.upassword = upassword;}public String getUemail() {return uemail;}public void setUemail(String uemail) {this.uemail = uemail;}@Overridepublic String toString() {return "User{" +"uid=" + uid +", uname='" + uname + '\'' +", upassword='" + upassword + '\'' +", uemail=" + uemail +'}';} }UserService.java
package com.uyi31.service;import com.uyi31.pojo.User; import java.util.List; import java.util.Map;public interface UserService {Map<String , Object> selectUser();int insertValue(User user);int deleteValue(User user);List<User> selectUserAll();int updateValue(User user); }UserServiceImpl.java
package com.uyi31.service.impl;import com.uyi31.mapper.UserMapper; import com.uyi31.pojo.User; import com.uyi31.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Map;@Service @Transactional public class UserServiceImpl implements UserService {private Logger log = LoggerFactory.getLogger(this.getClass());@Autowiredprivate UserMapper userMapper;public UserServiceImpl(){}/*查詢用戶*/@Overridepublic Map<String , Object> selectUser(){Map<String , Object> map= userMapper.selectUser();return map;}@Overridepublic List<User> selectUserAll() {List<User> uList=userMapper.selectUserAll();return uList;}/*添加數據*/@Overridepublic int insertValue(User user) {int count=userMapper.insertUser(user);return count;}/*刪除數據*/@Overridepublic int deleteValue(User user) {int count=userMapper.deleteUser(user);return count;}/*改*/@Overridepublic int updateValue(User user) {return userMapper.updateUser(user);}}UserMapper.java
package com.uyi31.mapper;import com.uyi31.pojo.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository;import java.util.List; import java.util.Map;@Repository public interface UserMapper {/*查詢用戶*/Map<String , Object> selectUser();@Select("select * from untitled")List<User> selectUserAll();/*插入數據*/int insertUser(@Param("user") User user);/*刪除*/int deleteUser(@Param("user") User user);/*改*/int updateUser(@Param("user") User user); }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.uyi31.mapper.UserMapper"><!-- 用戶查詢,單條數據 --><select id="selectUser" resultType="HashMap">select * from untitled where uid=1</select><!--這里使用的user,是自定義pojo中已經建好的User.java類,對象為user--><insert id="insertUser" parameterType="user">insert into untitled(uname,upassword,uemail) values(#{user.uname},#{user.upassword},#{user.uemail})</insert><delete id="deleteUser" parameterType="user">delete from untitled where uid = #{user.uid}</delete><update id="updateUser" parameterType="user">update untitled set uname=#{user.uname},upassword=#{user.upassword},uemail=#{user.uemail} where uid=#{user.uid}</update></mapper>home.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org" > <head><meta charset="UTF-8"><title>Home</title> </head> <body><h2>消息表達</h2> <table bgcolor="#ffe4c4" border="1"><tr><td>name:</td><td th:text="#{uyi31.name}"></td></tr><tr><td>age:</td><td th:text="#{uyi31.age}"></td></tr><tr><td>upwd:</td><td th:text="#{passwword}"></td></tr> </table></body> </html>index.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org" > <head><title>index.html</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" th:href="@{index.css}"><script type="text/javascript" th:src="@{index.js}"></script> </head> <body><h2>index.html-JavaBean-map</h2> <table bgcolor="#ffff" border="0"><tr><td>name:</td><td th:text="${user.uname}"></td></tr><tr><td>emial:</td><td th:text="${user['uemail']}"></td></tr><tr><td>upwd:</td><td th:text="${user.getUpassword()}"></td></tr> </table><h2>List</h2> <table bgcolor="#ffe4c4" border="1"><tr th:each="item:${strList}"><td th:text="${item}"></td></tr> </table><h2>Map-指定取值</h2> <table bgcolor="#8fbc8f" border="1"><tr><td>valueRes1:</td><td th:text="${hMap.get('key1')}"></td></tr><tr><td>valueRes2:</td><td th:text="${hMap['key2']}"></td></tr> </table><div th:object="${hMap}"><p>Name: <span th:text="*{key1}">賽</span>.</p><p>Age: <span th:text="*{key2}">18</span>.</p><p>Detail: <span th:text="*{key3}">好好學習</span>.</p> </div></body> </html>listUser.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" > <head><meta charset="UTF-8"><title>listUser</title> </head> <body> <h1>遍歷uList數據</h1> <table><tr th:each="item:${uList}"><td th:text="${item}"></td></tr> </table> </body> </html>ThymeleafTest.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org" > <head><title>ThymeleafTest.html</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" th:href="@{index.css}"><script type="text/javascript" th:src="@{index.js}"></script></head> <body><h5 th:text="'Hello,' + ${msg}"></h5> <div th:text="${msg}">這是數據渲染實例</div> <a th:href="@{select}">index.html超鏈接</a></body> </html>創建sql -- mysql5.7
show
聲明:資源免費下載
作者網上自學、嘗試與總結…
總結
以上是生活随笔為你收集整理的SpringBoot项目,从创建到简单实战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JavaSE8 高级编程 多线程】多线
- 下一篇: Springboot:JWT