Spring4增长了对泛型注入的支持,这个特性对通用Mapper来讲,很是的有用,能够说有了这个特性,能够直接在Service中写Mapper<UserInfo> mapper
,能够经过BaseService<T>
来实现通用的Service
。html
这篇文档主要讲解通用Mapper在Spring4中的**最佳用法**。java
和其余里面配置的区别就是在Spring4中能够配置通用Mapper这个类,咱们能够把通用Mapper中提供的Mapper<T>
配置到Spring中,若是你有本身实现的通用Mapper,也能够这么配置:git
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper,com.isea533.mybatis.mapperhelper"/> </bean>
这里在配置basePackage
的时候,将通用Mapper<T>
所在的包com.isea533.mybatis.mapperhelper
也配置上了。这样就能在Spring4中直接注入Mapper<T>
。github
另外就是通用Mapper自身的配置:spring
<bean class="com.isea533.mybatis.mapperhelper.MapperHelper" depends-on="sqlSession" init-method="initMapper" scope="singleton" lazy-init="false"> <property name="mappers"> <array> <value>com.isea533.mybatis.mapperhelper.Mapper</value> </array> </property> <property name="sqlSessions" ref="sqlSession"/> </bean>
这里的配置和Spring3没什么区别,另外须要保证有sqlSession
,能够按以下配置:sql
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean>
其余的配置按照一般的方法进行配置便可,没有特殊的地方,若是有人不明白完整的配置什么样,能够看下面的例子:数据库
applicationContext.xmlmybatis
Mapper<T>
实现本身的实体接口类这里以Country2Mapper
为例:app
public interface Country2Mapper extends Mapper<Country2> { //省略其余本身增长的方法 }
若是你点进去上面的Country2Mapper
查看,会发现里面还有一些Example
的方法,这些是代码生成器生成的,生成的方法不包含通用的CRUD,只有Example
的方法,还有一个对应的Country2Mapper.xml
。
这个例子主要说明,除了通用Mapper的方法外,你能够添加本身的方法,和原来的没有区别。
这里的实体Country2
代码以下:
@Table(name="country") public class Country2 { @Id private Integer id; private String countryname; private String countrycode; //省略getter和setter方法 }
这里配置对应的表名为country
。只有一个主键id
。
在Service中的使用方式有不少种。
Country2Mapper
@Service public class DemoService { @Autowired private Country2Mapper mapper; public List<Country2> selectPage(int pageNum,int pageSize){ PageHelper.startPage(pageNum, pageSize); //Spring4支持泛型注入 return mapper.select(null); } }
这种方式太常见,太普通,这里很少解释。
这种方式用的就不多了,可是Spring4支持泛型注入,所以在第一种的基础上,咱们能够写出以下的代码:
@Service public class DemoService { @Autowired private Mapper<Country2> mapper; public List<Country2> selectPage(int pageNum,int pageSize){ //这里用到了分页插件PageHelper PageHelper.startPage(pageNum, pageSize); //Spring4支持泛型注入 return mapper.select(null); } }
对于不了解泛型注入的,可能会不习惯Mapper<Country2> mapper
这种写法,实际上这么写的优点并不明显。还不如第一种明确。
可是经过第二种,咱们能够引出第三种,也可能会是很经常使用的通用Service。
通常操做数据库都在Service
中进行,不可避免的就要写出大量重复的CRUD方法,若是能有一个通用的Service
,确定也会减小不少工做量。
这里经过简单扩展来说,更复杂的封装,各位能够根据本身的状况动手实践。
以下简单例子:
@Service public abstract class BaseService<T> { @Autowired protected Mapper<T> mapper; public int save(T entity){ return mapper.insert(entity); } public int delete(T entity){ return mapper.deleteByPrimaryKey(entity); } /** * 单表分页查询 * * @param pageNum * @param pageSize * @return */ public List<T> selectPage(int pageNum,int pageSize){ PageHelper.startPage(pageNum, pageSize); //Spring4支持泛型注入 return mapper.select(null); } }
建立如上所示的抽象类BaseService<T>
,这里封装三个方法仅做为简单的例子。须要更复杂逻辑的能够自行摸索。
而后修改刚才的DemoService
例子:
@Service public class DemoService extends BaseService<Country2>{ }
因为BaseService<T>
封装了单表的分页插件,所以目前的DemoService
中没有任何代码。
假如咱们要增长一个包含校验的保存方法。添加以下代码:
@Service public class DemoService extends BaseService<Country2>{ public int save(Country2 country2) { if (country2 == null) { throw new NullPointerException("保存的对象不能为空!"); } if (country2.getCountrycode() == null || country2.getCountrycode().equals("")) { throw new RuntimeException("国家代码不能为空!"); } if (country2.getCountryname() == null || country2.getCountryname().equals("")) { throw new RuntimeException("国家名称不能为空!"); } return super.save(country2); } }
上面只是个例子,是否抛出异常各位不用计较。
从这个例子应该也能看到,当使用Spring4和通用Mapper的时候,是多么的方便。
Mapper<T>
我一开始为何要设计为必须继承Mapper<T>
实现本身的Mapper
呢?
主要考虑到两个方面。
经过<T>
能够方便的获取泛型的类型,在通用的方法中就不须要传递实体类型。
经过继承的Mapper
,例如Country2Mapper
,有独立的Mapper
就意味着有独立的命名空间,能够缓存结果,而且不须要拦截器就能实现。
如今有了Spring4后,又有了一个很重要的缘由。
支持泛型注入,能够实现本身的通用Service,在通用Mapper基础上再次简化操做,加快开发效率。
若是以前说通用Mapper不如Mybatis-Generator自动生成好,我也只能说看我的喜爱,不须要通用Mapper的能够不用,通用Mapper只是为了知足一部分的人须要。
如今来看,**若是还有人说通用Mapper不如Mybatis-Generator自动生成好**,我会建议他看看这篇文档
实际上,不须要说那个更好,适合本身的才好。
另外看完这篇文档后,不须要再说**通用Mapper不如Mybatis-Generator自动生成好**,由于我和一些朋友正在翻译**Mybatis-Generator**,最后还会提供**Mybatis-Generator和通用Mapper的集成插件**,能够用**Mybatis-Generator**直接生成实体类、继承通用Mapper的实体Mapper以及XML文件。
Mybatis-Generator中文文档地址:http://generator.sturgeon.mopaas.com/
Mybatis-Generator官方英文地址:http://mybatis.github.io/generator/index.html
这个文档尚未翻译完,并且译者水平有限,若是发现翻译错误或者不合适的地方,能够在下面的地址提ISSUE
上面这个地址只是生成后的项目文档地址,并非咱们直接用来翻译的项目。