在此做一个对Mvc的复习,便于以后快速复习mvc配置。

开发环境 :

    IDe :iDEA

    JDK:1.8

    使用的框架技术:Mybtais ,Spring,Spring MVC 数据源选用Dbcp

首先贴上我们的项目结构,很简单的一个Demo层次分明


当我们使用IDea创建Spring MVC项目时Idea会自动为我们加入Web组件 我们可以看到我们的配置文件并没有像myeclipse一样在Src下 ,IDea默认给我们的配置文件放到了Web/web-inf文件夹下

 上面这些JAr是我们自己导入的,让我们来看一下

以下的Jar包为Idea自动为我们下载并且添加到项目中的,在这里不多做阐述


按照Mvc的流程来说项目对应实体结构如下


 我们在entity包中创建我们与数据库相连接,并且指定对应表内字段的一一对应。


数据库字段如下


通常的执行流程为:Serviceimpl调用和对应Mapper 来实现对数据库的增删改查等一系列功能

这里演示了一个简单的查询全部,并且使用 El 和JStl将查询的数据显示到JSp页面上,

Mapper

 package com.mapper;

 import com.entity.User;
import org.springframework.stereotype.Repository; import java.util.List;
@Repository
public interface UserMapper {   与下面对应Mapper中的Id向对应
List<User> cxall();
}

于是便引入今天的第一个配置,对对应Mapper的操作映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" > 引入对对应Mapper操作的命名空间,来告诉Mybatis 我们要对哪一个Mapper执行什么样的操作
<mapper namespace="com.mapper.UserMapper">
简单查询,id对应Mapper中的查询方法名, 返回结果集也是一个User类型的
<select id="cxall" resultType="User">
select * from smbms_user
</select>
</mapper>

  


 Service:

 package com.service;

 import com.entity.User;
import org.springframework.stereotype.Repository; import java.util.List; public interface UserService {
List<User> cxall();
}

以及Service的实现:

package com.serviceimpl;

import com.entity.User;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List;
@Service
public class UserServiceImpl implements UserService {
  使用自动注入将UserMapper注入到当前Service的实现中,调用其方法
@Autowired
private UserMapper userMapper;
@Override
public List<User> cxall() {
return userMapper.cxall();
}
}

controller:

 package com.web;

 import com.alibaba.fastjson.JSON;
import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List;
@RequestMapping("/user")
@Controller
public class UserController {
  注入Service 调用其子类的实现 @Autowired
private UserService userService;

  结果返回页面的Json格式乱码的问题
@RequestMapping(value = "/cx.do",produces = {"text/javascript;charset=UTF-8"})
@ResponseBody
public String cx(){
List<User> cxall = userService.cxall();
String JSon= JSON.toJSONString(cxall);
return JSon;
}
}

如上便是这个小Demo的经典三层实现,下面我们来说说为什么通过这么简单的操作就可以达到我们所要的效果呢?

首先我们来看一下我们的Jdbc配置文件


jdbc.properties

 jdbc.driver=com.mysql.jdbc.Driver
jdbc.uri=jdbc:mysql://localhost\:3306/smbms?useUnicode\=true&characterEncoding\=utf8
jdbc.name=root
jdbc.pwd=1234

Log4J.properties

log4j.rootLogger=debug, stdout,logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%l %F %p %m%n
org.apache.jasper.servlet.TldScanner.level = FINE

重点在下面,我们看一下Idea自动在为我们配置了什么,我们想当这个项目启动的时候,就自动为启动我们SpringMvc的组件,那么我们这个Demo是一个Web工程,并不需要对应的Main方法之类,那我们究竟改怎么做呢?

我们来看一下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>/WEB-INF/applicationContext.xml</param-value>
</context-param>
    同理,自动加载Mvc配置文件,加载位置是在Web/web-inf/文件夹下,通常我们在Myeclipse中的配置文件是在Src下 ,这里不要搞混淆
<context-param>
<param-name>spring</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
    配置过滤器
<filter>
<filter-name>filter</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>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
   配置了监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
    配置了前端控制器
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
     对所有访问为.do的访问进行拦截处理
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

 


 Spring配置文件

 <?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"
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">
    
        加载类路径下的Jdbc配置文件
<context:property-placeholder location="classpath:Jdbc.properties"></context:property-placeholder>

      配置Dbcp数据源
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.uri}"></property>
<property name="username" value="${jdbc.name}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean>
      配置SqlsessionFactory用来创建Sqlsession
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
    对该包下的实体类起别名  不需要用全类名的方法要标记
