Mybatis入门学习四:Spring 与 MyBatis整合
来源:互联网 发布:阿里云服务器推荐码 编辑:程序博客网 时间:2024/05/29 07:26
参考:
http://www.mybatis.org/mybatis-3/zh/index.html
http://www.mybatis.org/spring/zh/index.html
基于 IDEA 和 Gradle来实现。
0、准备工作
编写 build.gradle
group 'com.xiya'version '1.0-SNAPSHOT'apply plugin: 'java'apply plugin: 'idea'sourceCompatibility = 1.8repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }}dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' compile group: 'org.springframework', name: 'spring-context', version: '4.3.7.RELEASE' compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.7.RELEASE' compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.40'compile group: 'org.mybatis', name: 'mybatis', version: '3.4.4'compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4' compile group: 'log4j', name: 'log4j', version: '1.2.17' compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'}tasks.withType(JavaCompile) { options.encoding = "UTF-8"}
其中
compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'
是用来Spring和MyBatis整合的。
在cmd下执行 gradle idea 生成IDEA开发环境。
以上可参考:http://blog.csdn.net/x_iya/article/details/64442112
其他诸如log4j、数据库配置等可参考:http://blog.csdn.net/x_iya/article/details/71436381
1、编写Spring配置文件
<?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/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"> <!-- 加载数据库配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean><!-- 默认情况下Spring通过单例模式来管理bean(sqlSessionFactory),并由sqlSessionFactory生成sqlSession --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- dataSource属性指定要用到的连接池 --> <property name="dataSource" ref="dataSource"/> <!-- configLocation属性指定mybatis的核心配置文件 --> <property name="configLocation" value="mybatis-config.xml"/> </bean> <bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- sqlSessionFactory属性指定要用到的SqlSessionFactory实例 --> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <!-- mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 --> <property name="mapperInterface" value="com.xiya.dao.PersonMapper"/> </bean></beans>
mybatis-spring类包提供了一个SqlSessionFactoryBean,以便通过Spring风格创建MyBatis的SqlSessionFactory。
只需要注入数据源并指定MyBatis的总配置文件就可以了。
因为我们按照约定把Mapper接口放在:
\src\main\java\cn\bjut\mapper\PersonMapper.java
Mapper映射文件放在:src\main\resources\cn\bjut\mapper\PersonMapper.xml
所以不需要配置mapperLocations用来指定Mapper映射文件的路径。
MapperFactoryBean:根据指定的Mapper接口生成Bean实例。
当有很多Mapper接口时,使用上述方式明显低效。 可以使用MapperScannerConfigurer。
MapperScannerConfigurer:根据指定包批量扫描Mapper接口并生成实例。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.bjut.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean>
在这里只能使用sqlSessionFactoryBeanName而不能使用sqlSessionFactory,如下配置将导致jdbc.properties不能被加载。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.bjut.mapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/></bean>
使用以上自动扫描方式,自动注入的bean的名称为Mapper代理对象的类名,首字母小写。
@Testpublic void test1() { PersonMapper personMapper = (PersonMapper) ctx.getBean("personMapper"); List<Person> list = personMapper.selectAllPersons(); list.forEach(System.out::println);}
2、编写MyBatis配置文件
<?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> <!-- <mappers> <mapper resource="com/xiya/entity/PersonMapper.xml"/> </mappers> --></configuration>
原本需要在MyBatis配置的数据源等信息,我们交由Spring来管理。
此处,如果我们把PersonMapper.xml与接口PersonMapper.java放在同一目录下(Maven/Gradle管理下在src/main/java与src/main/resources有相同的目录结构),
则不再需要配置mappers属性。PersonMapper.xml相当于接口PersonMapper的实现类。
3、编写测试文件
package com.xiya.test;import com.xiya.dao.PersonMapper;import com.xiya.entity.Person;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;/** * Created by N3verL4nd on 2017/5/13. */public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonMapper personMapper = ctx.getBean(PersonMapper.class); List<Person> persons = personMapper.getAllPersons(); for (Person person : persons) { System.out.println(person); } }}
目录结构:
测试:
Demo:
https://github.com/N3verL4nd/SpringMyBatis
参考:http://www.luoshengsha.com/284.html
- Mybatis入门学习四:Spring 与 MyBatis整合
- MyBatis入门学习(Spring整合MyBatis)
- Mybatis入门实例(四)——MyBatis与Spring 3.X的整合
- Mybatis学习日记(四) --整合spring,struts2
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- spring与mybatis四种整合方法
- Mybatis学习笔记(四)——MyBatis整合spring
- Mybatis笔记之四 -- mybatis与spring整合简单例子
- mybatis学习-入门(2)-整合spring
- Mybaties入门 Mybatis与SpringMVC,spring整合
- spring与 mybatis整合 - 入门程序
- MyBatis入门(六)---mybatis与spring的整合
- Spring+Mybatis整合学习
- spring与mybatis整合
- 整合spring与myBatis
- 最简单的纯js实现点击展开二级菜单功能
- 使用python下载一些链接的软件包
- Python random模块
- 用c语言实现Linux命令ls操作
- win7 64位下安装多版本的python及tensorflow安装
- Mybatis入门学习四:Spring 与 MyBatis整合
- 进程调度模拟算法
- CentOS7 安装JDK1.8
- 设置内外网同时用
- 解决ListView复用的乱码问题
- Video Introduction to Bayesian Data Analysis, Part 1: What is Bayes?
- codeforcodeforces 808E——Selling Souvenirs(动态规划)官方题解详解
- 【ssm个人博客项目实战08】博客的分页显示以及模糊查询,删除。
- 如何传输文件到Linux服务器上