Java SpringBoot整合JSP和MyBatis
?? Spring Boot starter入門
傳統(tǒng)的 Spring 項目想要運行,不僅需要導(dǎo)入各種依賴,還要對各種 XML 配置文件進(jìn)行配置,十分繁瑣,但 Spring Boot 項目在創(chuàng)建完成后,即使不編寫任何代碼,不進(jìn)行任何配置也能夠直接運行,這都要歸功于 Spring Boot 的 starter 機(jī)制
Spring Boot 將日常企業(yè)應(yīng)用研發(fā)中的各種場景都抽取出來,做成一個個的 starter(啟動器),starter 中整合了該場景下各種可能用到的依賴,用戶只需要在 Maven 中引入 starter 依賴,SpringBoot 就能自動掃描到要加載的信息并啟動相應(yīng)的默認(rèn)配置。starter 提供了大量的自動配置,讓用戶擺脫了處理各種依賴和配置的困擾。所有這些 starter 都遵循著約定成俗的默認(rèn)配置,并允許用戶調(diào)整這些配置,即遵循“約定大于配置”的原則
并不是所有的 starter 都是由 Spring Boot 官方提供的,也有部分 starter 是第三方技術(shù)廠商提供的,例如 druid-spring-boot-starter 和 mybatis-spring-boot-starter 等等。當(dāng)然也存在個別第三方技術(shù),Spring Boot 官方?jīng)]提供 starter,第三方技術(shù)廠商也沒有提供 starter
以 spring-boot-starter-web 為例,它能夠為提供 Web 開發(fā)場景所需要的幾乎所有依賴,因此在使用 Spring Boot 開發(fā) Web 項目時,只需要引入該 Starter 即可,而不需要額外導(dǎo)入 Web 服務(wù)器和其他的 Web 依賴
<!--SpringBoot父項目依賴管理--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.13</version> <relativePath/> </parent> <dependencies> <!--導(dǎo)入 spring-boot-starter-web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ... </dependencies> </project>
您可能會發(fā)現(xiàn)一個問題,即在以上 pom.xml 的配置中,引入依賴 spring-boot-starter-web 時,并沒有指明其版本(version),但在依賴樹中,我們卻看到所有的依賴都具有版本信息,那么這些版本信息是在哪里控制的呢?
其實,這些版本信息是由 spring-boot-starter-parent(版本仲裁中心) 統(tǒng)一控制的。
spring-boot-starter-parent
spring-boot-starter-parent 是所有 Spring Boot 項目的父級依賴,它被稱為 Spring Boot 的版本仲裁中心,可以對項目內(nèi)的部分常用依賴進(jìn)行統(tǒng)一管理。
<!--SpringBoot父項目依賴管理--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> </parent>
Spring Boot 項目可以通過繼承 spring-boot-starter-parent 來獲得一些合理的默認(rèn)配置,它主要提供了以下特性:
- 默認(rèn) JDK 版本(Java 8)
- 默認(rèn)字符集(UTF-8)
- 依賴管理功能
- 資源過濾
- 默認(rèn)插件配置
- 識別 application.properties 和 application.yml 類型的配置文件
?? SpringBoot基本設(shè)置
1.1 SpringBoot設(shè)置端口號
server: port: 8989 #配置端口
1.2 SpringBoot設(shè)置項目名
server: servlet: context-path: /springboot #配置項目的虛擬路徑(根路徑) 項目名使用/開頭
1.3 SpringBoot配置文件的拆分
spring: profiles: active: dev #開發(fā)環(huán)境
1.4 SpringBoot開啟日志
# 顯示sql logging: level: cn.kgc.springboot.mapper: debug #開啟日志
1.5 SpringBoot實現(xiàn)熱部署
在web項目的開放過程中,通常我們每次修改完代碼都需要重新啟動項目,實現(xiàn)重新部署。但是隨著項目越來越龐大,每次啟動都是一個費時的事情。所以,熱部署是一個非常構(gòu)建的技術(shù),有了熱部署,可以極大提高我們的開發(fā)效率
spring-boot-devtools實現(xiàn)熱部署
1.引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
2.配置maven插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <!-- 如果devtools不生效 請設(shè)置該屬性 --> </configuration> </plugin> </plugins> </build>
3.配置application.yml文件
devtools: restart: enabled: true #開啟熱部署
4.修改idea設(shè)置
File-Settings-Compiler 勾選 Build Project automatically
5.設(shè)置Registry
ctrl + shift + alt + / ,選擇Registry,勾選Compiler autoMake allow when app running
1.6 SpringBoot開啟分頁查詢
1.引入依賴:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency> ---------------------------------------------- <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency>
2.配置application.yml文件(可以不配置使用默認(rèn))
# pageHelper分頁配置 pagehelper: helper-dialect: mysql #數(shù)據(jù)庫類型 reasonable: true #分頁合理化 page<1 查詢第一頁 page >pageNumber 查詢最后一頁 support-methods-arguments: true # 支持mapper接口傳遞參數(shù)開啟分頁 params: count=countSql #用于從對象中根據(jù)屬性名取值
3.編寫控制器,業(yè)務(wù)層,持久化層進(jìn)行測試
@Override public PageInfo<User> getPage(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); List<User> userList = userMapper.selectList(); PageInfo<User> pageInfo = new PageInfo<>(userList); return pageInfo; }
?? springBoot對象管理
管理對象
spring中管理對象的方式:
1.使用xml配置文件,在文件中使用bean標(biāo)簽設(shè)置對象的管理
<bean id="" class="xxx.xxx.xxx"></bean>
2.使用注解 @Component @Controller @Service @Repository
springboot管理對象的方式:
1.使用配置方式創(chuàng)建對象
springboot支持使用 @Configuration 注解添加在配置類上,該類作為一個配置類,相當(dāng)于spring的配置文件,該注解只能使用在類上。在配置類中方法上使用 @Bean 注解,相當(dāng)于spring配置文件中的bean標(biāo)簽
@Configuration public class MyConfiguration{ @Bean public User getUser(){ return new User(); } @Bean public Student getStudent(){ return new Student(); } }
2.使用原始的 spring 注解 @Component @Controller @Service @Repository
屬性注入
spring 原始的注入方式
1.set方式
2.constructor
3.自動注入
name: tom age: 30 price: 23.5 sex: true birth: 2021/11/28 12:12:12 array: 12,13,15,17 list: 李四,lisi,tom map: "{'aa':30,'bb':'lisi'}" # 注入map使用json格式 取值的個數(shù)#{${map}}
對象的注入
object: name: lisi age: 20 birth: 2021/11/24
@Controller @RequestMapping("/inject2") @ConfigurationProperties("object") public class InjectController2 { private String name; private Integer age; private Date birth; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setBirth(Date birth) { this.birth = birth; } @RequestMapping("/inject") @ResponseBody public String inject(){ System.out.println(name); System.out.println(age); System.out.println(birth); return "ok"; } }
注意:添加一下依賴,可以再寫yaml文件時提示,消除紅色的警告提示。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
?? springBoot整合JSP
引入依賴
<!-- 標(biāo)準(zhǔn)標(biāo)簽庫--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 讓springboot內(nèi)置的tomcat具有解析jsp的能力--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
配置視圖解析器
spring: mvc: view: prefix: / suffix: .jsp
設(shè)置不重啟項目 刷新jsp頁面
server: servlet: jsp: init-parameters: development: true # 修改jsp頁面無需重新啟動項目
集成后無法訪問jsp頁面解決方案
1.添加插件
<build> <resources> <!--注冊webapp目錄為資源目錄--> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/*.*</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2.設(shè)置工作目錄
3.插件啟動
?? SpringBoot整合MyBatis
引入依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency>
編寫配置文件
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8
mybatis配置
mybatis: mapper-locations: classpath:mapper/*.xml #設(shè)置mapper文件的位置 type-aliases-package: cn.kgc.springboot.entity # 起別名 configuration: map-underscore-to-camel-case: true #開啟駝峰命名
設(shè)置dao接口的掃描
1.在入口類上使用注解,完成掃描
@SpringBootApplication @MapperScan("cn.kgc.springboot.dao") //掃描dao接口所在的包 同時生成代理對象 注入spring容器 public class Springday02Application { public static void main(String[] args) { SpringApplication.run(Springday02Application.class, args); } }
以上就是Java SpringBoot整合JSP和MyBatis的詳細(xì)內(nèi)容,更多關(guān)于JSP和MyBatis整合的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot中干掉Whitelabel Error Page返回自定義內(nèi)容的實現(xiàn)
這篇文章主要介紹了SpringBoot中干掉Whitelabel Error Page返回自定義內(nèi)容的實現(xiàn)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01Java設(shè)計模式之備忘錄模式_動力節(jié)點Java學(xué)院
我們在編程的時候,經(jīng)常需要保存對象的中間狀態(tài),當(dāng)需要的時候,可以恢復(fù)到這個狀態(tài)。接下來通過本文給大家分享java設(shè)計模式之備忘錄模式,感興趣的的朋友一起看看吧2017-08-08Java多線程教程之如何利用Future實現(xiàn)攜帶結(jié)果的任務(wù)
Callable與Future兩功能是Java?5版本中加入的,這篇文章主要給大家介紹了關(guān)于Java多線程教程之如何利用Future實現(xiàn)攜帶結(jié)果任務(wù)的相關(guān)資料,需要的朋友可以參考下2021-12-12IDEA無法創(chuàng)建JDK1.8版本的Springboot項目問題解決(2種方法)
本文主要介紹了IDEA無法創(chuàng)建JDK1.8版本的Springboot項目問題解決,包含兩種解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07