Mybatis与Spring的集成

1.配置Spring环境

创建maven工程

pom.xml导入依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>cn.itcast.parent</groupId>
  6. <artifactId>itcast-parent</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. </parent>
  9. <groupId>cn.itcast.mybatis</groupId>
  10. <artifactId>itcast-mybatis-spring</artifactId>
  11. <version>1.0.0-SNAPSHOT</version>
  12.  
  13. <dependencies>
  14. <!-- 单元测试 -->
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <scope>test</scope>
  19. </dependency>
  20. <!-- Mybatis -->
  21. <dependency>
  22. <groupId>org.mybatis</groupId>
  23. <artifactId>mybatis</artifactId>
  24. </dependency>
  25. <!-- MySql -->
  26. <dependency>
  27. <groupId>mysql</groupId>
  28. <artifactId>mysql-connector-java</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.slf4j</groupId>
  32. <artifactId>slf4j-log4j12</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>cglib</groupId>
  36. <artifactId>cglib</artifactId>
  37. <version>3.1</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>com.github.pagehelper</groupId>
  41. <artifactId>pagehelper</artifactId>
  42. <version>3.7.5</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>com.github.jsqlparser</groupId>
  46. <artifactId>jsqlparser</artifactId>
  47. <version>0.9.1</version>
  48. </dependency>
  49.  
  50. <!-- 整合 -->
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-context</artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.mybatis</groupId>
  57. <artifactId>mybatis-spring</artifactId>
  58. </dependency>
  59.  
  60. <!-- 连接池 -->
  61. <dependency>
  62. <groupId>com.jolbox</groupId>
  63. <artifactId>bonecp-spring</artifactId>
  64. <version>0.8.0.RELEASE</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-jdbc</artifactId>
  69. </dependency>
  70. <dependency>
  71. <groupId>org.springframework</groupId>
  72. <artifactId>spring-aspects</artifactId>
  73. </dependency>
  74. </dependencies>
  75. </project>

创建applicationContext.xml文件

jdbc.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis_0716?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
  3. jdbc.username=root
  4. jdbc.password=123456

applicationContext.xml(配置资源文件替换器、配置数据源)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <!-- 使用spring自带的占位符替换功能 -->
  11. <bean
  12. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  13. <!-- 允许JVM参数覆盖 -->
  14. <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
  15. <!-- 忽略没有找到的资源文件 -->
  16. <property name="ignoreResourceNotFound" value="true" />
  17. <!-- 配置资源文件 -->
  18. <property name="locations">
  19. <list>
  20. <value>classpath:jdbc.properties</value>
  21. </list>
  22. </property>
  23. </bean>
  24.  
  25. <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
  26. destroy-method="close">
  27. <!-- 数据库驱动 -->
  28. <property name="driverClass" value="${jdbc.driver}" />
  29. <!-- 相应驱动的jdbcUrl -->
  30. <property name="jdbcUrl" value="${jdbc.url}" />
  31. <!-- 数据库的用户名 -->
  32. <property name="username" value="${jdbc.username}" />
  33. <!-- 数据库的密码 -->
  34. <property name="password" value="${jdbc.password}" />
  35. <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
  36. <property name="idleConnectionTestPeriod" value="60" />
  37. <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
  38. <property name="idleMaxAge" value="30" />
  39. <!-- 每个分区最大的连接数 -->
  40. <property name="maxConnectionsPerPartition" value="150" />
  41. <!-- 每个分区最小的连接数 -->
  42. <property name="minConnectionsPerPartition" value="5" />
  43. </bean>
  44.  
  45. </beans>

applicationContext-mybatis.xml(SqlSessionFactory)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <!-- 定义Mybatis的SqlSessionFactory -->
  11. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  12. <!-- 定义数据源 -->
  13. <property name="dataSource" ref="dataSource" />
  14. <!-- 指定mybatis全局配置文件 -->
  15. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  16.  
  17. <!-- 扫描mappers目录以及子目录下的所有xml文件 -->
  18. <property name="mapperLocations" value="classpath:mappers/**/*.xml" />
  19.  
  20. <!-- 别名扫描包 -->
  21. <property name="typeAliasesPackage" value="cn.itcast.mybatis.pojo"/>
  22. </bean>
  23.  
  24. </beans>

mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.  
  7. <settings>
  8. <!-- 开启驼峰自动映射 -->
  9. <setting name="mapUnderscoreToCamelCase" value="true" />
  10. </settings>
  11.  
  12. </configuration>

单元测试:

