B站教学 同步学习 运用Spring框架 编写书籍APP项目https://www.bilibili.com/video/av71874024?p=17
环境要求
- IDEA
- MySQL(使用Navicat Preminm数据库管理软件)
- Tomcat
- Maven
技术要求:
- 熟练掌握数据库、spring、Javaweb、mybatis的知识,以及简单的前端知识
简易开发步骤.PNG
第一部分:创建数据库阶段
- 在Navicat Preminm中新建连接
接口:3307
用户名密码: root - 编写SQL语句
//创建数据源
CREATE DATABASE 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 '描述',
PRIMARY KEY (bookID)
);
ENGINE=INNODB DEFAULT CHARSET(utf-8)
//插入3条数据
INSERT INTO books(bookID,bookName,bookCounts,detail)VALUES
(1,'java',12,'从入门到放弃'),
(2,'MySql',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
-
结果如下:
数据库.PNG
第二部分:IDEA 编写SSM框架代码阶段
0. 准备工作:
- 在IDEA中新建Maven工程
- 新项目需要注意在 file->settings->Maven 中设置本地maven仓库
具体操作请点击查看
新项目的maven本地仓库设置.PNG
-
IDEA连接数据库的方法:
IDEA连接数据库.PNG 当点击测试出现因时区而产生错误时:
-
解决办法1:服务器时区错误的解决办法1.PNG
-
解决办法1:
-
解决办法2:(未测试)
服务器时区错误的解决办法-2.PNG
-
1. mybatis层:
简单工程架构.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、数据库驱动、servlet、jsp、mybatis-spring、spring-->
<dependencies>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</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.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能通过注解的方式,在编译时自动为属性生成
构造器、getter/setter、equals、hashcode、toString方法。
出现的神奇就是在源码中没有getter和setter方法,但是在编译
生成的字节码文件中有getter和setter方法。这样就省去了手动
重建这些代码的麻烦,使代码看起来更简洁些。
-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
<!--静态资源导出问题-->
<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下创建:
applicationContext.xml
database.properties
mybatis-config.xml- applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--此间用来引入其他spring配置文件-->
</beans>
- database.properties中的内容:
jdbc.driver=com.mysql.jdbc.Driver
#如果使用的是MySQL8.0+,增加一个时区的配置; &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中的内容(这个文件是有关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>
<!--配置数据源,注册映射关系,交给spring去做-->
<!--取别名,这样就可以在 sql 映射配置文件中使用别名来指定 输入/输出 参数的类型了-->
<typeAliases>
<package name="com.lht.pojo"/>
</typeAliases>
<!--注册映射关系,将SQL语句与对应接口绑定到配置文件-->
<mappers>
<!--class里是接口-->
<mapper class="com.lht.dao.BookMapper"></mapper>
</mappers>
</configuration>
- 在pojo中创建实体类(Books):
package com.lht.pojo;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
/**
* 实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
- 在dao中创建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);//#{}里面的名称对应的是注解@Param括号里面修饰的名称。
//更新书
int updateBook(Books books);
//查询书
Books queryBookById(@Param("bookID") int id);
//查询所有
List<Books> queryAllBook();
}
- 在dao中创建A接口对应的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中创建B接口(业务层):
package com.lht.service;
import com.lht.pojo.Books;
import java.util.List;
/**
* 业务层
*/
public interface BookService {
//添加书
int addBook(Books books);
//删除书
int deleteBooksById(int id);
//更新书
int updateBook(Books books);
//查询书
Books queryBookById(int id);
//查询所有
List<Books> queryAllBook();
}
- 在service子文件夹impl中创建B接口的实现类(业务层操作):
package com.lht.service.impl;
import com.lht.dao.BookMapper;
import com.lht.pojo.Books;
import com.lht.service.BookService;
import java.util.List;
/**
* 业务层调DAO层,实现具体方法:组合DAO
*/
public class BookServiceImpl implements BookService {
//业务层调DAO层:组合DAO
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
//将来可以在此处横切、增强业务
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
//将来可以在此处横切、增强业务
return bookMapper.addBook(books);
}
public int deleteBooksById(int id) {
//将来可以在此处横切、增强业务
return bookMapper.deleteBooksById(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();
}
}
2. spring层:
spring层整合.png
- 首先在resources下创建:
spring-dao.xml
spring-service.xml - spring-dao.xml是spring用来整合DAO的配置文件,内容:
<?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.关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
<!--数据源注入-->
<!--2.连接池
dbcp:半自动化操作,不能自动连接
*c3p0:自动化操作(自动化加载配置文件,并且可以自动设置到对象中!)
druid:有数据监控功能
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>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"></property>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="10000"></property>
<!--当获取连接失败重试次数-->
<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接口扫描包,动态的实现了dao接口可以注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!--要扫描的dao包,把mapper接口动态添加到spring容器里/@Repository-->
<property name="basePackage" value="com.lht.dao"></property>
</bean>
</beans>
- spring-service.xml是spring用来整合service业务的配置文件,内容:
<?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>
<!--业务类注入-->
<!--2.将我们的所有业务类,注入到spring,可以通过配置,或者注解实现(方法:类上@Service+方法上@Autowired)-->
<bean id="BookServiceImpl" class="com.lht.service.impl.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"></property>
</bean>
<!--3.声明事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4.aop事务支持-->
<aop:config>
</aop:config>
</beans>
3. springMVC层:
springMVC.PNG
-
首先将SpringBooks项目右击->选择Add Frameworks Support->勾选Web Application添加:
项目添加web工程的方法.PNG WEB-INF下的web.xml,内容:
<?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">
<!--配置调度servlet (DispatchServlet),及其映射-->
<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:spring-mvc.xml</param-value>
</init-param>
<!--
1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。
2)它的值必须是一个整数,表示servlet应该被载入的顺序
3)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;
4)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。
5)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。
6)当值相同时,容器就会自己选择顺序来加载。
-->
<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过期时间为15分钟
即客户端连续两次与服务器交互间隔时间最长为15分钟,15分钟后session.getAttribute()获取的值为空
-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
- 在resources下添加spring-mvc.xml,此配置文件是spring整合M(配置模型)V(配置视图解析器)C(配置控制器),内容:
<?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.注解驱动-->
<mvc:annotation-driven/>
<!--2.静态资源过滤-->
<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引入其他配置文件),内容:
<?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 编写业务/逻辑代码阶段