整合SSM框架及配置
7、整合SSM框架
7.1、导入相关依赖
<?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.bing</groupId>
<artifactId>ssmBuilder</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入相关依赖-->
<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>
<!-- 数据库连接池 -->
<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>
<!-- 支持jsp中的el表达式的-->
<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>
<!-- myBatis和spring结合的包-->
<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>
<!-- Spring连接jdbc的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- 偷懒-->
<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>
7.2、M 层
也就是Model层,也就是主要配置MyBatis
1、核心配置文件
在这里起个别名,表示我们用了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>
<!-- 开启日志功能,用这个默认的日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.bing.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.bing.mapper.BooksMapper"></mapper>
</mappers>
</configuration>
2、实体类
import lombok.NoArgsConstructor;
/**
* @author zhangbingbing
* @version 1.0
* @date 2020/5/28 17:02
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCount;
private String detail;
}
3、mapper
package com.bing.mapper;
import com.bing.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author zhangbingbing
* @version 1.0
* @date 2020/5/28 17:11
*/
public interface BooksMapper {
//增加一本数
int addBook(Books books);
//删除一本书
int deleteBookById(@Param("bookID") int id);
//修改书
int updateBook(Books book);
//查询一本书,其实可以实现模糊查询的
Books queryBookById(@Param("bookID") int id);
//查询全部
List<Books> queryAll();
}
- mapper.xml
<?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.bing.mapper.BooksMapper">
<insert id="addBook" parameterType="books">
insert into
smbmsbuilder.books(bookName, bookCount, detail)
VALUES (#{bookName}, #{bookCount}, #{detail});
</insert>
<delete id="deleteBookById" parameterType="int">
delete from smbmsbuilder.books where bookID = #{bookID};
</delete>
<update id="updateBook" parameterType="books">
update smbmsbuilder.books set
bookName = #{bookName}, bookCount = #{bookCount},
detail = #{detail} where bookID = #{bookID};
</update>
<select id="queryBookById" resultType="books">
select * from smbmsbuilder.books where bookID = #{bookID};
</select>
<select id="queryAll" resultType="books">
select * from smbmsbuilder.books;
</select>
</mapper>
4、service
package com.bing.service;
import com.bing.mapper.BooksMapper;
import com.bing.pojo.Books;
import java.util.List;
/**
* @author zhangbingbing
* @version 1.0
* @date 2020/5/28 17:52
*/
public class BooksServiceImpl implements BooksService {
private BooksMapper booksMapper;
public void setBooksMapper(BooksMapper booksMapper) {
this.booksMapper = booksMapper;
}
//真正的开发,我们会用aop在这个增加事务等功能
public int addBook(Books books) {
return booksMapper.addBook(books);
}
public int deleteBookById(int id) {
return booksMapper.deleteBookById(id);
}
public int updateBook(Books book) {
return booksMapper.updateBook(book);
}
public Books queryBookById(int id) {
return booksMapper.queryBookById(id);
}
public List<Books> queryAll() {
return booksMapper.queryAll();
}
}
7.3、spring层
首先我们要想清楚spring层干嘛?
- 整合Mybatis,连接数据库
- 整合mapper层
- 整合service
1、spring-mapper
1、连接关联数据库信息
数据库properties
如果是MySQL 8+的版本,则我们最好在连接的后面加上时区!
如果出现时区问题连接失败,则可以加上这一句设置时区:
serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/cnblogs?serverTimezone=Asia/Shanghai&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名词?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=用户名
password=密码
就是关联一下文件
<!-- 1、关联数据库文件-->
<context:property-placeholder location="classpath:db.properties"/>
2、创建数据库连接池
<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
3、配置sqlSessionFactory
3.1、连接上数据源
<!-- 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>
3.2、导入配置文件
4、创建扫描的包
不用写实现类了
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.bing.mapper"/>
</bean>
目的是让我们不用再写个实现类
2、spring-service
这个一把是做一些业务,典型的是AOP开启事务!
1、配置自动扫描
<!-- 把service包下的被注解配置的全部扫过来-->
<context:component-scan base-package="com.bing.service"/>
<!-- 注入-->
<bean id="bookServiceImpl" class="com.bing.service.BooksServiceImpl">
<property name="booksMapper" ref="booksMapper"/>
</bean>
2、开启事务
开启事务之前一定要导入AOP织入的依赖,不然又是500,说类无法创建异常
<!-- AOP织入-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!-- 开启事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 还可以有AOP事务切入,但是我们上面已经开启了事务它走的是默认值-->
<!-- 我们现在需要去通知再什么地方开启事务-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- AOP实现横向插入-->
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.bing.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
7.4、SpringMVC层
其实SpringMVC层对应Controller层
1、配置DispatcherServlet和过滤器
有个坑:这里一定要写全部配置文件
<?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">
<!-- 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>
<!-- 乱码过滤器,设置所有编码为utf-8-->
<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生存时间-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
2、配置springmvc-servlet.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 1、配置注解自动导入 处理器映射器 和 处理器适配器-->
<mvc:annotation-driven/>
<!-- 2、静态资源过滤,让代码不会zou .css .js等静态资源-->
<mvc:default-servlet-handler/>
<!-- 3、自动扫描指定的包,下面所有注解类交给IOC容器管理 ,用来扫面此包中的controller注解配置的controller-->
<context:component-scan base-package="com.bing.controller"/>
<!-- json乱码问题-->
<!-- 乱码问题统一解决-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
7.5、纠错步骤
0、每一个排错步骤第一步都是仔细阅读错误描述,发挥我们英语功底的时候到了!
1、提取异常信息
2、这里说Bean没找到,类没有找到
3、然后我们直接到管理这个类的bean中寻找
如果没配就配上,再试一下
如果配了就不是这个的问题
4、我们指用用junit,通过ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
获取看到底能不能获取到
5、如果获取到了,这时候问题就明显了
肯定是import等资源导入的问题
这时候我们就要查一些自动扫描或者资源导入的问题
1、web.xml中的
其作用是讲DisPatcherServlet和Spring配置文件联系到一起,就是联系三个东西
- 处理器映射器
- 处理器适配器
- 视图处理器
- 控制器:以后我们会用注解@Controller直接导入,就是那些controller
如果配错的报错信息:找不到BooksService
2、MyBatis中的namespace配错
3、MyBatis主配置文件的mapper没配
4、spring-mapper层
1、连接数据库
2、自动实现mappe层的接口
5、spring-service
6、spring-servlet层
<!-- 1、配置注解自动导入 处理器映射器 和 处理器适配器-->
<mvc:annotation-driven/>
<!-- 2、静态资源过滤,让代码不会zou .css .js等静态资源-->
<mvc:default-servlet-handler/>
<!-- 3、自动扫描指定的包,下面所有注解类交给IOC容器管理 ,用来扫面此包中的controller注解配置的controller-->
<context:component-scan base-package="com.bing.controller"/>
<!-- 乱码问题-->
<!-- 乱码问题统一解决-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
7、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">
<import resource="springmvc-servlet.xml"/>
<import resource="spring-mapper.xml"/>
<import resource="spring-service.xml"/>
</beans>