spring整合mybatis的流程步骤
spring整合mybatis
创建流程
1.创建项目
导入依赖
POM文件
<?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.lx.spring</groupId> <artifactId>03-spring-mybatis</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- spring家族依赖 创建spring全家桶尽量版本号一致,防止加载不必要的插件及测试出错 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.10.RELEASE</version> </dependency> <!-- MyBatis连接数据依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- mybatis整合spring依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies></project>
2.编写mybatis核心配置文件(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标签用来配置MyBatis的运行环境 --> <settings> <setting name="logImpl" value="LOG4J"/> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 配置别名: 作用就是方便!--> <typeAliases> <!-- typeAlias: 给某一个具体的类,配置别名--> <!-- <typeAlias type="com.qf.mybatis.entity.User" alias="user" />--> <!-- package: 是扫描一个包下的全部类,这个包的类,在映射文件中就可以直接写类名--> <package name="com.qf.air.entity"/> </typeAliases> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 分页插件 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> </plugin> </plugins></configuration>
3.编写mybatis的ORM配置(实体类、数据库、mapper文件)
根据自己需求创建对应的文件
4.编写spring整合mybatis的配置文件
头文件(applicationContext-mybatis.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: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 https://www.springframework.org/schema/context/spring-context.xsd"><!-- 只要在pom文件中导好依赖,就可在新建XML configuration File里面找到对应的头文件-->
①加载外部的properties文件
<!-- 加载外部的properties --><context:property-placeholder location="classpath:database.properties"></context:property-placeholder>
②创建数据源对象
<!-- 创建数据源对象--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
③创建SqlSessionFactoryBean
<!--sqlSessionFactoryBean--><!-- 因为它不能自己new对象需要交给spring--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入Datasource--> <property name="dataSource" ref="dataSource"></property><!-- 注入mybatis的核心配置文件--> <property name="configLocation" value="mybatis-config.xml"></property><!-- 注入mybatis的映射配置文件mapper--> <property name="mapperLocations"> <array> <value>classpath*:mapper/*.xml</value> </array> </property> </bean>
④创建扫描仪(扫描接口所在的包)
<!-- 创建对象扫描接口所在路径--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 注入mapper接口所在路径--> <property name="basePackage" value="com.lx.mapper"></property> </bean>
赞 (0)