SSM项目(三) 项目搭建 -- 4. 设置web.xml文件 、5. 使用Mabatis逆向工程生成pojo和mapper

4. 设置web.xml文件

4.1 添加字符编码过滤器

注:字符编码过滤器需放在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">
    <!--添加字符编码过滤器(注:字符编码过滤器需放在web.xml顶端)-->
    <filter>
        <filter-name>encode</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>ture</param-value>
        </init-param>
        <!--参数:强制响应转换-->
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>ture</param-value>
        </init-param>
    </filter>
    <!--匹配所有请求-->
    <filter-mapping>
        <filter-name>encode</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!--注册SpringMVC框架-->

    <!--注册Spring框架-->
</web-app>

4.2 注册SpringMVC框架

    <!--注册SpringMVC框架 - 核心是servlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--参数:springmvc配置文件路径-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <!--匹配以.action为后缀请求-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <!--注册Spring框架-->

4.3 注册Spring框架

spring配置文件若在WEB-INF目录下,会自动加载,在其他位置要手动加载

加载多个文件时有两个方法
1. 逗号隔开 :classpath:applicationContext_dao.xml,classpath:applicationContext_service.xml
2. 使用通配符 :classpath:applicationContext_*.xml (代表以 applicationContext_ 为前缀以.xml为后缀的文件)

    <!--注册Spring框架 - 通过监听器注册-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--spring配置文件若在WEB-INF目录下,会自动加载,在其他位置要手动加载-->
    <!--加载spring配置文件(applicationContext_dao.xml,applicationContext_service.xml)-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--加载多个文件时有两个方法
              1. 逗号隔开 :classpath:applicationContext_dao.xml,classpath:applicationContext_service.xml
              2. 使用通配符 :classpath:applicationContext_*.xml (代表以 applicationContext_ 为前缀以.xml为后缀的文件)
        -->
        <param-value>classpath:applicationContext_*.xml</param-value>
    </context-param>

5. 使用Mabatis逆向工程生成pojo和mapper

5.1 导入逆向工程

逆向工程简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sqI的定义需要我们手工编写

在这里插入图片描述

5.2 修改配置文件

在generatorConfig.xml中配置Mapper生成的详细信息,如下图:
在这里插入图片描述
注意修改内容主要以下几点:

  1. 修改数据库连接的信息

    	<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    	<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
    		connectionURL="jdbc:mysql://localhost:3306/mimissm?useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true" 
    		userId="root"
    		password="root">
    	</jdbcConnection>
    
  2. 指定数据库表 (生成那些数据库表对应的文件) – admin,product_info,product_type

     	<!-- 指定数据库表 -->
     	<table schema="" tableName="admin"></table>
     	<table schema="" tableName="product_info"></table>
     	<table schema="" tableName="product_type"></table>
    
  3. 生成PO类的位置 – com.yanyu.pojo

    <!-- targetProject:生成PO类的位置 -->
    	<javaModelGenerator targetPackage="com.yanyu.pojo"
    		targetProject=".\src">
    		<!-- enableSubPackages:是否让schema作为包的后缀 -->
    		<property name="enableSubPackages" value="false" />
    		<!-- 从数据库返回的值被清理前后的空格 -->
    		<property name="trimStrings" value="true" />
    	</javaModelGenerator>
    
  4. mapper映射文件生成的位置 – com.yanyu.mapper

     <!-- targetProject:mapper映射文件生成的位置 -->
    	<sqlMapGenerator targetPackage="com.yanyu.mapper"
    		targetProject=".\src">
    		<!-- enableSubPackages:是否让schema作为包的后缀 -->
    		<property name="enableSubPackages" value="false" />
    	</sqlMapGenerator>
    
  5. mapper接口生成的位置

    <!-- targetPackage:mapper接口生成的位置 -->
     	<javaClientGenerator type="XMLMAPPER"
     		targetPackage="com.yanyu.mapper"
     		targetProject=".\src">
     		<!-- enableSubPackages:是否让schema作为包的后缀 -->
     		<property name="enableSubPackages" value="false" />
     	</javaClientGenerator>
    

5.3 运行GeneratorSqlmap,生成pojo和mapper文件

在这里插入图片描述

运行前删除src下同名目录,防止文件重叠

运行程序后,在设置的目录下生成对应文件
在这里插入图片描述

5.4 MyBatis逆向工程中的Mapper接口以及Example的实例函数及详解

参考:https://blog.csdn.net/qq_44058265/article/details/120460879

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值