Spring框架集成mybatis框架的配置(笔记)

<!-- 0.注解扫描 -->
<!-- 1.导入外部文件 -->
<!-- 2.数据源 -->
<!-- 3.session Factory -->
<!-- 4.事务模板 -->
<!-- 5.AOP相关配置 -->
<!-- Mapper扫描 -->

<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
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/jee http://www.springframework.org/schema/jee/spring-jee.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">

<!-- 0.注解扫描 上下文 构成 扫描 -->
<context:component-scan base-package="com.wxzj.crm"></context:component-scan>
<!-- 1.导入外部文件 上下文 属性 占位 位置-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.数据源 ctrl shift T 搜索datasource 告诉spring初始化时初始化链接,销毁时关闭链接
同时需要配置参数,驱动的名字,链接,账号,密码 name value ${}(在尖括号里写)

-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.session Factory mybatis获取session的工场是SqlSessionFactoryBean
里面需要配置一个数据库源
和一个 配置config 源location 指向mybatis的主配置文件
第三个给每一个类起一个别名,这是为啥
找mapper 这次找的是路径 选择路径下的所有mapper (映射)
-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
<property name="typeAliasePackage" value="com.wxzj.crm.domain"></property>
<property name="mapperLocations" value="com/wxzj/crm/mapper/*Mapper.xml"></property>
</bean>
<!-- 事务管理器 transaction事务的意思 给他配置数据源,让他在这个数据源上建立事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.事务模板 他自己没有事务,得用别人的,这次用阿里巴巴druid 他需要的事务管理器的名字叫做transaction-manager,默认引用

advice: n. 建议;忠告;劝告;通知 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<!-- 配置每一个方法的专属配置 attributes 属性 -->
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 5.AOP 切面相关配置 和事务一块使用的,他是面向切面编程的配置
事务(id=advice)配置的是怎么管理事务,AOP配置的是在哪执行事务
如果只配置事务不配置aop的话应该是没用的
pointcut切入点 execution执行-->
<aop:config>
<aop:pointcut expression="execution(* com.xyz.myapp.service.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointCut"/>
</aop:config>
<!-- Mapper扫描 配置个mapper扫描的对象,让他去扫描mapper ,这应该是spring整合其他框架的手法-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wxzj.crm.mapper"></property>
</bean>

</beans>

时间: 2024-07-29 17:01:38

Spring框架集成mybatis框架的配置(笔记)的相关文章

Spring Boot集成MyBatis实现通用Mapper

前言 MyBatis关于MyBatis,大部分人都很熟悉.MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录.不管是DDD(Domain Driven Design,领域驱动建模)还是分层架构的风

Spring Boot 集成 MyBatis(四)

Spring Boot 集成 MyBatis A.ORM框架是什么? 对象关系映射(Object Relational Mapping,简称 ORM)模式是一种为了解决面向对象与关系数据库存在的 互不匹配的现象技术.简单的说,ORM 是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自 动持久化到关系数据库中. B.为什么需要ORM框架 当开发一个应用程序的时候(不使用 O/R Mapping),可能会写不少数据访问层的代码,用来从数据库保存. 删除.读取对象信息等.在 DAL 中写了很

SpringBoot集成MyBatis的Bean配置方式

SpringBoot集成MyBatis的Bean配置方式 SpringBoot是一款轻量级开发的框架,简化了很多原先的xml文件配置方式,接下来就介绍一下如何不适用XML来配置Mybatis springboot的yml文件 spring: profiles: active: dev application: name: service-fishkk ##MySql datasource: url: jdbc:mysql://47.94.200.0:3306/mybatis?useSSL=fal

spring boot集成MyBatis 通用Mapper 使用总结

spring boot集成MyBatis 通用Mapper 使用总结 2019年 参考资料: Spring boot集成 MyBatis 通用Mapper SpringBoot框架之通用mapper插件(tk.mybatis) spring boot 如何优雅的使用mybatis-spring-boot-starter 三分钟让你看懂Springboot 中 Mybatis 的使用 Spring Boot 集成MyBatis__动力节点 ===================== end 原文地

Spring Boot 集成MyBatis

Spring Boot 集成MyBatis 在集成MyBatis前,我们先配置一个druid数据源. Spring Boot 系列 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Boot 静态资源处理 Spring Boot - 配置排序依赖技巧 Spring Boot - DevTools 介绍 Spring Boot 集成druid druid有非常多个配置选项,使用Spring Boot 的配置文件能够方便的

spring boot集成mybatis 自动生成实体类和mapper文件、Dao层

1.创建spring boot集成mybatis请见 2.在resources目录下新键mybatis-generator文件夹,并在文件夹中新键mybatis-generatorConfig.xml文件和mybatis-generatorinit.properties两个文件 mybatis-generatorinit.properties jdbc_driver=oracle.jdbc.driver.OracleDriver jdbc_url=jdbc:oracle:thin:@loclho

Spring集成MyBatis框架

Java在写数据库查询时,我接触过四种方式: 1.纯Java代码,引用对应的数据库驱动包,自己写连接与释放逻辑(可以用连接池) 这种模式实际上性能是非常不错的,但是使用起来并不是非常方便:一是要手工为Connection做获取与释放,大量的冗余代码也容易出错:另一个是,复杂的SQL用字符串写起来简直不可维护(换行.可视长度.参数都是问题). 2.使用Spring JdbcTemplate 这个其实还是挺不错的,配置比较简单,功能丰富上比手工管理Connection要舒服多了,而且代码也比较简洁.

IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

项目创建 本项目使用的是IDEA 2016创建.项目使用Spring 4.2.6,Mybatis3.4.0,Tomcat使用的是Tomcat8,数据库为MySQL. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId,GroupId在公司中一般都是域名的逆序,ArtifactId用来标明该项目是用来做什么的,接着下一步. 添加一个archetypeCatalog,值为internal可以加速项目的创

spring、spring mvc、mybatis框架整合基本知识

学习了一个多月的框架知识了,这两天很想将它整合一下.网上看了很多整合案例,基本都是基于Eclipse的,但现在外面公司基本都在用Intellij IDEA了,所以结合所学知识,自己做了个总结,有不足之处欢迎指正. 首先,我是参考了http://blog.csdn.net/zhshulin/article/details/37956105这篇做的Intellij IDEA翻版.Intellij IDEA的许多操作方式与习惯与eclipse区别很大,所以很容易走入误区.直接上操作吧. 1.基本概念