SSM框架整合(以及遇到的各种问题)

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.
      • 在maven 工程的pom.xml文件中加入
      • 关闭maven的运行检查
    • 5.idea中无法自动装配Could not autowire. No beans of 'UserMapper' type found. more...
    • 6.无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]解决方法
  • 环境搭建
    • 整体结构
    • 首先创建一个空的maven项目
    • 添加web
    • 在pom.xml导入ssm的依赖
    • 配置web.xml
    • 配置mvc.xml
    • 各个层的代码编写
      • entity
      • dao层
      • UserMapper.xml
      • service层
      • controller层
    • jdbc.properties
    • 配置applicationContext.xml
    • logback配置文件

虽然学完了SSM开始整合了但是一整合就各种报错,所以大家一定要小心小心再小心,千万不要用idea的2020.1的版本全是bug趁早卸了换其他更稳定的版本

报错和原因

1.maven编译 Process terminated

首先第一个坑就是创建maven的项目名称时一定不要起中文名字
造成这个的原因可能有很多,然后我的问题就是项目的中文名字,时导致

<groupId>org.examplegroupId>
<artifactId>ssm1artifactId>
<version>1.0-SNAPSHOTversion>

artifactId里面有中文名字引起的

2.maven导入了依赖找不到包

SSM框架整合(以及遇到的各种问题)_第1张图片
这个是idea2020.1的锅

3.Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <version>2.4.2version>
                <configuration>
                    <skipTests>trueskipTests>
                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.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<configuration>
<testFailureIgnore>truetestFailureIgnore> 
 
configuration>
plugin>
plugins>
build>

关闭maven的运行检查

SSM框架整合(以及遇到的各种问题)_第2张图片

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不存在
SSM框架整合(以及遇到的各种问题)_第3张图片

最后就是千万不要用idea的2020.1的版本全是bug趁早卸了换其他更稳定的版本

还有一些其他的错误这里就不一一列举了还是要仔细小心

环境搭建

整体结构

SSM框架整合(以及遇到的各种问题)_第4张图片

首先创建一个空的maven项目

SSM框架整合(以及遇到的各种问题)_第5张图片

添加web

SSM框架整合(以及遇到的各种问题)_第6张图片
SSM框架整合(以及遇到的各种问题)_第7张图片

在pom.xml导入ssm的依赖


<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.0modelVersion>
    <packaging>warpackaging>

    <groupId>org.examplegroupId>
    <artifactId>dykssm1artifactId>
    <version>1.0-SNAPSHOTversion>
    <properties>
        <java.version>13java.version>
        <maven.compiler.source>${java.version}maven.compiler.source>
        <maven.compiler.target>${java.version}maven.compiler.target>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.7.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
            <scope>providedscope>
        dependency>


        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.75version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.2.7.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>5.2.7.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>5.2.7.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.3version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.20version>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.5version>
        dependency>

        <dependency>

            <groupId>com.alibabagroupId>

            <artifactId>druidartifactId>

            <version>1.1.16version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.30version>
        dependency>

        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.2.3version>
        dependency>

        
        <dependency>
            <groupId>javax.annotationgroupId>
            <artifactId>javax.annotation-apiartifactId>
            <version>1.3.2version>
        dependency>



    dependencies>

    
    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>
project>

配置web.xml


<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>
        <servlet-name>mvc_myservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:mvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>mvc_myservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>




    <filter>
        <filter-name>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-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


<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">

    
    <context:property-placeholder location="classpath:jdbc.properties" />

    <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>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="mapperLocations">
            <list>
                <value>classpath:com/blb/dao/*.xmlvalue>
            list>
        property>
        
        <property name="typeAliasesPackage" value="com.blb.entity">property>
    bean>

    <bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        
        <property name="basePackage" value="com.blb.dao">property>
        
        <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>

    
    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <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 无效),如果出现类似的问题直接把注释都删了试一下

你可能感兴趣的:(java,spring,java,mybatis,maven,ssm)