ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

后端三大框架整合

2021-07-02 21:06:49  阅读:147  来源: 互联网

标签:xml web account 框架 后端 Spring 控制器 public 三大


项目,学术之余断断续续的把Java后端三大框架Spring,SpringMVC,Mybatis学习完成。在这里对三大框架的整合做一个总结。

整体来看,Spring MVC,Mybatis都是通过Spring的Ioc容器来整合的。

首先从整个web项目的启动开始,tomcat会加载web.xml:

1. 配置前端控制器DispatcherServlet(启动时加载springmvc.xml);

2. 配置中文乱码过滤器CharacterEncodingFilter;

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--设置配置文件的路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!--解决中文乱码过滤器-->
  <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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--ServletContext监听器 加载spring配置文件 默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <!--前端过控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动服务器,创建该servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

springmvc.xml中:

1. 开启注解扫描,并且只扫描Controller(将所有的Controller对象交给Ioc管理);

2. 开启Spring MVC注解支持(隐式创建处理器映射器,处理器适配器);

3. 配置哪些静态资源不被前端控制器拦截;(html,css,js,images...)

4. 配置视图解析器;

<?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/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">

    <!--开启注解扫描,只扫描controller注解-->
    <context:component-scan base-package="nlp87v5">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--开启Spring MVC 注解的支持-->
    <mvc:annotation-driven/>

    <!--过滤静态资源-->
    <mvc:resources location="/static/html" mapping="/static/html/*"></mvc:resources>
    <mvc:resources location="/static/css" mapping="/static/css/*"></mvc:resources>
    <mvc:resources location="/static/js" mapping="/static/js/*"></mvc:resources>
    <mvc:resources location="/static/scripts" mapping="/static/scripts/*"></mvc:resources>

    <!--配置视图解析器-->
    <bean id="intervalResourceResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

到此为止,随着web.xml在tomcat中的加载,springmvc.xml也被加载,spring MVC中的前端控制器,处理器映射器,处理器适配器,视图解析器均被Spring的Ioc容器掌控着:

每当用户发送请求,请求到达SpringMVC的前端控制器(DispatcherServlet),前端控制器根据用户的url请求处理器映射器查找匹配该url的handler,并返回一个执行链,前端控制器再请求处理器适配器调用相应的handler进行处理并返回给前端控制器一个modelAndView,前端控制器再请求视图解析器对返回的逻辑视图进行解析,最后前端控制器将返回的视图进行渲染并把数据装入到request域,返回给用户。DispatcherServlet作为springMVC的前端控制器,负责接收用户的请求并根据用户的请求返回相应的视图给用户。 

而Controller层的方法需要调用Service层的方法,进行业务处理,Service层调用Dao层的方法,访问持久层。Service层和Dao层均交给Spring来管理,因此需要加载spring的配置文件:

在web.xml中配置ServletContext监听器ContextLoaderListener,该监听器会在web启动的时候,自动加载ApplicationContext的配置文件,但因为applicationConetext.xml在类路径下,ContextLoaderListener默认加载的是/WEB-INF,因此需要配置<context-param>。

通过以上机制,我们将Spring的相关配置放在applicationContext.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.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/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--开启扫描 希望处理service,dao controller不进行处理-->
    <context:component-scan base-package="nlp87v5">
        <!--配置哪些注解不扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--Spring整合Mybatis框架-->
    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="@Nlpnlp00"/>
    </bean>

    <!--配置SqlSessionFactory工厂对象-->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置AccountDao接口所在包-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="nlp87v5.dao"/>
    </bean>

    <!--配置Spring框架声明式事务管理-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

其中我们配置了

1. 开启注解扫描(Service,Dao,没有Controller)

2. Druid的数据库连接池对象,用来建立与数据库的连接

3. Mybatis的SqlSessionFactory对象,用来将dao接口实现为相应的dao实现类

4. Mybatis的MapperScannerConfiguration对象,用来扫描对象的dao接口包

从而实现了Controller,Service,Dao三层对象均交给Spring的Ioc容器来管理,并通过依赖注入的方式分别在三层中新建其他层对象:

Controller:

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        System.out.println("表现层:查询所有账户");
        //调用service方法
        List<Account> lists = accountService.findAll();
        model.addAttribute("list", lists);
        return "success";
    }

    @RequestMapping("/saveAccount")
    public String save(Account account) {
        System.out.println("表现层:保存账户");
         accountService.saveAccount(account);
        return "success";
    }

}

Service:

@Service("accountService")
@Transactional(propagation = Propagation.REQUIRED)
public class AccountService implements nlp87v5.service.AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public List<Account> findAll() {
        System.out.println("业务层:查询所有账户信息");
        return accountDao.findAll();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveAccount(Account account) {
        System.out.println("业务层:保存账户信息");
        accountDao.saveAccount(account);
    }
}

而对于Dao层只需要编写Dao接口,实现类交由Mybatis通过动态代理的方式来完成:

@Repository
public interface AccountDao {

    /**
     * 查询所有
     * @return
     */
    @Select("select * from account")
    public List<Account> findAll();

    /**
     * 保存账户信息
     * @param account
     */
    @Insert("insert into account(name, money) values(#{name}, #{money})")
    public void saveAccount(Account account);

}

标签:xml,web,account,框架,后端,Spring,控制器,public,三大
来源: https://blog.csdn.net/nlp87v5/article/details/118387010

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有