spring framework如何集成mybatis呢?
下文笔者讲述spring集成mybatis的方法分享,如下所示
我们都知道,在Spring中,如果想方便使用一个框架,只需将这个框架交给SpringIOC容器 管理,然后我们就可以非常方便的使用属性注入,应用框架中的方法及属性 那么Mybatis框架如何集成到Spring框架上呢? 下文笔者将一一道来,如下所示
mybatis官网简介
MyBatis对应的github官网(https://github.com/mybatis/spring) 里面有一个叫MyBatis Spring Adapter(MyBatis-Spring)库--我们称之为"Mybatis Spring" 她可以非常方便的将Mybatis集成到Spring中
MyBatis-Spring功能简介
springframework集成mybatis时 可使用MyBatis-Spring,这就是MyBatis-Spring的功能 可以使MyBatis的代码无缝地整合到Spring中 使用这个类库中的类 Spring将会加载必要的MyBatis工厂类和session类
Spring4.X整合MyBatis3.X初级版示例分享
1.修改pom.xml,并添加依赖 MyBatis-Spring(Spring核心,AOP,JDBC) <?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.mzj.springframework</groupId> <artifactId>springframework-mybatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springframework-mybatis Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.java265.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>4.3.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--log4j日志包 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.6.1</version> </dependency> <!-- mybatis ORM框架 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- JUnit单元测试工具 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <!--mybatis-spring适配器 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--Spring框架核心库 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- aspectJ AOP 织入器 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!--Spring java数据库访问包,在本例中主要用于提供数据源 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>springframework-mybatis</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project> 2.创建Spring上下文初始化配置文件 该文件取名为ApplicationContext.xml 主要原因是“约束优于配置”的理由 使用Web监听器加载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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!--1定义一个jdbc数据源,创建一个驱动管理数据源的bean --> <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.8.8:3306/test" /> <property name="username" value="java265" /> <property name="password" value="java265" /> </bean> <!--2创建一个sql会话工厂bean,指定数据源--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="jdbcDataSource" /><!-- 指定数据源 --> <property name="configLocation" value="classpath:MyBatisCfg.xml"></property> <!-- 指定配置文件 --> </bean> <!--3创建一个UserTypeDAO--> <bean id="userTypeDAO" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!--指定映射文件 --> <property name="mapperInterface" value="com.java265.springframework.mybatis.mapping.UserTypeDAO"></property> <!-- 指定sql会话工厂--> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!--下面的配置暂时未使用 --> <context:component-scan base-package="com.java265"> </context:component-scan> <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>
测试运行
package com.java265.springframework.mybatis.dao; import com.java265.springframework.mybatis.entities.UserType; import com.java265.springframework.mybatis.mapping.UserTypeDAO; import org.junit.Test; import static org.junit.Assert.assertNotNull; import java.util.list; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMyBatisSpring01 { @Test public void test01() { //初始化容器 ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml"); //获得bean UserTypeDAO userTypeDAO=ctx.getBean("userTypeDAO",UserTypeDAO.class); //访问数据库 List<UserType> usertypes=userTypeDAO.getAllUserTypes(); for (UserType userType : usertypes) { System.out.println(userType); } assertNotNull(userTypes); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。