applicationContext配置mybatis

1.引入外部的properties属性文件

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd">

2. 使用import引入分配置文件

<!--将分配置文件引进来-->
<import resource="classpath:applicationContext-dao.xml"/>

3.applicationContext.xml中整合mybatis

第一步:导入spring整合mybatis的依赖

 <dependencies><!--mybatis和mysql依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--spring核心依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency><!--spring整合mybatis框架--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!--druid连接池依赖--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.6</version></dependency></dependencies>

第二步:在applicationContext.xml中整合mybatis

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--  一   ::配置studentService对象--><bean id="studentService" class="com.itheima.service.impl.StudentServiceImpl"><property name="mapper" ref="studentMapper"/></bean><!--  二   ::配置studentMapper对象【通过mybatis动态代理】--><!--配置spring整合mybatis的bean(三个)--><!--(配置3个Bean:DruidDataSource、SqlSessionFactoryBean、MapperScannerConfigurer)--><!--1 配置DruidDataSource连接池--><!--引入外部属性文件--><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--2 配置SqlSessionFactoryBean对象,作用1:封装SqlSessionFactoryBuilder,SqlSessionFactory对象的创建作用2:加载mybatis需要的环境信息--><!--因为不需要这个类的bean对象 则可以不添加唯一标识--><bean class="org.mybatis.spring.SqlSessionFactoryBean"><!--1 必须要注入连接池对象【必须】--><property name="dataSource" ref="dataSource"/><!--2 加载MybatisConfig.xml核心配置文件,如果核心配置文件中的内容都被抽取了,那么就可以不用加载【可选】--><property name="configLocation" value="classpath:MybatisConfig.xml"/><!--3 配置别名,如果是使用注解配置SQL语句,可以不用配置别名【可选】--><property name="typeAliasesPackage" value="com.itheima.bean"/><!--4 加载映射配置文件,如果映射配置文件和mapper接口在同一个包下,并且同名,那么会自动加载【可选】--><property name="mapperLocations" value="classpath:StudentMapper.xml"/></bean><!--3 配置MapperScannerConfigurer对象,作用1:扫mapper接口所在的包,创建接口的代理对象,添加到spring容器中作用2:如果映射配置文件和mapper接口在同一个包下,并且同名,那么会自动加载对应的映射配置文件--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--扫mapper接口所在的包--><property name="basePackage" value="com.itheima.mapper"/></bean>
</beans>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/330656.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux-basic(8)linux磁盘与文件系统管理

【8.1】认识EXT2文件系统【补充】硬盘与存储设备 https://blog.csdn.net/pacosonswjtu/article/details/79811762&#xff08;1&#xff09;实际的数据都是写在具有磁性物质的磁碟盘上头&#xff0c;而读写主要是透过在机械手臂上的读取头(head)来达成。 实际运作时&#xff0c…

阿里巴巴对Java编程【应用结构】的规约

转载自 阿里巴巴对Java编程【应用结构】的规约 应用分层 1. 【推荐】图中默认上层依赖于下层&#xff0c;箭头关系表示可直接依赖&#xff0c;如&#xff1a;开放接口层可以依赖于Web 层&#xff0c;也可以直接依赖于 Service 层&#xff0c;依此类推&#xff1a;  开放接口…

spring注解注入IOC

1.创建Bean对象的注解 <1>配置自定义Bean对象注解 Component、Controller、Service、repository 用法&#xff1a;写在类上&#xff0c; 例如&#xff1a;Service 或者 Service(“userService”) 作用&#xff1a;将该类交给Spring创建对象保存到Spring容器中&#xff…

jstack命令:教你如何排查多线程问题

转载自 jstack命令&#xff1a;教你如何排查多线程问题这是之前的一个死锁案例&#xff1a;一个多线程死锁案例&#xff0c;如何避免及解决死锁问题&#xff1f;如程序中发生这样的死锁问题该如何排查呢&#xff1f;我们可以使用java自带的jstack命令进行排查。1、先在服务器运…

linux-basic(9)文件与文件系统的压缩与打包

【9.1】压缩文件的用途与技术【9.2】linux系统常见的压缩命令1&#xff09;压缩文件的扩展名大多是 tar, tar.gz, tgz, gz, Z, bz2 等&#xff1b;如下图所示&#xff1a;&#xff08;1&#xff09;Linux上常见的压缩命令就是 gzip 与 bzip2 &#xff0c;至於 compress 已经不再…

纯注解开发配置spring

1.纯注解开发【定义配置类的注解】 Confituration 表示该类是一个配置类 ComponentScan(“com.itheima”) 配置包扫描 PropertySource(“classpath:jdbc.properties”) 加载属性文件 Import(JdbcConfig.class) 加载其他配置类 2.spring整合mybatis【纯注解&#xff0c;3个…

阿里巴巴对Java编程【命名风格】的规约

转载自 阿里巴巴对Java编程【命名风格】的规约 命名风格 1. 【强制】代码中的命名均不能以下划线或美元符号开始&#xff0c;也不能以下划线或美元符号结束。 反例&#xff1a; _name / __name / $Object / name_ / name$ / Object$ 2. 【强制】代码中的命名严禁使用拼音与英文…

