iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >spring、mybatis配置方式有哪些
  • 914
分享到

spring、mybatis配置方式有哪些

springmybatis 2023-05-30 18:05:36 914人浏览 八月长安
摘要

这篇文章将为大家详细讲解有关spring、mybatis配置方式有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一、 动态代理实现 不用写dao的实现类这种方式比较简单,不用实现dao层,只需要定义接

这篇文章将为大家详细讲解有关springmybatis配置方式有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一、 动态代理实现 不用写dao的实现类

这种方式比较简单,不用实现dao层,只需要定义接口就可以了,这里只是为了记录配置文件所以程序写的很简单:

整体结构图:

spring、mybatis配置方式有哪些

三个配置文件以及一个映射文件

(1)、程序入口以及前端控制器配置 WEB.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"  version="3.0">  <display-name>website1</display-name>  <!-- 设置监听,在web容器启动时自动装配ApplicationContext的配置信息-->  <listener>   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- 设置Spring容器加载配置文件路径 -->  <context-param>   <param-name>contextConfigLocation</param-name>   <param-value>   classpath:config/springMVC-servlet.xml,   classpath:config/ApplicationContext.xml   </param-value>  </context-param>  <!-- 字符编码过滤器 -->  <filter>   <filter-name>encodingFilter</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>   <init-param>    <param-name>forceEncoding</param-name>    <param-value>true</param-value>   </init-param>  </filter>  <filter-mapping>   <filter-name>encodingFilter</filter-name>   <url-pattern>*.do</url-pattern>  </filter-mapping>  <!-- 前端控制器 -->  <servlet>   <servlet-name>springmvc</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   <init-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:config/springmvc-servlet.xml</param-value>   </init-param>   <!-- 这个配置文件在容器启动的时候 就加载 -->   <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>   <servlet-name>springmvc</servlet-name>   <!-- 拦截请求 -->   <url-pattern>*.do</url-pattern>  </servlet-mapping>  <welcome-file-list>   <welcome-file>index.html</welcome-file>   <welcome-file>index.htm</welcome-file>   <welcome-file>index.jsp</welcome-file>   <welcome-file>default.html</welcome-file>   <welcome-file>default.htm</welcome-file>   <welcome-file>default.jsp</welcome-file>  </welcome-file-list> </web-app>

  (2)、扫描控制层、自动注入以及视图解析器的配置 springmvc-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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  xmlns:cache="http://www.springframework.org/schema/cache"  xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">  <!-- 注解驱动 -->  <mvc:annotation-driven />  <!-- <context:annotation-config /> -->  <!-- context:component-scan 具有annotation-config 的功能 -->  <!-- 扫描 控制层 -->  <context:component-scan base-package="com.website.controller"></context:component-scan>  <!-- 视图解析器 -->  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">   <property name="prefix" value="/WEB-INF/view/">   </property>   <property name="suffix" value=".jsp"></property>  </bean> </beans>

(3)、数据源、service 自动扫描注入、spring代管mybatissqlsessionFactory 、dao层接口动态代理以及事务的配置ApplicationContext.xml

这里会有多中配置文件

1)、单数据源,动态代理实现dao层接口时不设置sqlSessionFactoryBeanName、或sqlSessionTemplateBeanName 两个属性的值

<?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:tx="http://www.springframework.org/schema/tx" 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.2.xsd   http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.2.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  <!-- 加载配置JDBC文件 -->  <context:property-placeholder location="classpath:db.properties" />  <!-- 数据源 -->  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   <property name="driverClassName">    <value>${jdbc.driverClassName}</value>   </property>   <property name="url">    <value>${jdbc.url}</value>   </property>   <property name="username">    <value>${jdbc.username}</value>   </property>   <property name="passWord">    <value>${jdbc.password}</value>   </property>  </bean>  <!-- 开启注解配置 即Autowried -->  <!-- <context:annotation-config/> -->  <!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 -->  <context:component-scan base-package="com.website.service" />  <!-- 在使用mybatis时 spring使用sqlsessionFactoryBean 来管理mybatis的sqlsessionFactory -->  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">   <property name="dataSource" ref="dataSource" />   <!-- mybatis配置文件路径 -->   <property name="configLocation" value="" />   <!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis @Controller @RequestMapping(value = "/user") public class UserController {  @Autowired  private UserService userService;  @RequestMapping(value = "/save.do")  public String saveUser(httpservletRequest request,   HttpServletResponse response) {  String id = request.getParameter("id");  String name = request.getParameter("name");  String password = request.getParameter("password");  Map<String, String> map = new HashMap<String, String>();  map.put("id", id);  map.put("name", name);  map.put("password", password);  userService.saveUser(map);  return "index";  } }

业务逻辑层 service

package com.website.service; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.website.dao.UserDao;  @Service("userService") @Transactional public class UserService {  @Autowired  private UserDao userDao;  public void saveUser(Map<String, String> map) {  userDao.saveUser(map);  } }

dao层

package com.website.dao; import java.util.Map; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;  @Repository("userDao") public class UserDao {  @Autowired  private SqlSessionTemplate sqlSession;  public void saveUser(Map<String, String> map) {  int end = sqlSession.insert("com.website.userMapper.insertUser", map);  System.out.println("end" + end);  } }

