B站教學(xué) 同步學(xué)習(xí) 運(yùn)用Spring框架 編寫書籍APP項(xiàng)目https://www.bilibili.com/video/av71874024?p=17
環(huán)境要求
- IDEA
- MySQL(使用Navicat Preminm數(shù)據(jù)庫管理軟件)
- Tomcat
- Maven
技術(shù)要求:
- 熟練掌握數(shù)據(jù)庫鞭缭、spring、Javaweb压语、mybatis的知識褂傀,以及簡單的前端知識
簡易開發(fā)步驟.PNG
第一部分:創(chuàng)建數(shù)據(jù)庫階段
- 在Navicat Preminm中新建連接
接口:3307
用戶名密碼: root - 編寫SQL語句
//創(chuàng)建數(shù)據(jù)源
CREATE DATABASE books
//創(chuàng)建表
CREATE TABLE Books
(
bookID int(10) not NULL auto_increment COMMENT'書id',
bookName VARCHAR(100) not NULL COMMENT'書名',
bookCounts int(11) not NULL COMMENT'數(shù)量',
detail VARCHAR(200) not NULL COMMENT '描述',
PRIMARY KEY (bookID)
);
ENGINE=INNODB DEFAULT CHARSET(utf-8)
//插入3條數(shù)據(jù)
INSERT INTO books(bookID,bookName,bookCounts,detail)VALUES
(1,'java',12,'從入門到放棄'),
(2,'MySql',10,'從刪庫到跑路'),
(3,'Linux',5,'從進(jìn)門到進(jìn)牢');
-
結(jié)果如下:
數(shù)據(jù)庫.PNG
第二部分:IDEA 編寫SSM框架代碼階段
0. 準(zhǔn)備工作:
- 在IDEA中新建Maven工程
- 新項(xiàng)目需要注意在 file->settings->Maven 中設(shè)置本地maven倉庫
具體操作請點(diǎn)擊查看
新項(xiàng)目的maven本地倉庫設(shè)置.PNG
-
IDEA連接數(shù)據(jù)庫的方法:
IDEA連接數(shù)據(jù)庫.PNG 當(dāng)點(diǎn)擊測試出現(xiàn)因時(shí)區(qū)而產(chǎn)生錯誤時(shí):
-
解決辦法1:服務(wù)器時(shí)區(qū)錯誤的解決辦法1.PNG
-
解決辦法1:
-
解決辦法2:(未測試)
服務(wù)器時(shí)區(qū)錯誤的解決辦法-2.PNG
-
1. mybatis層:
簡單工程架構(gòu).PNG
- 首先在pom.xml中配置依賴的jar包:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lht</groupId>
<artifactId>SpringBooks</artifactId>
<version>1.0-SNAPSHOT</version>
<!--依賴jar包:Junit忍啤、數(shù)據(jù)庫驅(qū)動、servlet仙辟、jsp同波、mybatis-spring、spring-->
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--數(shù)據(jù)庫驅(qū)動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--數(shù)據(jù)庫連接池: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.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--lombok:Lombok能通過注解的方式参萄,在編譯時(shí)自動為屬性生成
構(gòu)造器、getter/setter煎饼、equals讹挎、hashcode校赤、toString方法。
出現(xiàn)的神奇就是在源碼中沒有g(shù)etter和setter方法筒溃,但是在編譯
生成的字節(jié)碼文件中有g(shù)etter和setter方法马篮。這樣就省去了手動
重建這些代碼的麻煩,使代碼看起來更簡潔些怜奖。
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
<!--靜態(tài)資源導(dǎo)出問題-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
在main->resources下創(chuàng)建:
applicationContext.xml
database.properties
mybatis-config.xml- applicationContext.xml中的內(nèi)容:
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--此間用來引入其他spring配置文件-->
</beans>
- database.properties中的內(nèi)容:
jdbc.driver=com.mysql.jdbc.Driver
#如果使用的是MySQL8.0+,增加一個時(shí)區(qū)的配置; &serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3307/books?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
- mybatis-config.xml中的內(nèi)容(這個文件是有關(guān)mybatis的全局配置):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置數(shù)據(jù)源,注冊映射關(guān)系,交給spring去做-->
<!--取別名浑测,這樣就可以在 sql 映射配置文件中使用別名來指定 輸入/輸出 參數(shù)的類型了-->
<typeAliases>
<package name="com.lht.pojo"/>
</typeAliases>
<!--注冊映射關(guān)系,將SQL語句與對應(yīng)接口綁定到配置文件-->
<mappers>
<!--class里是接口-->
<mapper class="com.lht.dao.BookMapper"></mapper>
</mappers>
</configuration>
- 在pojo中創(chuàng)建實(shí)體類(Books):
package com.lht.pojo;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
/**
* 實(shí)體類
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
- 在dao中創(chuàng)建A接口(DAO層操作):
package com.lht.dao;
import com.lht.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* DAO層
*/
public interface BookMapper {
//添加書
int addBook(Books books);
//刪除書
int deleteBooksById(@Param("bookID") int id);//#{}里面的名稱對應(yīng)的是注解@Param括號里面修飾的名稱。
//更新書
int updateBook(Books books);
//查詢書
Books queryBookById(@Param("bookID") int id);
//查詢所有
List<Books> queryAllBook();
}
- 在dao中創(chuàng)建A接口對應(yīng)的SQL語句配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lht.dao.BookMapper">
<insert id="addBook" parameterType="Books">
insert into books.books(bookName, bookCounts, detail)
values (#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="deleteBooksById" parameterType="int">
delete from books.books
where bookID=#{bookID}
</delete>
<update id="updateBook" parameterType="com.lht.pojo.Books">
update books.books
set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
where bookID=#{bookID}
</update>
<select id="queryBookById" parameterType="int" resultType="com.lht.pojo.Books">
select *
from books.books
where bookID=#{bookID}
</select>
<select id="queryAllBook" resultType="com.lht.pojo.Books">
select *
from books.books
</select>
</mapper>
- 在service中創(chuàng)建B接口(業(yè)務(wù)層):
package com.lht.service;
import com.lht.pojo.Books;
import java.util.List;
/**
* 業(yè)務(wù)層
*/
public interface BookService {
//添加書
int addBook(Books books);
//刪除書
int deleteBooksById(int id);
//更新書
int updateBook(Books books);
//查詢書
Books queryBookById(int id);
//查詢所有
List<Books> queryAllBook();
}
- 在service子文件夾impl中創(chuàng)建B接口的實(shí)現(xiàn)類(業(yè)務(wù)層操作):
package com.lht.service.impl;
import com.lht.dao.BookMapper;
import com.lht.pojo.Books;
import com.lht.service.BookService;
import java.util.List;
/**
* 業(yè)務(wù)層調(diào)DAO層歪玲,實(shí)現(xiàn)具體方法:組合DAO
*/
public class BookServiceImpl implements BookService {
//業(yè)務(wù)層調(diào)DAO層:組合DAO
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
//將來可以在此處橫切迁央、增強(qiáng)業(yè)務(wù)
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
//將來可以在此處橫切、增強(qiáng)業(yè)務(wù)
return bookMapper.addBook(books);
}
public int deleteBooksById(int id) {
//將來可以在此處橫切滥崩、增強(qiáng)業(yè)務(wù)
return bookMapper.deleteBooksById(id);
}
public int updateBook(Books books) {
//將來可以在此處橫切岖圈、增強(qiáng)業(yè)務(wù)
return bookMapper.updateBook(books);
}
public Books queryBookById(int id) {
//將來可以在此處橫切、增強(qiáng)業(yè)務(wù)
return bookMapper.queryBookById(id);
}
public List<Books> queryAllBook() {
//將來可以在此處橫切钙皮、增強(qiáng)業(yè)務(wù)
return bookMapper.queryAllBook();
}
}
2. spring層:
spring層整合.png
- 首先在resources下創(chuàng)建:
spring-dao.xml
spring-service.xml - spring-dao.xml是spring用來整合DAO的配置文件蜂科,內(nèi)容:
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.關(guān)聯(lián)數(shù)據(jù)庫配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!--數(shù)據(jù)源注入-->
<!--2.連接池
dbcp:半自動化操作,不能自動連接
*c3p0:自動化操作(自動化加載配置文件短条,并且可以自動設(shè)置到對象中5枷弧)
druid:有數(shù)據(jù)監(jiān)控功能
hikari:光速連接池,速度快茸时,效率高
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!--c3p0連接池的私有屬性-->
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="10"></property>
<!--關(guān)閉連接后不自動commit-->
<property name="autoCommitOnClose" value="false"></property>
<!--獲取連接超時(shí)時(shí)間-->
<property name="checkoutTimeout" value="10000"></property>
<!--當(dāng)獲取連接失敗重試次數(shù)-->
<property name="acquireRetryAttempts" value="2"></property>
</bean>
<!--工廠注入-->
<!--3.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--綁定mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--dao接口注入-->
<!--4.配置dao接口掃描包,動態(tài)的實(shí)現(xiàn)了dao接口可以注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--要掃描的dao包,把mapper接口動態(tài)添加到spring容器里/@Repository-->
<property name="basePackage" value="com.lht.dao"></property>
</bean>
</beans>
- spring-service.xml是spring用來整合service業(yè)務(wù)的配置文件贡定,內(nèi)容:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://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/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1.掃描service下的包-->
<context:component-scan base-package="com.lht.service"></context:component-scan>
<!--業(yè)務(wù)類注入-->
<!--2.將我們的所有業(yè)務(wù)類,注入到spring可都,可以通過配置厕氨,或者注解實(shí)現(xiàn)(方法:類上@Service+方法上@Autowired)-->
<bean id="BookServiceImpl" class="com.lht.service.impl.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"></property>
</bean>
<!--3.聲明事務(wù)配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入數(shù)據(jù)源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4.aop事務(wù)支持-->
<aop:config>
</aop:config>
</beans>
3. springMVC層:
springMVC.PNG
-
首先將SpringBooks項(xiàng)目右擊->選擇Add Frameworks Support->勾選Web Application添加:
項(xiàng)目添加web工程的方法.PNG WEB-INF下的web.xml,內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置調(diào)度servlet (DispatchServlet),及其映射-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化參數(shù)-綁定配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--
1)load-on-startup元素標(biāo)記容器是否在啟動的時(shí)候就加載這個servlet(實(shí)例化并調(diào)用其init()方法)汹粤。
2)它的值必須是一個整數(shù),表示servlet應(yīng)該被載入的順序
3)當(dāng)值為0或者大于0時(shí)田晚,表示容器在應(yīng)用啟動時(shí)就加載并初始化這個servlet嘱兼;
4)當(dāng)值小于0或者沒有指定時(shí),則表示容器在該servlet被選擇時(shí)才會去加載贤徒。
5)正數(shù)的值越小芹壕,該servlet的優(yōu)先級越高,應(yīng)用啟動時(shí)就越先加載接奈。
6)當(dāng)值相同時(shí)踢涌,容器就會自己選擇順序來加載。
-->
<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>
<!--初始化參數(shù)-設(shè)置編碼格式-->
<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>
<!--為了安全起見序宦,設(shè)置session過期時(shí)間為15分鐘
即客戶端連續(xù)兩次與服務(wù)器交互間隔時(shí)間最長為15分鐘睁壁,15分鐘后session.getAttribute()獲取的值為空
-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
- 在resources下添加spring-mvc.xml,此配置文件是spring整合M(配置模型)V(配置視圖解析器)C(配置控制器),內(nèi)容:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.注解驅(qū)動-->
<mvc:annotation-driven/>
<!--2.靜態(tài)資源過濾-->
<mvc:default-servlet-handler/>
<!--3.掃描controller包-->
<context:component-scan base-package="com.lht.controller"/>
<!--4.視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- applicationContext.xml是spring總配置文件潘明,用來整合其他配置文件(使用import引入其他配置文件)行剂,內(nèi)容:
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--applicationContext.xml是spring總配置文件,用來整合其他配置文件(使用import引入其他配置文件)-->
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
第三部分:IDEA 編寫業(yè)務(wù)/邏輯代碼階段