前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC和Mybatis的整合的工作原理

SpringMVC和Mybatis的整合的工作原理

作者头像
马克java社区
修改2019-09-25 17:42:20
4990
修改2019-09-25 17:42:20
举报
文章被收录于专栏:java大数据

4.SpringMVC和Mybatis的整合

在做这个项目之前,我们的前提条件是:你已经有一个能运行的SpringMVC的最简单的项目了。请同学们参考我的SpringMVC那章。做这个项目关键在于导包,请参考下图:

SpringMVC和Mybatis的整合的工作原理

org.mybatis.spring.mapper.MapperScannerConfigurer会自动往Spring容器注入com.mapper目录下所有的Mapper比如RegisterMapper(这就是为什么在Controller当中你能用@Resource注解找到RegisterMapper的实例的原因),条件是org.mybatis.spring.SqlSessionFactoryBean类的实例已经在Spring容器中(实际上id是多少都无所谓,这里就是sqlSessionFactoryqqqq,因为反正也没有人调用它,只要有实例在容器中就行),有了它,MapperScannerConfigurer才能正常工作,因为SqlSessionFactoryBean的一个属性是dataSource,它掌握着连接数据库的密码等。

例 1.4

<html>

<head>

<title>Spring 3.0</title>

</head>

<body>

<a href="helloa.do">Say Hello</a>

</body>

</html>

spring-servlet.xml:

<?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:p="http://www.springframework.org/schema/p"

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

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

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

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

<context:component-scan

base-package="com" />

<bean id="viewResolver"

class="org.springframework.web.servlet.view.UrlBasedViewResolver">

<property name="viewClass"

value="org.springframework.web.servlet.view.JstlView" />

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

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

</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >

更多请见:https://blog.csdn.net/qq_44639795/article/details/100163517

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档