springmvc详解(二),字节跳动后端技术面

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

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

http://www.springframework.org/schema/mvc

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

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<context:component-scan base-package=“cn.itcast.ssm.service.impl”></context:component-scan>

3.2.2、spring管理事务的配置文件applicationContext-transaction.xml

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

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

http://www.springframework.org/schema/mvc

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

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<bean id=“transactionManager”

class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>

<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>

tx:attributes

<tx:method name=“save*” propagation=“REQUIRED”/>

<tx:method name=“delete*” propagation=“REQUIRED”/>

<tx:method name=“insert*” propagation=“REQUIRED”/>

<tx:method name=“update*” propagation=“REQUIRED”/>

<tx:method name=“find*” propagation=“SUPPORTS” read-only=“true”/>

<tx:method name=“get*” propagation=“SUPPORTS” read-only=“true”/>

<tx:method name=“select*” propagation=“SUPPORTS” read-only=“true”/>

</tx:attributes>

</tx:advice>

aop:config

<aop:advisor advice-ref=“txAdvice” pointcut="execution(*

cn.itcast.ssm.service.impl..(…))"/>

</aop:config>

3.3、配置springmvc的配置文件


springmvc是spring的一个模块,不需要整合,但也需要在springmvc.xml中配置Handler、处理器映射器、适配器、视图解析器。

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

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

http://www.springframework.org/schema/mvc

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

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<context:component-scan base-package=“cn.itcast.ssm.controller”></context:component-scan>

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

<bean

class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>

3.4、配置web.xml文件


在web.xml文件中完成一下几件事:

1、配置springmvc的前端控制器(加载springmvc配置文件)

2、加载spring的配置文件

3、加载监听器

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=“http://xmlns.jcp.org/xml/ns/javaee” xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd” id=“WebApp_ID” version=“3.1”>

springmvcfirst

contextConfigLocation

/WEB-INF/classes/spring/applicationContext-*.xml

org.springframework.web.context.ContextLoaderListener

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

springmvc

*.action

index.html

index.htm

index.jsp

<welc

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

ome-file>default.html

default.htm

default.jsp

3.5、三层整合配置文件总结


3.5.1、dao层

1、mybatis自己的配置文件sqlMapConfig.xml

实现别名定义、缓存设置等配置。

2、dao层spring配置文件applicationContext-dao.xml

完成数据源、SqlSessionFactory、mapper扫描器的配置。

3.5.2、service层

1、service层spring配置文件applicationContext-service.xml

管理service接口实现类的bean(注解方式)

2、spring管理事务的配置文件applicationContext-transaction.xml

实现事务控制、AOP

3.5.3、表现层配置文件springmvc.xml

配置Handler、处理器映射器、适配器、视图解析器。

3.5.4、web.xml文件

配置springmvc的前端控制器(加载springmvc配置文件)、加载spring的配置文件、加载监听器

四、三层代码编写

========

4.1、dao层


4.1.1、逆向工程生成单表的po类及mapper

参照博客:MyBatis详解:逆向工程自动生成代码

4.1.2、手动定义多表的po类和mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义po类和mapper,一般多表的po类继承自主表,次表以属性形式加载在po类中。

po类:ItemsCustom.java

这里仍是单表,多表的话在其中增加扩展属性即可。

package cn.itcast.ssm.po;

public class ItemsCustom extends Items {

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

}

ItemsQueryVo.java

package cn.itcast.ssm.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;

}

}

mapper.xml

在其中定义操作数据库的sql语句

<?xml version="1.0" encoding="UTF-8" ?>

items.name LIKE ‘%${itemsCustom.name}%’

<select id=“findItemsList” parameterType=“cn.itcast.ssm.po.ItemsQueryVo”

resultType=“cn.itcast.ssm.po.ItemsCustom”>

SELECT items.* FROM items

mapper.java

在其中定义对应sql语句的抽象方法

package cn.itcast.ssm.mapper;

import cn.itcast.ssm.po.Items;

import cn.itcast.ssm.po.ItemsCustom;

import cn.itcast.ssm.po.ItemsExample;

import cn.itcast.ssm.po.ItemsQueryVo;

import java.util.List;

import org.apache.ibatis.annotations.Param;

public interface ItemsMapperCustom {

//商品查询列表

public List findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;

}

4.2、service层


4.2.1、service接口

package cn.itcast.ssm.service;

import java.util.List;

import cn.itcast.ssm.po.ItemsCustom;

import cn.itcast.ssm.po.ItemsQueryVo;

public interface ItemsService {

//商品查询列表

public List findTtemsList(ItemsQueryVo itemsQueryVo) throws Exception;

}

4.2.2、service接口实现类

还记得外层向里的调用逻辑吗?:Handler中可以调用service接口,service中可以调用mapper接口。

由于service要调用mapper,故需要在service接口实现类中注入mapper代理对象

package cn.itcast.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import cn.itcast.ssm.mapper.ItemsMapperCustom;

import cn.itcast.ssm.po.ItemsCustom;

import cn.itcast.ssm.po.ItemsQueryVo;

import cn.itcast.ssm.service.ItemsService;

@Service(“itemsService”)

public class ItemsServiceImpl implements ItemsService{

@Autowired

private ItemsMapperCustom itemsMapperCustom;

@Override

public List findTtemsList(ItemsQueryVo itemsQueryVo) throws Exception {

return itemsMapperCustom.findItemsList(itemsQueryVo);

}

}

4.3、表现层


4.3.1、编写Controller(就是Handler)

由于表现层handler要调用service接口,故需要在handler中注入service接口。

package cn.itcast.ssm.controller;

import java.util.ArrayList;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import cn.itcast.ssm.po.Items;

import cn.itcast.ssm.po.ItemsCustom;

import cn.itcast.ssm.service.ItemsService;

//商品的controller

@Controller

public class ItemsController {

@Autowired

private ItemsService itemsService;

//商品查询列表

//@RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url

//一般建议将url和方法写成一样

@RequestMapping("/queryItems")

public ModelAndView quertItems() throws Exception{

//调用service查找 数据库,查询商品列表,这里使用静态数据模拟

List itemsList = itemsService.findTtemsList(null);

//返回ModelAndView

ModelAndView modelAndView = new ModelAndView();

//相当 于request的setAttribut,在jsp页面中通过itemsList取数据

modelAndView.addObject(“itemsList”, itemsList);

//指定视图

//下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为

//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

//上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀

modelAndView.setViewName(“items/itemsList”);

return modelAndView;

}

}

4.3.2、编写jsp

编写itemsList.jsp文件,存于/springmvcfirst/WebRoot/WEB-INF/jsp/items/itemsList.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c” %>

<%@ taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt”%>

查询商品列表

查询条件:

商品列表:

商品名称 商品价格 生产日期 商品描述 操作

<c:forEach items="${itemsList }" var=“item”>

${item.name } ${item.price } ${item.detail }

id=${item.id}">修改

</c:forEach>

4.4、三层代码编写总结


4.4.1、dao层

1、逆向工程生成单表的po类及mapper

2、手动定义多表的po类和mapper(包括mapper.java和mapper.xml)

一般多表的po类继承自主表,次表以属性形式加载在po类中。在mapper.xml定义操作数据库的sql语句,在mapper.java中编写sql语句对应的抽象方法。

4.4.2、service层

1、编写service接口,对应于所调用的dao层的mapper.java

2、编写service接口的实现类,并在其中注入mapper代理对象

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值