发表于: 2019-11-25 22:24:08

1 1172


接口文档改良


springmvc入门程序小结

前端控制器配置:

第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析

第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析,使用此种方式可以实现RESTful风格的url

处理器映射器:

非注解处理器映射器(了解)

注解的处理器映射器(掌握)

对标记@Controller类中标识有@RequestMapping的方法进行映射。在@RequestMapping里边定义映射的url。使用注解的映射器不用在xml中配置url和Handler的映射关系。

处理器适配器:

非注解处理器适配器(了解) 注解的处理器适配器(掌握) 注解处理器适配器和注解的处理器映射器是配对使用。理解为不能使用非注解映射器进行映射。

<mvc:annotation-driven></mvc:annotation-driven> 

可以代替下边的配置:

<!--注解映射器 -->  

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  

    <!--注解适配器 -->  

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  

非注解的完整的配置文件

src/main/resources/springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       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-4.0.xsd

    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 配置Handler -->

    <bean name="/queryItems.action" class="com.iot.ssm.controller.ItemsController"/>

    <!-- 处理器映射器

    将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)

     -->

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!-- 处理器适配器

     所有处理器适配器都实现了HandlerAdapter接口

     -->

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!-- 视图解析器

    解析jsp,默认使用jstl,classpath下要有jstl的包

    -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

</beans>

注解的完整配置文件

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       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-4.0.xsd

    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

   <!-- 对于注解的Handler 可以单个配置

    实际开发中加你使用组件扫描

    -->

    <!--  <bean  class="com.iot.ssm.controller.ItemsController3"/> -->

    <!-- 可以扫描controller、service、...

这里让扫描controller,指定controller的包

-->

    <context:component-scan base-package="com.iot.ssm.controller"></context:component-scan>

    <!-- 注解的映射器 -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <!-- 注解的适配器 -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <!-- 使用mvc:annotation-driven代替上面两个注解映射器和注解适配的配置

     mvc:annotation-driven默认加载很多的参数绑定方法,

     比如json转换解析器默认加载了,如果使用mvc:annotation-driven则不用配置上面的RequestMappingHandlerMapping和RequestMappingHandlerAdapter

     实际开发时使用mvc:annotation-driven

     -->

    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 视图解析器

    解析jsp,默认使用jstl,classpath下要有jstl的包

    -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <!-- 配置jsp路径的前缀 -->

        <property name="prefix" value="/WEB-INF/jsp/"/>

        <!-- 配置jsp路径的后缀 -->

        <property name="suffix" value=".jsp"/>

    </bean>

</beans>

springmvc整合mybatis的思路

  • 第一步:整合dao层

    • mybatis和spring整合,通过spring管理mapper接口。
    • 使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
  • 第二步:整合service层

    • 通过spring管理service接口。
    • 使用配置方式将service接口配置在spring配置文件中。
    • 实现事务控制。
  • 第三步:整合springmvc

    • 由于springmvc是spring的模块,不需要整合。

  整合dao

首先在resource文件夹下添加两个文件:数据库配置文件和日志配置文件

数据库配置文件db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://120.25.162.238:3306/task2?characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

日志配置文件log4j.properties

# Global logging configuration

log4j.rootLogger=DEBUG, stdout

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

sqlMapConfig.xml

mybatis自己的配置文件

在resources目录下新建mybatis文件夹,并新建sqlMapConfig.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>

    <!-- 全局setting配置,根据需要添加 -->

    <!-- 配置别名 -->

    <typeAliases>

        <!-- 批量扫描别名 -->

        <package name="com.iot.learnssm.firstssm.po"/>

    </typeAliases>

    <!-- 配置mapper

    由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。

    必须遵循:mapper.xml和mapper.java文件同名且在一个目录

     -->

    <!-- <mappers>

    </mappers> -->

</configuration>

applicationContext-dao.xml

在resources目录下新建spring文件夹,并新建applicationContext-dao.xml文件

配置:

数据源

SqlSessionFactory

mapper扫描器

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       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-4.0.xsd

    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->

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

    <!-- 配置数据源 ,dbcp -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

        <property name="driverClassName" value="${jdbc.driver}"/>

        <property name="url" value="${jdbc.url}"/>

        <property name="username" value="${jdbc.username}" />

        <property name="password" value="${jdbc.password}" />

        <property name="maxActive" value="30" />

        <property name="maxIdle" value="5" />

    </bean>

    <!-- 从整合包里找,org.mybatis:mybatis-spring:1.2.4 -->

    <!-- sqlSessionFactory -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <!-- 数据库连接池 -->

        <property name="dataSource" ref="dataSource" />

        <!-- 加载mybatis的全局配置文件 -->

        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />

    </bean>

    <!-- mapper扫描器 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->

        <property name="basePackage" value="com.iot.learnssm.firstssm.mapper"/>

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

       <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />

       会导致数据源配置不管用,数据库连接不上。

       且spring 4弃用

       -->

    </bean>

</beans>

逆向工程生成po类及mapper(单表增删改查)

手动定义商品查询mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义mapper

ItemsMapperCustom.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.iot.learnssm.firstssm.mapper.ItemsMapperCustom" >

   <!-- 定义商品查询的sql片段,就是商品查询条件 -->

   <sql id="query_items_where">

    <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->

    <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 -->

    <if test="itemsCustom!=null">

    <if test="itemsCustom.name!=null and itemsCustom.name!=''">

    items.name LIKE '%${itemsCustom.name}%'

    </if>

    </if>

   </sql>

 

  <!-- 商品列表查询 -->

  <!-- parameterType传入包装对象(包装了查询条件)

  resultType建议使用扩展对象

  -->

  <select id="findItemsList" parameterType="com.iot.learnssm.firstssm.po.ItemsQueryVo"

  resultType="com.iot.learnssm.firstssm.po.ItemsCustom">

  SELECT items.* FROM items  

  <where>

  <include refid="query_items_where"></include>

  </where>

  </select>

 

</mapper>

ItemsMapperCustom.java

public interface ItemsMapperCustom {

    //商品查询列表

    List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;

}

po类ItemsCustom

package com.iot.learnssm.firstssm.po;

/**

 *  

 * 商品信息的扩展类

 */

public class ItemsCustom extends Items{

    //添加商品信息的扩展属性

}

输入pojo的包装类

package com.iot.learnssm.firstssm.po;

/**

 * 

 */

public class ItemsQueryVo {

    //商品信息

    private Items items;

    //为了系统 可扩展性,对原始生成的po进行扩展

    private ItemsCustom itemsCustom;

    public Items getItems() {

        return items;

    }

    public void setItems(Items items) {

        this.items = items;

    }

    public ItemsCustom getItemsCustom() {

        return itemsCustom;

    }

    public void setItemsCustom(ItemsCustom itemsCustom) {

        this.itemsCustom = itemsCustom;

    }

}

Controller类:


还有很多功能没实现。。

明天的计划 写完这个GITHUB小项目 




返回列表 返回列表
评论

    分享到