SSM框架整合
- 报错和原因
- 1.maven编译 Process terminated
- 2.maven导入了依赖找不到包
- 3.Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4
- 4 IDEA install项目时报错Please refer to…for the individual test results.
- 5.idea中无法自动装配Could not autowire. No beans of 'UserMapper' type found. more...
- 6.无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]解决方法
- 环境搭建
虽然学完了SSM开始整合了但是一整合就各种报错,所以大家一定要小心小心再小心,千万不要用idea的2020.1的版本全是bug趁早卸了换其他更稳定的版本
报错和原因
1.maven编译 Process terminated
首先第一个坑就是创建maven的项目名称时一定不要起中文名字
造成这个的原因可能有很多,然后我的问题就是项目的中文名字,时导致
<groupId>org.example</groupId>
<artifactId>ssm1</artifactId>
<version>1.0-SNAPSHOT</version>
artifactId里面有中文名字引起的
2.maven导入了依赖找不到包
这个是idea2020.1的锅
3.Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
4 IDEA install项目时报错Please refer to…for the individual test results.
两种解决方案
在maven 工程的pom.xml文件中加入
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
关闭maven的运行检查
5.idea中无法自动装配Could not autowire. No beans of ‘UserMapper’ type found. more…
还有其他的解决方法:大佬博客的解决方法链接
在自动转配的注解后面添加(required=false)
@Autowired(required=false)
public UserMapper userMapper;
6.无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]解决方法
这是idea的问题,有时候导入了报错最好检查一下这个lib里面可能jar不存在
最后就是千万不要用idea的2020.1的版本全是bug趁早卸了换其他更稳定的版本
还有一些其他的错误这里就不一一列举了还是要仔细小心
环境搭建
整体结构
首先创建一个空的maven项目
添加web
在pom.xml导入ssm的依赖
<?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>
<packaging>war</packaging>
<groupId>org.example</groupId>
<artifactId>dykssm1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>13</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--添加Servlet和JSP依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--spring AOP的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!--注解依赖 https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</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>
配置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">
<!-- springMVC前端控制器-->
<!-- 1.前端,接收所有的请求-->
<!-- 2.启动springMVC工厂 mvc.xml-->
<!-- 3.springMVC流程调度-->
<servlet>
<servlet-name>mvc_my</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 局部参数 声明配置文件位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc_my</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置springMVC的乱码过滤器-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--启动spring工厂-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
配置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: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/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 ">
<!-- 注解扫描-->
<context:component-scan base-package="com.blb.web" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 注解驱动-->
<mvc:annotation-driven>
<!-- 安装Fastjson转换器-->
<mvc:message-converters register-defaults="false">
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 加入支持的媒体类型:返回contentType -->
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然ie下会出现下载提示 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 视图解析器
作用 :1.捕获后端控制器的返回值 如果返回的是hello
2.解析:在返回值前后拼接 "/hello.jsp"
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/"></property>
<!-- 后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
各个层的代码编写
这里我主要是整合的笔记还不是项目,所有都没有什么具体的功能就是打印了
entity
public class User {
private Integer id;
private String username;
private String password;
private String sex;
private Date registTime;
get/set
}
dao层
UserMapper
package com.blb.dao;
import com.blb.entity.User;
import java.util.List;
public interface UserMapper {
public List<User> queryUsers();
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.blb.dao.UserMapper">
<select id="queryUsers" resultType="User">
select id,name,password,sex,regist_time registerTime
from t_users
</select>
</mapper>
service层
package com.blb.service;
import com.blb.entity.User;
import java.util.List;
public interface UserService {
public List<User> queryUsers();
}
controller层
package com.blb.web;
import com.blb.entity.User;
import com.blb.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class UserController {
private UserService userService;
@GetMapping("/users")
public String queryUsers(Model model){
System.out.println("queryUsers");
List<User> users = userService.queryUsers();
model.addAttribute("users",users);
return "index";
}
@GetMapping("/users/{id}")
public String queryOne(@PathVariable Integer id){
System.out.println("query users with get :"+id);
return "index";
}
@PutMapping("/users")
public String updateUser(@RequestBody User user){
System.out.println("update user with put: "+user);
return "index";
}
@DeleteMapping("/users/{id}")
public String deleteOne(@PathVariable Integer id){
System.out.println("delete one user with delete:"+id);
return "index";
}
@PostMapping("/users")
public String saveUser(@RequestBody User user){
System.out.println("save user with post: "+user);
return "index";
}
}
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db3?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
jdbc.init=1
jdbc.minIdle=1
jdbc.maxActive=10
配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--读取jdbc.properties 配置文件参数化(参数占位符)-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 集成Druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 基本配置-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 配置初始化大小,最大,最小-->
<property name="initialSize" value="${jdbc.init}"></property>
<property name="minIdle" value="${jdbc.minIdle}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 配置获取连接等待超时时间-->
<property name="maxWait" value="60000"></property>
<!-- 配置间隔多久时间才能进行一次检测,检测需要关闭的空闲连接,单位是毫秒-->
<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
<!-- 配置一个连接池中最小生存时间,单位是毫秒-->
<property name="minEvictableIdleTimeMillis" value="300000"></property>
</bean>
<!-- 生产sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入连接池-->
<property name="dataSource" ref="dataSource"></property>
<!-- 注入Mapper文件信息,如果映射文件和Mapper接口,同包且同名,则此配置可以省略-->
<property name="mapperLocations">
<list>
<value>classpath:com/blb/dao/*.xml</value>
</list>
</property>
<!-- 定义别名-->
<property name="typeAliasesPackage" value="com.blb.entity"></property>
</bean>
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- dao接口所在的包,如果有多个包逗号或分号分隔-->
<!-- <property name="basePackage" value="com.a.dao,com.b.dao"></property>-->
<property name="basePackage" value="com.blb.dao"></property>
<!-- 如果工厂只有一个sqlsessionfactory的bean此配置可以省略-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<context:component-scan base-package="com.blb" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 引入一个事务管理器,其中依赖DataSource,借以获取连接,进而控制事务逻辑-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--告知spirng@transactional在制定事务时基于tx= DataSourceTransactionManager -->
<tx:annotation-driven transaction-manager="tx"></tx:annotation-driven>
</beans>
logback配置文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 添加输出器(追加器) 作用:日志在什么地方进行输出
ch.qos.logback.core.ConsoleAppender:向控制台进行打印输出
-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder:编码-->
<encoder>
<!-- pattern:规定日志输出的格式
%d{HH:mm:ss.SSS}:开头的时间
[%thread]:输出线程的名称
%-5level:日志的级别 -5表示按5个字符右对齐
%logger{36}:表示那个类进行日志输出 36:这段字符串最多允许36个字符串,超过则会用简写的方式输出
-%msg:具体日志输出的内容
%n:换行
-->
<pattern>[%thread] %d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- 日志打印的根标签 level:日志输出级别-->
<root level="debug">
<!-- 引用appender name="console" 在日志输出的过程中会,只要是等于debug级别或以上都会按照 appender name="console"的格式进行打印-->
<appender-ref ref="console"/>
</root>
</configuration>
有时候如果没配置好代码格式在xml里面写中文注释,即使注释了也可能报错,XML文件存在中文注释报错问题( 3 字节的 UTF-8 序列的字节 3 无效),如果出现类似的问题直接把注释都删了试一下