Marco's Java【Spring进阶(三) 之 Spring及Mybatis集成(下)】

前言

话说上回我们讲到SSM三兄弟"桃园结义",百战百胜,一套心法(application-dao.xml),直接重组了MVC,每人占领了一个地盘,Spring大哥、SpringMVC二哥占领了View和Controller地盘,Mybatis三弟占领了Model中Dao的地盘,控制了JDBC主权,但是大哥终究还是大哥,为了能够保护好三弟,想划出一部分兵力给三弟Mybatis,但是这样的话,Mybatis就要把它的核心配置绑定到大哥的靡下,那么本章我们就来解析他们之间的"秘密"。

集成Mybatis无mybatis.config.xml

第一步,删之。
第二步,修改application-dao.xml,将Mybatis的核心配置到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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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.xsd">
		
	<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="FALLBACK"/>
	<!-- 这里的数据库连接池暂时还是使用Spring自带的DriverManagerDataSource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- 注入数据库的连接属性 -->
		<property name="driverClassName" value="${driverClassName}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	<!-- 声明sessionFactory  并注入mybatis.config.xml(mybatis基本配置文件)-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations">
			<array>
				<value>classpath:mapper/*Mapper.xml</value>
			</array>
		</property>
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageInterceptor"></bean>
			</array>
		</property>
		<property name="typeAliasesPackage" value="com.marco.domain"></property>
	</bean>
		
	<!-- 扫描mapper接口 将sqlSessionFactory注入到mapper中 实现接口和配置文件的关联 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 注入Mapper接口所在的包-->
		<property name="basePackage" value="com.marco.dao"></property>
		<!-- 注入sqlSessionFactory -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
</beans>

大家可以看到,之前的配置是不变的,主要就是sqlSessionFactory的内容改变了,好,那我们还是来分析一下,为什么要这么配置,大家还是先看这张图
在这里插入图片描述
我们发现之前用过的Resource是configLocation,这次我们使用mapperLocations,为什么要使用这个呢?
其实configuration就是解析我们之前的mybatis.config.xml,mybatis.config.xml中又关联了UserMapper.xml这个对象映射文件,那么此次我们直接掌管UserMapper.xml,因此,使用的就是mapperLocations
不难看出mapperLocations实则上是一个Resource数组,因此我们里面应该这么配置

<property name="mapperLocations">
	<array>
		<value>classpath:mapper/*Mapper.xml</value>
	</array>
</property>

接着,下面的这些配置相信大家应该猜的出来和mybatis.config.xml中的哪些配置对应起来的,我就不一一诉说啦

<property name="plugins">
	<array>
		<bean class="com.github.pagehelper.PageInterceptor"></bean>
	</array>
</property>
<property name="typeAliasesPackage" value="com.marco.domain"></property>
完善Service层

那既然到了SSM三兄弟下集我就把Service给加进来吧,还记得我们之前讲到的注解方法,实现MVC三层结构的依赖关系吗?那学以致用,我们就用上吧,说到底还是懒得写配置…

package com.marco.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.marco.dao.UserMapper;
import com.marco.domain.User;
import com.marco.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	UserMapper userMapper;
	
	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		return userMapper.deleteByPrimaryKey(id) < 0? false:true;
	}

	@Override
	public boolean insert(User user) {
		return userMapper.insert(user) < 0? false:true;
	}

	@Override
	public boolean insertSelective(User user) {
		return userMapper.insertSelective(user)< 0? false:true;
	}

	@Override
	public User selectByPrimaryKey(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean updateByPrimaryKeySelective(User user) {
		return userMapper.updateByPrimaryKeySelective(user)< 0? false:true;
	}

	@Override
	public boolean updateByPrimaryKey(User user) {
		return userMapper.updateByPrimaryKey(user)< 0? false:true;
	}
}

Ok,接下来使用scanner扫描我们的Service

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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.xsd">
	
	<import resource="classpath:application-dao.xml"/>
	<import resource="classpath:application-service.xml"/>
	<context:component-scan base-package="com.marco.service"></context:component-scan>
</beans>

至于为什么不用扫描Dao呢?之前讲过了,我们通过MapperScannerCo’n’fi’gu’rer将sqlSessionFactory注入到mapper中 实现接口和配置文件的关联,并在内存中动态的生成了代理类,因此,只用将UserService和UserDao结合起来就可以了,所以我只在ServiceImpl上写上了注解

接下来我们测试一下,由于UserService关联了UserMapper,我们直接调用UserService中的方法就可以啦

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		UserService userService = context.getBean(UserService.class);
		System.out.println(userService);
		User user = userService.selectByPrimaryKey(3);
		System.out.println(user);
	}
}

在这里插入图片描述

后语

至此为止,SSM三兄弟结义的故事我就讲完啦,老实说,虽然SSM框架处于鼎立的位置,但是不管是什么东西,总会由盛既衰,SSM框架也不例外,就目前来看SSM也逐渐被被Spring Boot框架取代,原因是随着新技术的发展,大脚本语言的新时代(Node JS,Ruby,Groovy,Scala等)到啦,Java EE使用Spring逐渐变得笨重起来,大量的XML文件存在与项目中,繁琐的配置,整合第三方框架的配置问题,低下的开发效率和部署效率等等问题。
Spring团队也开发出了相应的框架:Spring Boot。Spring Boot可以说是至少近5年来Spring乃至整个Java社区最有影响力的项目之一,也被人看作是:Java EE开发的颠覆者!
大家明显的感觉到,虽然比起之前做MVC项目的时候代码少了不少,但是配置变得越来越繁琐,部署起来也相对麻烦,后期使用Spring Boot可以说就是注解的天下了。
可以说Spring Boot就像海贼王里的路飞,继承了海贼王罗杰(Spring)的意志,在大脚本语言时代所向披靡,但并不脱离它的轨道,因此,在后续的博文里我们也会详细的讲到SpringBoot的使用,大家可以期待一下啦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值