IDEA SSM框架实战CRUD(二)SSM整合配置 MyBatis逆向工程 (含报错的详细解决方案)

超详细IDEA SSM框架实战CRUD(一)创建一个Maven web tomacat(插件)项目:
https://blog.csdn.net/hahameier/article/details/115605731?spm=1001.2014.3001.5502

接上篇博客的内容继续。

三、步骤

4、编写SSM整合的关键配置文件

web.xml, spring, springmvc, mybatis

  • 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">
    <!--启动spring的容器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--SpringMVC的前端控制器, 拦截所有请求-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--字符编码过滤器。注意:一定放到所有字符编码之前-->
    <filter>
        <filter-name>CharacterEncodingFilter</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>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--使用REST风格的请求 将页面普通的post请求转为指定的delete和put请求-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
  • dispatcherServlet-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">

    <!--springMVC的配置,包含网站的跳转控制,配置-->
    <context:component-scan base-package="com" use-default-filters="false">
        <!--    只扫描控制器-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--    配置视图解析器,方便页面返回-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--    两个标准配置-->
    <!--    将Springmvc不能处理的请求交给tomcat-->
    <mvc:default-servlet-handler/>
    <!--    能支持springmvc更高级的一些功能,比如JSR303校验,快捷的ajax请求,映射动态请求-->
    <mvc:annotation-driven/>
</beans>
  • Spring 的配置文件,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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">



    <!--Spring的配置文件,这里主要配置和业务逻辑相关的-->
    <!--    数据源,事务控制等-->
    <context:component-scan base-package="com">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <context:property-placeholder location="classpath:dbconfig.properties"/>
    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--    配置和MyBatis的整合-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--        指定mybatis全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref="pooledDataSource"></property>
    <!--        指定mybatis,mapper文件的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>

    <!--    配置扫描器,将mybatis接口的实现加入到IOC容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--        扫描所有的dao接口的实现,加入到容器中-->
        <property name="basePackage" value="com.crud.dao"></property>
    </bean>

    <!--    事务控制的配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--        控制住数据源-->
        <property name="dataSource" ref="pooledDataSource"></property>
    </bean>

    <!--    开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)-->
    <aop:config>
    <!--        切入点表达式-->
        <aop:pointcut id="txPoint" expression="execution(* com.crud.service..*(..))"/>
    <!--        配置事务增强-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    <!--    配置事务增强,事务如何切入-->
    <tx:advice id="txAdvice">
        <tx:attributes>
    <!--    所有方法都是事务方法-->
            <tx:method name="*"/>
    <!--    以get开始的所有方法-->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
</beans>
  • dbconfig.properties
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/ssm_crud?useSSL=false
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=XXXX
  • mybatis-config.xml
<?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="mapUnderscoreToCamelCase" value="true"/>-->
<!--    </settings>-->
    <typeAliases>
        <package name="com.crud.bean"/>
    </typeAliases>
</configuration>
  • 创建数据库和表

在这里插入图片描述
在这里插入图片描述
表的具体内容可以参考视频教程https://www.bilibili.com/video/BV17W411g7zP中的P9。

  • 逆向工程 MBGTest.java
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MBGTest {
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("mbg.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

然后就生成出来了各种文件。
目录参考:
在这里插入图片描述

5、测试mapper

添加两张表的联合查询后,进行测试。

  • MapperTest.java
package com.crud.test;


import com.crud.dao.DepartmentMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试dao层的工作
 * 推荐Spring的项目就可以使用Spring的单元测试,可以自动注入我们需要的组件
 * 1、导入SpringTest
 * 2、使用ContextConfiguration注解指定Spring配置文件的位置
 * 3、直接autowired要使用的组件即可
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {
    @Autowired
    DepartmentMapper departmentMapper;
    /**
     * 测试DepartmentMapper
     */
    @Test
    public void testCRUD(){

//        ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
//        DepartmentMapper bean = ioc.getBean(DepartmentMapper.class);
        System.out.println(departmentMapper);
    }
}

本次内容中的错误汇总:

1、数据库连接错误

因为当前我电脑里面没有数据库的,我就搜索了下当下的数据库的软件,随即下载了并安装了DataGrip。由于太久没有接触这些东西,有点点懵,尝试直接用DataGrip来直接连接,测试连接的时候出现了以下的错误:
[08S01] Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. java.net.ConnectException: Connection refused (Connection refused).

由于我是没有安装MySQL的,所以需要先安装MySQL。我这里附上我使用的教程参考。
MySQL下载:
https://dev.mysql.com/downloads/mysql/
MySQL安装:
https://zhuanlan.zhihu.com/p/27960044
https://cloud.tencent.com/developer/article/1625825

2、逆向工程的时候,数据库连接错误

Establishing SSL connection without server’s identity verification is not recommended. Ac
解决办法:

jdbc.jdbcUrl = jdbc:mysql://localhost:3306/ssm_crud

需要将要加入?useSSL=false

jdbc.jdbcUrl = jdbc:mysql://localhost:3306/ssm_crud?useSSL=false

参考链接:https://cloud.tencent.com/developer/article/1601218

3、MySQL版本问题

上面的问题解决后,紧接着出现了如下的错误:
Exception in thread “main” java.sql.SQLException: Unknown system variable ‘query_cache_size’
解决办法:需要将pom.xml文件中的mysql-connector-java的版本修改为8.0.11

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
</dependency>
4、数据库驱动问题

连环报错,解决了上面的版本问题后,Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 驱动也需要进行修改。

按照给的提示,将com.mysql.jdbc.Driver修改为com.mysql.cj.jdbc.Driver即可。

参考链接:https://blog.csdn.net/weixin_42054155/article/details/100412848

5、逆向工程无报错,但是没有生成文件

因为在mac中,所有路径的斜杠,要反过来才行。
解决办法:将mbg.xml文件中所有路径的斜杠方向修改为以下形式:targetProject="/src/main/java”

6、测试mapper错误

java.lang.IllegalStateException: Failed to load ApplicationContext

需要修改的点:

1. 将pom.xml文件中的scope这行删去
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.7.RELEASE</version>
    <scope>test</scope>   删掉这一行
</dependency>
2. 将mybatis.config文件中的驼峰命名删去
<!--    <settings>-->
<!--        <setting name="mapUnderscoreToCamelCase" value="true"/>-->
<!--    </settings>-->
7、 “通配符的匹配很全面, 但无法找到元素 ‘context:component-scan’ 的声明”

原因:命名空间不完整。
将web.xml文件中的内容按照以下内容进行修改

<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"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> 
8、Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionDecorator for element [property-placeholder]

错误:(context:的位置放错了,写到注解里面去了)
在这里插入图片描述
正确:
在这里插入图片描述
参考链接:https://blog.csdn.net/hzz1993/article/details/89207421

码字截图不容易,如果对你有帮助的话,点赞评论下再走呀~有什么问题可以评论区问,看见了会回答。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值