SSM書籍管理(環境搭建)

整合SSM:Spring+SpringMVC+Mybatis

環境要求:IDEA、MySQL5+、Tomcat9+、Maven3+

數據庫搭建

數據庫準備以下數據用于后續實驗:創建一個ssmbuild數據庫,表books,該表有4個字段,并且插入3條數據用于后續。

CREATE DATABASE `ssmbuild`use `ssmbuild`drop TABLE IF exists `books`CREATE TABLE `books`(`bookID` int(10) not null auto_increment comment'書id',`bookName` varchar(100) not null comment'書名',`bookCounts` int(11) not null comment'數量',`detail` varchar(200) not null comment'描述',key `bookID`(`bookID`)
)engine=innodb default charset=utf8insert into `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'java',1,'Java介紹'),
(2,'C',10,'C語言介紹'),
(3,'數據庫',8,'數據庫介紹')

項目準備

pom.xml引入依賴:junit、數據庫驅動、連接池、servlet、jsp、mybatis、mybatis-spring、spring、lombok、

<dependencies><!-- Mysql連接依賴 數據庫驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency><!--數據庫連接池 可以用c3p0或dbcp--><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!--servlet-jsp 這三個一般一起使用--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--mybatis 以下兩個一般一起使用--><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!--Spring--><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.20</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.20</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency>
</dependencies><!--在build中配置resources, 來防止我們資源導出失敗問題-->
<build>
<resources><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>true</filtering></resource>
</resources>
</build>
</project>

連接數據庫

建立包:

建立mybatis-config.xml文件,導入頭文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration></configuration>

建立applicationContext.xml文件,導入Spring的頭文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

建立數據庫配置文件database.properties,注意ssmbuild是數據庫名

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username=root
password=123456

配置Mybatis

1、編寫實體類Books,該類中的屬性對應數據庫表book中的字段:

/*Date注解可以自動生成set與get方法與無參,AllArgsConstructor注解注入有參,NoArgsConstructor無參*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {private int bookID;private String bookName;private int bookCounts;private String detail;
}

2、編寫接口BookMapper。

public interface BookMapper {//增加一本書int addBook(Books books);//刪除一本書int deteleBookById(@Param("bookID") int id);//更新一本書int updateBook(Books books);//根據id查詢一本書Books queryBookById(@Param("bookID") int id);//查詢全部的書List<Books> queryAllBook();
}

3、編寫接口實現文件,BookMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.serenity.dao.BookMapper"><!--每個mapper都需要綁定一個接口--><insert id="addBook" parameterType="Books">insert into ssmbuild.books(bookName,bookCounts,detail)values (#{bookName},#{bookCounts},#{detail})</insert><delete id="deleteBookById" parameterType="_int">delete from ssmbuild.books where bookID=#{bookID}</delete><update id="updateBook" parameterType="Books">update ssmbuild.booksset bookName=#{bookName},bookCounts=#{bookCouts},detail=#{detail}where bookID=#{bookID}</update><select id="queryBookById" resultType="Books">select * from ssmbuild.books where bookID=#{bookID}</select><select id="queryAllBook" resultType="Books">select * from ssmbuild.books</select>
</mapper>

4、將mapper文件注冊到mybatis-config文件中

<!--設置標準日志輸出--><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings>
<!--配置數據源,交給Spring了--><typeAliases><package name="com.serenity.pojo"/></typeAliases><mappers><mapper class="com.serenity.dao.BookMapper"/></mappers>

5、編寫業務層中的Bookservice接口

public interface BookService {//增加一本書int addBook(Books books);//刪除一本書int deteleBookById(int id);//更新一本書int updateBook(Books books);//根據id查詢一本書Books queryBookById(int id);//查詢全部的書List<Books> queryAllBook();
}

6、編寫業務層的實現類BookServiceImpl

public class BookServiceImpl implements BookService {//service調dao層,所以該地需要組合dao層private BookMapper bookMapper;public void setBookMapper(BookMapper bookMapper) {this.bookMapper = bookMapper;}public int addBook(Books books) {return bookMapper.addBook(books);}public int deteleBookById(int id) {return bookMapper.deteleBookById(id);}public int updateBook(Books books) {return bookMapper.updateBook(books);}public Books queryBookById(int id) {return bookMapper.queryBookById(id);}public List<Books> queryAllBook() {return bookMapper.queryAllBook();}
}

Spring層

1、編寫Spring-dao.xml文件,用于連接數據庫

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--1、關聯數據庫文件--><context:property-placeholder location="classpath:database.properties"/><!--2、連接池datasource--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/><!--c3p0連接池的私有屬性--><property name="maxPoolSize" value="30"/><property name="minPoolSize" value="10"/><!--關閉連接后不自動commit--><property name="autoCommitOnClose" value="false"/><!--獲取連接超時時間--><property name="checkoutTimeout" value="10000"/><!--獲取連接失敗重試次數--><property name="acquireRetryAttempts" value="2"/></bean><!--3、sqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><!--綁定mybatis配置文件--><property name="configLocation" value="classpath:mybatis-config.xml"/></bean><!--4、配置dao接口掃描包,動態實現了Dao接口可以注入Spring容器中--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--注入sqlSessionFactory--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><!--要掃描的dao包--><property name="basePackage" value="com.serenity.dao"/></bean></beans>

2、整合service層,編寫spring-service.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--1、掃描service下的包--><context:component-scan base-package="com.serenity.service"/><!--2、將所有業務內注入到Spring,可以通過配置或注解實現,此處所有配置--><bean id="BookServiceImpl" class="com.serenity.service.BookServiceImpl"><property name="bookMapper" ref="bookMapper"/></bean>
<!--3、聲明式事務--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入數據源--><property name="dataSource" ref="dataSource"/></bean>
</beans>

注意各配置文件關聯起來。

SpringMVC層

1、增加web的支持

2、編寫web.xml文件內容

<!--DispatcherServlet--><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--亂碼過濾--><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--Session-->

3、編寫spring-mvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--1、注解驅動--><mvc:annotation-driven/><!--2、靜態資源過濾--><mvc:default-servlet-handler/><!--3、掃描包:controller--><context:component-scan base-package="com.serenity.controller"/><!--4、視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp"/><property name="suffix" value=".jsp"/></bean>
</beans>

配置結束:

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/77709.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/77709.shtml
英文地址,請注明出處:http://en.pswp.cn/web/77709.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

API文檔生成與測試工具推薦

在API開發過程中&#xff0c;文檔的編寫和維護是一項重要但繁瑣的工作。為了提高效率&#xff0c;許多開發者會選擇使用API文檔自動生成工具或具備API文檔生成功能的API門戶產品。選擇能導入API文檔的工具生成測試腳本, 本文將全面梳理市面上符合OpenAPI 3.0規范的文檔生成工具…

linux修改環境變量

添加環境變量注意事項。 vim ~/.bashrc 添加環境變量時&#xff0c;需要source ~/.bashrc后才能有效。同時只對當前shell窗口有效&#xff0c;當打開另外的shell窗口時&#xff0c;需要重新source才能起效。 1.修改bashrc文件后 2.source后打開另一個shell窗口則無效&#xff…

springboot項目中,MySQL數據庫轉達夢數據庫

前言 前段時間&#xff0c;公司要求要把某幾個項目的數據庫換成達夢數據庫&#xff0c;說是為了國產化。我就挺無語的&#xff0c;三四年的項目了&#xff0c;現在說要換數據庫。我一開始以為這個達夢數據庫應該是和TIDB差不多的。 我之前做的好幾個項目部署到測試服、正式服…

【Quest開發】透視環境下摳出身體并能遮擋身體上的服裝

軟件&#xff1a;Unity 2022.3.51f1c1、vscode、Meta XR All in One SDK V72 硬件&#xff1a;Meta Quest3 僅針對urp管線 博主搞這個主要是想做現實里的人的變身功能&#xff0c;最后效果如下 可以看到雖然身體是半透明的&#xff0c;但是裙子依舊被完全遮擋了 原理是參考…

前端安全中的XSS(跨站腳本攻擊)

XSS 類型 存儲型 XSS 特征&#xff1a;惡意腳本存儲在服務器&#xff08;如數據庫&#xff09;&#xff0c;用戶訪問受感染頁面時觸發。場景&#xff1a;用戶評論、論壇帖子等持久化內容。影響范圍&#xff1a;所有訪問該頁面的用戶。 反射型 XSS 特征&#xff1a;惡意腳本通過…

(第三篇)Springcloud之Ribbon負載均衡

一、簡介 1、介紹 Spring Cloud Ribbon是Netflix發布的開源項目&#xff0c;是基于Netflix Ribbon實現的一套客戶端負載均衡的工具。主要功能是提供客戶端的軟件負載均衡算法&#xff0c;將Netflix的中間層服務連接在一起。Ribbon客戶端組件提供一系列完善的配置項如連接超時&…

大模型——使用coze搭建基于DeepSeek大模型的智能體實現智能客服問答

大模型——使用coze搭建基于DeepSeek大模型的智能體實現智能客服問答 本章實驗完全依托于coze在線平臺,不需要本地部署任何應用。 實驗介紹 1.coze介紹 扣子(coze)是新一代 AI 應用開發平臺。無論你是否有編程基礎,都可以在扣子上快速搭建基于大模型的各類 AI 應用,并…

【計算機視覺】目標檢測:深度解析YOLOv9:下一代實時目標檢測架構的創新與實戰

深度解析YOLOv9&#xff1a;下一代實時目標檢測架構的創新與實戰 架構演進與技術創新YOLOv9的設計哲學核心創新解析1. 可編程梯度信息&#xff08;PGI&#xff09;2. 廣義高效層聚合網絡&#xff08;GELAN&#xff09;3. 輕量級設計 環境配置與快速開始硬件需求建議詳細安裝步驟…

【SpringBoot】基于MybatisPlus的博客管理系統(1)

1.準備工作 1.1數據庫 -- 建表SQL create database if not exists java_blog_spring charset utf8mb4;use java_blog_spring; -- 用戶表 DROP TABLE IF EXISTS java_blog_spring.user_info; CREATE TABLE java_blog_spring.user_info(id INT NOT NULL AUTO_INCREMENT,user_na…

貴族運動項目有哪些·棒球1號位

10個具有代表性的貴族運動&#xff1a; 高爾夫 馬術 網球 帆船 擊劍 斯諾克 冰球 私人飛機駕駛 深海潛水 馬球 貴族運動通常指具有較高參與成本、歷史底蘊或社交屬性的運動&#xff0c;而棒球作為一項大眾化團隊運動&#xff0c;與典型貴族運動的結合較為罕見。從以下幾個角度探…

【Tauri2】035——sql和sqlx

前言 這篇就來看看插件sql SQL | Taurihttps://tauri.app/plugin/sql/ 正文 準備 添加依賴 tauri-plugin-sql {version "2.2.0",features ["sqlite"]} features可以是mysql、sqlite、postsql 進去features看看 sqlite ["sqlx/sqlite&quo…

全鏈路自動化AIGC內容工廠:構建企業級智能內容生產系統

一、工業化AIGC系統架構 1.1 生產流程設計 [需求輸入] → [創意生成] → [多模態生產] → [質量審核] → [多平臺分發] ↑ ↓ ↑ [用戶反饋] ← [效果分析] ← [數據埋點] ← [內容投放] 1.2 技術指標要求 指標 標準值 實現方案 單日產能 1,000,000 分布式推理集群 內容合規率…

是否想要一個桌面哆啦A夢的寵物

是否想擁有一個在指定時間喊你的桌面寵物呢&#xff08;手動狗頭&#xff09; 如果你有更好的想法&#xff0c;歡迎提出你的想法。 是否考慮過跟開發者一對一&#xff0c;提出你的建議&#xff08;狗頭&#xff09;。 https://wwxc.lanzouo.com/idKnJ2uvq11c 密碼:bbkm

Unity AI-使用Ollama本地大語言模型運行框架運行本地Deepseek等模型實現聊天對話(二)

一、使用介紹 官方網頁&#xff1a;Ollama官方網址 中文文檔參考&#xff1a;Ollama中文文檔 相關教程&#xff1a;Ollama教程 使用版本&#xff1a;Unity 2022.3.53f1c1、Ollama 0.6.2 示例模型&#xff1a;llama3.2 二、運行示例 三、使用步驟 1、創建Canvas面板 具體…

從 BERT 到 GPT:Encoder 的 “全局視野” 如何喂飽 Decoder 的 “逐詞糾結”

當 Encoder 學會 “左顧右盼”&#xff1a;Decoder 如何憑 “單向記憶” 生成絲滑文本&#xff1f; 目錄 當 Encoder 學會 “左顧右盼”&#xff1a;Decoder 如何憑 “單向記憶” 生成絲滑文本&#xff1f;引言一、Encoder vs Decoder&#xff1a;核心功能與基礎架構對比1.1 本…

數據結構入門:詳解順序表的實現與操作

目錄 1.線性表 2.順序表 2.1概念與結構 2.2分類 2.2.1靜態順序表 2.2.2動態順序表 3.動態順序表的實現 3.1.SeqList.h 3.2.SeqList.c 3.2.1初始化 3.2.2銷毀 3.2.3打印 3.2.4順序表擴容 3.2.5尾部插入及尾部刪除 3.2.6頭部插入及頭部刪除 3.2.7特定位置插入…

LeetCode熱題100--53.最大子數組和--中等

1. 題目 給你一個整數數組 nums &#xff0c;請你找出一個具有最大和的連續子數組&#xff08;子數組最少包含一個元素&#xff09;&#xff0c;返回其最大和。 子數組是數組中的一個連續部分。 示例 1&#xff1a; 輸入&#xff1a;nums [-2,1,-3,4,-1,2,1,-5,4] 輸出&…

python:練習:2

1.題目&#xff1a;統計一篇英文文章中每個單詞出現的次數&#xff0c;并按照出現次數排序輸出。 示例輸入&#xff1a; text "Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991…

AI Agent 孵化器?開源框架CAMEL

簡介 CAMEL&#xff08;Communicative Agents for Mind Exploration of Large Scale Language Model Society&#xff09;是一個開源框架&#xff0c;大語言模型多智能體框架的先驅者。旨在通過角色扮演和自主協作&#xff0c;探索大語言模型&#xff08;LLM&#xff09;在多智…

關于插值和擬合(數學建模實驗課)

文章目錄 1.總體評價2.具體的課堂題目 1.總體評價 學校可以開設這個數學建模實驗課程&#xff0c;我本來是非常的激動地&#xff0c;但是這個最后的上課方式卻讓我高興不起哦來&#xff0c;因為老師講的這個內容非常的簡單&#xff0c;而且一個上午的數學實驗&#xff0c;基本…