<property name="typeAliasesPackage" value="com.entity"></property>
      扫描对应的Mapper.xml 需要引入的Mapper.Xml
<property name="mapperLocations">
<list>
<value>classpath*:com/mapper/UserMapper.xml</value>
</list>
</property>
</bean>
      配置Mapper扫描,对指定mAooer包下的的Mapper生成对应的实现,并且注入Spring容器
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"></property>
</bean> </beans>

mvc配置:

 <?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
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--mvc自动配置-->
<mvc:annotation-driven/>
     扫描对应文件夹下的所有类 开启注解
<context:component-scan base-package="com"></context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

上述有一点需要注意的是:在我们开启Mvc配置的时候一定要导入Mvc的命名空间,

实例:


项目运行测试:

前台以Json格式返回。没有进行多余的操作。若有问题敬请大佬指正,想要一起学习交流的欢迎加入Java交流群:682677231

SpringMvc配置详解的更多相关文章

  1. SpringMVC配置详解(转)

    要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar ...

  2. springmvc配置详解 教程

    https://www.cnblogs.com/sunniest/p/4555801.html

  3. SpringMVC 框架系列之组件概述与配置详解

    在上一篇文章 SpringMVC 框架系列之初识与入门实例 的实例中,我们已经知道,SpringMVC 框架是一个 web 层的框架,本篇文章就详细解释一下 SpringMVC 框架具体文件的配置以及 ...

  4. SpringMVC RequestMapping 详解

    SpringMVC RequestMapping 详解 RequestMapping这个注解在SpringMVC扮演着非常重要的角色,可以说是随处可见.它的知识点很简单.今天我们就一起学习Spring ...

  5. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  6. Spring学习 6- Spring MVC (Spring MVC原理及配置详解)

    百度的面试官问:Web容器,Servlet容器,SpringMVC容器的区别: 我还写了个文章,说明web容器与servlet容器的联系,参考:servlet单实例多线程模式 这个文章有web容器与s ...

  7. 2017.3.31 spring mvc教程(二)核心流程及配置详解

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  8. 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解

    http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...

  9. web.xml常用配置详解

    web.xml常用配置详解 context-param 指定 ServletContext(上下文) 配置文件路径,基本配置一般是Spring配置文件,或者是spring-security的配置文件. ...

随机推荐

  1. 解决ajax chrome禁止本地浏览时加载本地其他文件的方法

    在chrome快捷键右键--属性 “ --allow-file-access-from-files ”,前面用空格隔开.然后应用--确定.

  2. 2017 ACM暑期多校联合训练 - Team 4 1012 HDU 6078 Wavel Sequence (模拟)

    题目链接 Problem Description Have you ever seen the wave? It's a wonderful view of nature. Little Q is a ...

  3. HDU 5995 Kblack loves flag (模拟)

    题目链接 Problem Description Kblack loves flags, so he has infinite flags in his pocket. One day, Kblack ...

  4. Axure RP 授权码

    Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...

  5. 关于[神州数码信息安全DCN杯/信息安全管理与评估]的一些经验之谈

    前阵子参加了神州数码的比赛,赛后有如下经验分享,给还没参加过的朋友分享一下心德以及要注意的坑. 先科普一下这个比赛的三个阶段: 第一阶段主要是考网络部分的,例如搭建wifi以及防火墙诸如此类的设备. ...

  6. C# 开发(创蓝253)手机短信验证码接口

    创蓝253: https://www.253.com/ #region 获取手机验证码(创蓝253) /// <summary> /// 获取手机验证码(创蓝253) /// </s ...

  7. setsid

    说明:当进程是会话的领头进程时setsid()调用失败并返回(-1).setsid()调用成功后,返回新的会话的ID,调用setsid函数的进程成为新的会话的领头进程,并与其父进程的会话组和进程组脱离 ...

  8. 一键去除网页BOM属性【解决乱码,头部空白,&#65279问题】

    几个常出现的问题: 1.网站打开空白 2.页面头部出现多余的空白 3.网站出现乱码,如“锘�” 解决方法可以是: 1.选用专业的编辑器,例如notepad++,sublime,editplus这样不会 ...

  9. golang之结构体和方法

    结构体的定义 结构体是将零个或者多个任意类型的命令变量组合在一起的聚合数据类型.每个变量都叫做结构体的成员. 其实简单理解,Go语言的结构体struct和其他语言的类class有相等的地位,但是GO语 ...

  10. [MySQL] specified key was too long max key length is 767bytes

    https://blog.csdn.net/u012099869/article/details/53815084/