mybatis和spring的整合
Mybatis与Spring的集成
1.配置Spring环境
创建maven工程
pom.xml导入依赖
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>cn.itcast.parent</groupId>
- <artifactId>itcast-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>cn.itcast.mybatis</groupId>
- <artifactId>itcast-mybatis-spring</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <dependencies>
- <!-- 单元测试 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- Mybatis -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- </dependency>
- <!-- MySql -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </dependency>
- <dependency>
- <groupId>cglib</groupId>
- <artifactId>cglib</artifactId>
- <version>3.1</version>
- </dependency>
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper</artifactId>
- <version>3.7.5</version>
- </dependency>
- <dependency>
- <groupId>com.github.jsqlparser</groupId>
- <artifactId>jsqlparser</artifactId>
- <version>0.9.1</version>
- </dependency>
- <!-- 整合 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- </dependency>
- <!-- 连接池 -->
- <dependency>
- <groupId>com.jolbox</groupId>
- <artifactId>bonecp-spring</artifactId>
- <version>0.8.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- </dependency>
- </dependencies>
- </project>
创建applicationContext.xml文件
jdbc.properties
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis_0716?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
- jdbc.username=root
- jdbc.password=123456
applicationContext.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"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 使用spring自带的占位符替换功能 -->
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <!-- 允许JVM参数覆盖 -->
- <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
- <!-- 忽略没有找到的资源文件 -->
- <property name="ignoreResourceNotFound" value="true" />
- <!-- 配置资源文件 -->
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
- destroy-method="close">
- <!-- 数据库驱动 -->
- <property name="driverClass" value="${jdbc.driver}" />
- <!-- 相应驱动的jdbcUrl -->
- <property name="jdbcUrl" value="${jdbc.url}" />
- <!-- 数据库的用户名 -->
- <property name="username" value="${jdbc.username}" />
- <!-- 数据库的密码 -->
- <property name="password" value="${jdbc.password}" />
- <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
- <property name="idleConnectionTestPeriod" value="60" />
- <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
- <property name="idleMaxAge" value="30" />
- <!-- 每个分区最大的连接数 -->
- <property name="maxConnectionsPerPartition" value="150" />
- <!-- 每个分区最小的连接数 -->
- <property name="minConnectionsPerPartition" value="5" />
- </bean>
- </beans>
applicationContext-mybatis.xml(SqlSessionFactory)
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 定义Mybatis的SqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 定义数据源 -->
- <property name="dataSource" ref="dataSource" />
- <!-- 指定mybatis全局配置文件 -->
- <property name="configLocation" value="classpath:mybatis-config.xml"></property>
- <!-- 扫描mappers目录以及子目录下的所有xml文件 -->
- <property name="mapperLocations" value="classpath:mappers/**/*.xml" />
- <!-- 别名扫描包 -->
- <property name="typeAliasesPackage" value="cn.itcast.mybatis.pojo"/>
- </bean>
- </beans>
mybatis-config.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <settings>
- <!-- 开启驼峰自动映射 -->
- <setting name="mapUnderscoreToCamelCase" value="true" />
- </settings>
- </configuration>
单元测试:
UserMapperTest
- package cn.itcast.mybatis.mapper;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.itcast.mybatis.pojo.User;
- public class UserMapperTest {
- private UserMapper userMapper;
- @Before
- public void setUp() throws Exception {
- // 初始化SPring容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml",
- "applicationContext-mybatis.xml");
- //从容器中获取SqlSessionFactory
- SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- this.userMapper = sqlSession.getMapper(UserMapper.class);
- }
- @Test
- public void testQueryUserById() {
- User user = this.userMapper.queryUserById(1L);
- System.out.println(user);
- }
- }
测试成功:
注意:我的mysql数据库用户名和密码是root root
所以要修改jdbc.properties设置
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
- jdbc.username=root
- jdbc.password=root
所以对应的改成:
定义Mapper接口
在Spring容器中定义Mapper接口,无需手动获取SqlSessionFactory获取Session以及Mapper,只需要从Spring容器中获取即可。
配置Mapper接口的自动扫描器
多个包怎么配置,可以用逗号
通配符配置Mapper.xml和别名扫描包
mybatis和spring的整合的更多相关文章
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- 由“单独搭建Mybatis”到“Mybatis与Spring的整合/集成”
在J2EE领域,Hibernate与Mybatis是大家常用的持久层框架,它们各有特点,在持久层框架中处于领导地位. 本文主要介绍Mybatis(对于较小型的系统,特别是报表较多的系统,个人偏向Myb ...
- SSM :MyBatis与Spring的整合
MyBatis与Spring的整合 一:Spring整合MyBatis的准备工作: (1.)在项目中加入Spring,ByBatis及整合相关的jar文件 (2.)建立开发目录结构,创建实体类 (3. ...
- mybatis与spring的整合(代码实现)
mybatis与spring的整合(代码实现) 需要jar包: mybatis核心包:依赖包:log4j包:spring croe;beans;tx;aop;aspects;context;expre ...
- Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例
Java基础-SSM之Spring和Mybatis以及Spring MVC整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,详细你已经有一定的Java ...
- mybatis 学习笔记(四):mybatis 和 spring 的整合
mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...
- MyBatis与Spring的整合实例详解
从之前的代码中可以看出直接使用 MyBatis 框架的 SqlSession 访问数据库并不简便.MyBatis 框架的重点是 SQL 映射文件,为方便后续学习,本节讲解 MyBatis 与 Spri ...
- mybatis与spring的整合
今天是mybatis的最后一天,也是最为重要的一天,mybatis与spring整合,(spring相关知识我会抽一个大的模块进行讲解). 首先加入Spring的依赖 <dependency&g ...
- Java框架搭建-Maven、Mybatis、Spring MVC整合搭建
1. 下载eclipse 到网站下载 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/marsr 选择 ...
随机推荐
- websocket聊天体验(二)
上一篇说到后续可以支持:最近历史.表情+图片,顺便还实现了简易的音频和视频.暂时没有实现实时语音对讲,有待后续再研究.点开在线聊天页面,即可看到最近历史记录(18条). 聊天的底层数据都是基于txt文 ...
- Python学习笔记—Dict和set
dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子,假设要根据同学的名字 ...
- 只需体验三分钟,你就会跟我一样,爱上这款Toast
只需体验三分钟,你就会跟我一样,爱上这款Toast https://www.jianshu.com/p/9b174ee2c571
- LC 537. Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- ASP.Net Core承载外部程序集
故事背景 一般情况下ASP.Net Core项目配置可以直接在appsetting.json中添加,也可以在项目中添加新的配置文件.但如果想和其他项目一起实现配置文件通用呢?我们可以用绝对定位去访 ...
- JAVA记事本的图形用户界面应用程序含过滤
JAVA记事本的图形用户界面应用程序 过滤 题目简介: 整体分析: 实验代码: package note; import java.awt.EventQueue; import java.awt.ev ...
- 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 ...
- java:redis(java代码操作redis,实体类mapper生成器(generator))
1.redis_demo Maven ItemMapper.xml: <?xml version="1.0" encoding="UTF-8" ?> ...
- Git-T
或在命令行上创建一个新的存储库echo“#gittest”>> README.md git init git add README.md git commit -m“first commi ...
- 【VS开发】【图像处理】直方图均衡与平台直方图
目录(?)[-] 直方图均衡化Histogram Equalization 直方图均衡化的主要过程 一个简单的例子 关键的代码实现 平台直方图及均衡化 平台直方图的概念 平台阈值的确定 关键代码实现 ...