UserMapperTest

  1. package cn.itcast.mybatis.mapper;
  2.  
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9.  
  10. import cn.itcast.mybatis.pojo.User;
  11.  
  12. public class UserMapperTest {
  13.  
  14. private UserMapper userMapper;
  15.  
  16. @Before
  17. public void setUp() throws Exception {
  18. // 初始化SPring容器
  19. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml",
  20. "applicationContext-mybatis.xml");
  21. //从容器中获取SqlSessionFactory
  22. SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
  23. SqlSession sqlSession = sqlSessionFactory.openSession(true);
  24. this.userMapper = sqlSession.getMapper(UserMapper.class);
  25. }
  26.  
  27. @Test
  28. public void testQueryUserById() {
  29. User user = this.userMapper.queryUserById(1L);
  30. System.out.println(user);
  31. }
  32.  
  33. }

测试成功:

注意:我的mysql数据库用户名和密码是root root

所以要修改jdbc.properties设置

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
  3. jdbc.username=root
  4. jdbc.password=root

所以对应的改成:

定义Mapper接口

在Spring容器中定义Mapper接口,无需手动获取SqlSessionFactory获取Session以及Mapper,只需要从Spring容器中获取即可。

配置Mapper接口的自动扫描器

多个包怎么配置,可以用逗号

通配符配置Mapper.xml和别名扫描包

mybatis和spring的整合的更多相关文章

  1. 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】

    一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...

  2. 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”

    在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...

  3. SSM :MyBatis与Spring的整合

    MyBatis与Spring的整合 一:Spring整合MyBatis的准备工作: (1.)在项目中加入Spring,ByBatis及整合相关的jar文件 (2.)建立开发目录结构,创建实体类 (3. ...

  4. mybatis与spring的整合(代码实现)

    mybatis与spring的整合(代码实现) 需要jar包: mybatis核心包:依赖包:log4j包:spring croe;beans;tx;aop;aspects;context;expre ...

  5. Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例

    Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...

  6. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

  7. MyBatis与Spring的整合实例详解

    从之前的代码中可以看出直接使用 MyBatis 框架的 SqlSession 访问数据库并不简便.MyBatis 框架的重点是 SQL 映射文件,为方便后续学习,本节讲解 MyBatis 与 Spri ...

  8. mybatis与spring的整合

    今天是mybatis的最后一天,也是最为重要的一天,mybatis与spring整合,(spring相关知识我会抽一个大的模块进行讲解). 首先加入Spring的依赖 <dependency&g ...

  9. Java框架搭建-Maven、Mybatis、Spring MVC整合搭建

    1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr 选择 ...

随机推荐

  1. websocket聊天体验(二)

    上一篇说到后续可以支持:最近历史.表情+图片,顺便还实现了简易的音频和视频.暂时没有实现实时语音对讲,有待后续再研究.点开在线聊天页面,即可看到最近历史记录(18条). 聊天的底层数据都是基于txt文 ...

  2. Python学习笔记—Dict和set

    dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子,假设要根据同学的名字 ...

  3. 只需体验三分钟,你就会跟我一样,爱上这款Toast

    只需体验三分钟,你就会跟我一样,爱上这款Toast https://www.jianshu.com/p/9b174ee2c571

  4. LC 537. Complex Number Multiplication

    Given two strings representing two complex numbers. You need to return a string representing their m ...

  5. ASP.Net Core承载外部程序集

    故事背景   一般情况下ASP.Net Core项目配置可以直接在appsetting.json中添加,也可以在项目中添加新的配置文件.但如果想和其他项目一起实现配置文件通用呢?我们可以用绝对定位去访 ...

  6. JAVA记事本的图形用户界面应用程序含过滤

    JAVA记事本的图形用户界面应用程序 过滤 题目简介: 整体分析: 实验代码: package note; import java.awt.EventQueue; import java.awt.ev ...

  7. ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] - Data truncation: Incorrect datetime value: '' for column 'pubdate' at row 1

    之前的Connector/J版本是:mysql-connector-java-5.0.4-bin.jar 后来换成mysql-connector-java-5.1.45-bin.jar,问题解决 20 ...

  8. java:redis(java代码操作redis,实体类mapper生成器(generator))

    1.redis_demo Maven  ItemMapper.xml: <?xml version="1.0" encoding="UTF-8" ?> ...

  9. Git-T

    或在命令行上创建一个新的存储库echo“#gittest”>> README.md git init git add README.md git commit -m“first commi ...

  10. 【VS开发】【图像处理】直方图均衡与平台直方图

    目录(?)[-] 直方图均衡化Histogram Equalization 直方图均衡化的主要过程 一个简单的例子 关键的代码实现 平台直方图及均衡化 平台直方图的概念 平台阈值的确定 关键代码实现 ...