- Spring集成Mybatis
- 1.添加jar包(pom.xml)
- 2.配置sqlSessionFactiory(spring.xml)
- 3.再service類中注入Dao代理接口
- 4.測試類
- 5文件結構
Spring集成Mybatis
Spring集成Mybatis其核心是將SqlSessionFactory交由Spring管理,并由 Spring管理對dao接口的代理實現。 導入mybatisjar包
創建新的ssm項目為例文件結構如下
1.添加jar包(pom.xml)
<?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>org.example</groupId><artifactId>ssm</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.2.RELEASE</version></dependency><!-- spring-jdbc--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.2.RELEASE</version></dependency><!-- 阿里數據源--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!-- mysql驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.2</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency></dependencies>
</project>
2.配置sqlSessionFactiory(spring.xml)
<!-- spring管理生成SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis.xml"></property>
<!-- 指定配置文件--><property name="mapperLocations" value="classpath:mappers/*Mapper.xml">
<!-- 指定文件地址--></property></bean>
生成接口代理(spring.xml)
<!-- spring管理生成接口代理對象--><bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.example.dao"></property>
<!-- 對指定包下的接口進行掃描,并生成接口代理的對象--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
完整spring.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
"><!-- 開啟注解掃描 對指定包下的注解進行掃描 ,檢查添加spring類注解標簽的類--><context:component-scan base-package="org.example" ></context:component-scan><context:property-placeholder location="config.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!-- 方法一:通過配置文件配置數據源--><!-- <property name="driverClassName" value="com.mysql.jdbc.cj.Driver"/>--><!-- <property name="url" value="數據庫連接/>--><!-- <property name="username" value="root"/>--><!-- <property name="password" value="root"/>--><!-- <property name="initialSize" value="1"/>--><!-- <property name="maxActive" value="1"/>--><!-- 方法二:通過注解配置數據源--><property name="driverClassName" value="${driverClassName}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/><property name="initialSize" value="${initialSize}"/><property name="maxActive" value="${maxActive}"/></bean>
<!-- spring管理生成SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis.xml"></property>
<!-- 指定配置文件--><property name="mapperLocations" value="classpath:mappers/*Mapper.xml">
<!-- 指定文件地址--></property></bean>
<!-- spring管理生成接口代理對象--><bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.example.dao"></property>
<!-- 對指定包下的接口進行掃描,并生成接口代理的對象--><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
</beans>
注:對應的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><!-- 打印日志--><setting name="logImpl" value="STDOUT_LOGGING"/><!-- 開啟駝峰命名規則--><setting name="mapUnderscoreToCamelCase" value="true"/></settings><!-- 為類配置起別名--><typeAliases><!-- 下面別名怎么引用呢? 直接在xml文件中使用別名就可以了--><typeAlias type="org.example.model.Admin" alias="Admin"/></typeAliases>
</configuration>
3.再service類中注入Dao代理接口
@Service("loginService")
public class LoginService {@AutowiredLoginDao loginDao;public Admin login(Admin admin){return loginDao.login(admin);}
}
4.測試類
package org.example.test;import org.example.model.Admin;
import org.example.service.LoginService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");Admin admin = context.getBean("admin", Admin.class);admin.setAccount("admin");admin.setPassword("111");LoginService loginService = context.getBean("loginService", LoginService.class);Admin result = loginService.login(admin);System.out.println(result);}
}