linux-basic(10)vim程序编辑器

【10.1】vi 与 vim【10.2】vi的使用1&#xff09;3种模式&#xff1a;模式1&#xff1a;一般模式&#xff0c; vim打开就是这种模式&#xff0c;编辑模式下 按 esc 回到一般模式&#xff1b;模式2&#xff1a;编辑模式&#xff0c;要等到你按下『i, I, o, O, a, A, r, R』等任何…

阿里巴巴对Java编程【代码格式】的规约

转载自 阿里巴巴对Java编程【代码格式】的规约 代码格式 1. 【强制】大括号的使用约定。如果是大括号内为空&#xff0c;则简洁地写成{}即可&#xff0c;不需要换行 &#xff1b; 如果是非空代码块则&#xff1a; 1 &#xff09; 左大括号前不换行。 2 &#xff09; 左大括号后…

自定义通配器导入bean对象

1.CustomerImportSelector工具类&#xff1a; /*** description : 自动导入器* author : wanYunBo* date : 2021-09-02 20:46**/ package com.itheima.config.selector;import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import o…

linux-basic(11)认识和学习bash

【11.1】认识bash这个shell1&#xff09;介绍&#xff1a;其实壳程序的功能只是提供用户操作系统的一个接口&#xff0c;因此这个壳程序需要可以呼叫其他软件才好。命令&#xff0c;包括 man, chmod, chown, vi, fdisk, mkfs 等等命令&#xff0c;这些命令都是独立的应用程序&a…

阿里巴巴对Java编程【常量定义】的规约

转载自 阿里巴巴对Java编程【常量定义】的规约 常量定义 1. 【强制】不允许任何魔法值 &#xff08; 即未经定义的常量 &#xff09; 直接出现在代码中。 反例&#xff1a; String key " Id # taobao _" tradeId; cache . put(key , value); 2. 【强制】 long 或…

动态代理-AOP

1 什么是AOP&#xff1f; Aspect Oriented Programming的缩写&#xff0c;面向切面编程&#xff0c;切面指定就是动态代理的方法&#xff0c;作用是在不改变业务层方法源代码的基础上对方法进行增强&#xff0c;底层使用的是动态代理技术&#xff0c;面向切面编程也可以理解成…

linux-basic(12)正则表达式与文件格式化处理

【12.1.1】什么是正则表达式&#xff1f; 1&#xff09;简单说&#xff1a;正则表示法就是处理字串的方法&#xff0c;他是以行为单位来进行字串的处理行为&#xff0c; 正则表达式透过一些特殊符号的辅助&#xff0c;可以让使用者轻易的达到查找、删除、替换某特定字串的处理程…

阿里巴巴对Java编程【OOP规约】的规约

转载自 阿里巴巴对Java编程【OOP规约】的规约 OOP规约 1. 【强制】避免通过一个类的对象引用访问此类的静态变量或静态方法&#xff0c;无谓增加编译器解析成本&#xff0c;直接用类名来访问即可。 2. 【强制】所有的覆写方法&#xff0c;必须加 Override 注解。 说明&#xff…

AOP切点表达式及通知类参数传递方式

1.切入点表达式的写法 execution( * com.itheima.service.impl.StudentServiceImpl.findAll(…)) //较少 execution( * com.itheima.service.impl.StudentServiceImpl.(…)) //较少 execution( * com.itheima.service.StudentService.(…)) //StudentService中的所有方法会被代…

linux-basic(13)学习shell script

【13.1】什么是shell script&#xff1f;1&#xff09;shell script 是利用 shell 的功能所写的一个『程序 (program)』&#xff0c;这个程序是使用纯文字档&#xff0c;将一些 shell 的语法与命令(含外部命令)写在里面&#xff0c; 搭配正规表示法、管线命令与数据流重导向等功…

阿里巴巴对Java编程【集合处理】的规约

转载自 阿里巴巴对Java编程【集合处理】的规约集合处理1. 【强制】关于 hashCode 和 equals 的处理&#xff0c;遵循如下规则&#xff1a; 1&#xff09; 只要重写 equals &#xff0c;就必须重写 hashCode 。 2&#xff09; 因为 Set 存储的是不重复的对象&#xff0c;依据 ha…

http请求状态码400的原因总结

会出现这个HTTP请求状态码400&#xff0c;说明这个请求是无效的&#xff0c;并没有进入后台服务器&#xff08;控制器&#xff09;里。 通常的原因&#xff1a; 前端提交的字段名称或者字段类型和后台的实体类不一样&#xff0c;或者前端提交的参数跟后台需要的参数个数不一致…

做一个完整的Java Web项目需要掌握的技能

转自&#xff1a; https://blog.csdn.net/JasonLiuLJX/article/details/51494048--------------------------------------------------------------------------------最近自己做了几个Java Web项目&#xff0c;有公司的商业项目&#xff0c;也有个人做着玩的小项目&#xff0…