我们看倒dao层的 SqlSessionTemplate  这个其实是mybatis中的SqlSession 对象,我们看到在ApplicationContext.xml 中配置了,所以在我们使用时spring会帮我们自动注入,我们直接使用就可以了不用去自己创建,这也就是所谓的控制反转。ok 到此两种文件的配置方式就结束了。

关于“spring、mybatis配置方式有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: spring、mybatis配置方式有哪些

本文链接: https://www.lsjlt.com/news/220294.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • spring、mybatis配置方式有哪些
    这篇文章将为大家详细讲解有关spring、mybatis配置方式有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一、 动态代理实现 不用写dao的实现类这种方式比较简单,不用实现dao层,只需要定义接...
    99+
    2023-05-30
    spring mybatis
  • spring 的配置方式有哪些
    这篇文章将为大家详细讲解有关spring 的配置方式有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。就目前来说spring的配置方式一般为两种:JAVA配置和注解配置//注解配置:@Se...
    99+
    2023-05-31
    spring
  • Spring的配置方式有哪些
    Spring的配置方式有以下几种:1. XML配置:使用XML文件配置Spring的各种组件,包括Bean的定义、依赖关系、AOP等...
    99+
    2023-08-15
    Spring
  • spring配置的方式有哪些
    在Spring框架中,配置的方式有以下几种: XML配置:使用XML文件来配置Spring的各种组件、依赖关系和属性等。XML文...
    99+
    2023-10-25
    spring
  • Mybatis typeAlias的配置方式有哪些
    这篇文章主要介绍了Mybatis typeAlias的配置方式有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatis typeAlias的配置方式有哪些文章都会有所收获,下面我们一起来看看吧。Myb...
    99+
    2023-06-26
  • mybatis集成到spring的方式有哪些
    本篇文章和大家了解一下mybatis集成到spring的方式有哪些。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。1 前言1.1 集成spring前使用mybatis的方式mybatis单独使用时,一般的写法如下所示://...
    99+
    2023-07-06
  • spring mybatis获取mapper的方式有哪些
    本篇内容介绍了“spring mybatis获取mapper的方式有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!spring-myba...
    99+
    2023-07-05
  • mybatis spring配置SqlSessionTemplate的使用方式
    mybatis spring配置SqlSessionTemplate使用 1.application.xml配置 <?xml version="1.0" encod...
    99+
    2024-04-02
  • spring装配bean的方式有哪些
    Spring装配Bean的方式有以下几种:1. 基于XML配置文件:通过在XML配置文件中定义Bean的方式进行装配,可以使用元素定...
    99+
    2023-09-27
    spring bean
  • Spring Boot + Mybatis + Spring MVC环境配置(二):Mybatis Generator配置
    一、在Eclipse中安装mybatis generator     菜单选择:Help->Eclipse Marketplace二、 创建generatorConfig.xml配置文档配置好的gener...
    99+
    2023-06-02
  • Spring Boot 与 mybatis配置方法
    1.首先,spring boot 配置mybatis需要的全部依赖如下:<!-- Spring Boot 启动父依赖 --><parent><groupId>org.springframework.boo...
    99+
    2023-05-31
    spring boot mybatis
  • react-router的配置方式有哪些
    这篇文章主要介绍react-router的配置方式有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体如下:路由的概念路由的作用就是将url和函数进行映射,在单页面应用中路由是必...
    99+
    2024-04-02
  • Spring Security核心配置有哪些
    这篇文章主要讲解了“Spring Security核心配置有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring Security核心配置有哪些”吧!核心配置解读3.1 功能介绍这...
    99+
    2023-06-04
  • Spring Boot配置文件有哪些
    这篇文章主要介绍Spring Boot配置文件有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、自定义属性当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resour...
    99+
    2023-06-19
  • Spring注入方式有哪些
    本篇内容介绍了“Spring注入方式有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Spring的三种注入方式属性(filed)注入这种...
    99+
    2023-06-25
  • mybatis分页的方式有哪些
    本篇内容主要讲解“mybatis分页的方式有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mybatis分页的方式有哪些”吧!mybatis分页的方式:1、借助数组进行分页,首先查询出全部数...
    99+
    2023-07-04
  • nginx跳转配置的方式有哪些
    这篇文章主要介绍了nginx跳转配置的方式有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇nginx跳转配置的方式有哪些文章都会有所收获,下面我们一起来看看吧。一、配置server对应的域名server n...
    99+
    2023-07-02
  • 代理ip的配置方式有哪些
    这篇文章主要介绍“代理ip的配置方式有哪些”,在日常操作中,相信很多人在代理ip的配置方式有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”代理ip的配置方式有哪些”的疑惑有所帮助!接下来,请跟着小编一起来...
    99+
    2023-06-20
  • Spring Boot整合Mybatis的配置方法
    这篇文章主要讲解了“Spring Boot整合Mybatis的配置方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring Boot整合Mybatis的配置方法”吧!目录配置文件形式p...
    99+
    2023-06-20
  • mybatis spring配置SqlSessionTemplate的使用方法
    这篇文章主要讲解了“mybatis spring配置SqlSessionTemplate的使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mybatis spring配置SqlSess...
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作