本文主要是介绍Spring-mybtis整合过程详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、创建完整包 下载地址:01MyBatis基础代码.zip-Web开发文档类资源-CSDN下载
二、开始整合
1、方式1
step1:导入相关坐标
<dependencies>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.13</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.13</version></dependency><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency></dependencies>
step2:修改daoImpl中代码
修改serviceImpl中代码
step3:创建bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="db.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/>
</bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean><bean id="categoryDao" class="com.woniuxy.dao.impl.CategoryDaoImpl"><property name="sqlsessionFactory" ref="sqlSessionFactory"/></bean><bean id="categoryService" class="com.woniuxy.service.impl.CategoryServiceImpl"><property name="categoryDao" ref="categoryDao"/></bean></beans>
step4:编写测试代码
import com.woniuxy.model.Category;
import com.woniuxy.service.CategoryService;
import com.woniuxy.service.impl.CategoryServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;public class TestCategoryService {private static CategoryService categoryService = new CategoryServiceImpl();public static void main(String[] args) {testFindAllCategories();}private static void testFindAllCategories() {ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:bean.xml");CategoryService categoryService = (CategoryServiceImpl)ac.getBean("categoryService");List<Category> categories = categoryService.findAllCategories();for(Category category : categories){System.out.println(category.getName());}}
}
step5:测试结果
方式2:使用SqlSessionTemplate
基于以上代码
step1:修改daoImpl代码 注意:不需要提交commit()和close()的事务提交,因为spring容器已经帮你做了
是step2:修改bean.xml
step3:测试代码和上面一致,结果如下
3、方式3 基于以上代码修改
step1:修改daoImpl
step2:修改bean.xml
step3:测试代码一样,测试结果
4、方式4 可利用MapperFactoryBean创建Mapper
基于以上代码
step1:修改daoImpl
step2:修改bean.xml
step3:测试代码和测试结果同上
4、最终解决方案 基于以上代码
step1:
step2:修改bean.xml
step3:删除dao整个包
step4:测试代码同上和测试结果
这篇关于Spring-mybtis整合过程详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!