通用mapper的配置

https://gitee.com/free/Mapper/wikis/Home  

更多内容上该网址查看

Maven依赖

<!-- 通用mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper</artifactId>
			<version>4.0.0-beta3</version>
		</dependency>

Spring配置,代替org的mybatis

<!--通用mapper -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.yuan.dao" />
		 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
		<property name="properties">
			<value>
				mappers=tk.mybatis.mapper.common.Mapper
			</value>
		</property>
	</bean>

注意这里使用 tk.mybatis.spring.mapper.MapperScannerConfigure 替换原来Mybatis的 org.mybatis.spring.mapper.MapperScannerConfigurer 

可配参数介绍:

  1. UUID :设置生成UUID的方法,需要用OGNL方式配置,不限制返回值,但是必须和字段类型匹配
  2. IDENTITY :取回主键的方式,可以配置的内容看下一篇如何使用中的介绍
  3. ORDER : <seletKey> 中的order属性,可选值为BEFORE和AFTER
  4. catalog :数据库的catalog,如果设置该值,查询的时候表名会带catalog设置的前缀
  5. schema :同catalog,catalog优先级高于schema
  6. seqFormat :序列的获取规则,使用{num}格式化参数,默认值为{0}.nextval,针对Oracle,可选参数一共4个,对应0,1,2,3分别为SequenceName,ColumnName, PropertyName,TableName
  7. notEmpty :insert和update中,是否判断字符串类型!='',少数方法会用到
  8. style :实体和表转换时的规则,默认驼峰转下划线,可选值为normal用实体名和字段名;camelhump是默认值,驼峰转下划线;uppercase转换为大写;lowercase转换为小写
  9. enableMethodAnnotation :可以控制是否支持方法上的JPA注解,默认false。

大多数情况下不会用到这些参数,有特殊情况可以自行研究。

实体类的写法

记住一个原则:实体类的字段数量 >= 数据库表中需要操作的字段数量。默认情况下,实体类中的所有字段都会作为表中的字段来操作,如果有额外的字段,必须加上 @Transient 注解。


说明:

  1. 表名默认使用类名,驼峰转下划线(只对大写字母进行处理),如 UserInfo 默认对应的表名为 user_info 。
  2. 表名可以使用 @Table(name = "tableName") 进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.
  3. 字段默认和 @Column 一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式.
  4. 可以使用 @Column(name = "fieldName") 指定不符合第3条规则的字段名
  5. 使用 @Transient 注解可以忽略字段,添加该注解的字段不会作为表字段使用.
  6. 建议一定是有一个 @Id 注解作为主键的字段,可以有多个 @Id 注解的字段作为联合主键.
  7. 如果是MySQL的自增字段,加上 @GeneratedValue(generator = "JDBC") 即可。

DAO 的写法

在传统的Mybatis写法中, DAO 接口需要与 Mapper 文件关联,即需要编写 SQL 来实现 DAO 接口中的方法。而在通用Mapper中, DAO 只需要继承一个通用接口,即可拥有丰富的方法:

继承通用的Mapper ,必须指定泛型

@Component
public interface BasDictMapper extends Mapper<BasDict> {

}

一旦继承了Mapper ,继承的Mapper就拥有了Mapper 所有的通用方法:

Select

方法: List<T> select(T record); 
说明:根据实体中的属性值进行查询,查询条件使用等号

方法: T selectByPrimaryKey(Object key); 
说明:根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

方法: List<T> selectAll(); 
说明:查询全部结果,select(null)方法能达到同样的效果

方法: T selectOne(T record); 
说明:根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

方法: int selectCount(T record); 
说明:根据实体中的属性查询总数,查询条件使用等号

Insert

方法: int insert(T record); 
说明:保存一个实体,null的属性也会保存,不会使用数据库默认值

方法: int insertSelective(T record); 
说明:保存一个实体,null的属性不会保存,会使用数据库默认值

Update

方法: int updateByPrimaryKey(T record); 
说明:根据主键更新实体全部字段,null值会被更新

方法: int updateByPrimaryKeySelective(T record); 
说明:根据主键更新属性不为null的值

Delete

方法: int delete(T record); 
说明:根据实体属性作为条件进行删除,查询条件使用等号

方法: int deleteByPrimaryKey(Object key); 
说明:根据主键字段进行删除,方法参数必须包含完整的主键属性

Example方法

方法: List<T> selectByExample(Object example); 
说明:根据Example条件进行查询

重点:这个查询支持通过 Example 类指定查询列,通过 selectProperties 方法指定查询列

方法: int selectCountByExample(Object example); 
说明:根据Example条件进行查询总数

方法: int updateByExample(@Param("record") T record, @Param("example") Object example); 
说明:根据Example条件更新实体 record 包含的全部属性,null值会被更新

方法: int updateByExampleSelective(@Param("record") T record, @Param("example") Object example); 
说明:根据Example条件更新实体 record 包含的不是null的属性值

方法: int deleteByExample(Object example); 
说明:根据Example条件删除数据

代码中使用

在 service 中注入 dao ,即可使用。

@Service
@Transactional
public class BasDictServiceImpl extends BaseServiceImpl<BasDict>implements BasDictService {
	@Autowired
	private BasDictMapper basDictMapper;
}



总结

通用Mapper的原理是通过反射获取实体类的信息,构造出相应的SQL,因此我们只需要维护好实体类即可,对于应付复杂多变的需求提供了很大的便利。上文叙述的只是通用Mapper的简单用法,在实际项目中,还是要根据业务,在通用Mapper的基础上封装出粒度更大、更通用、更好用的方法。

@GeneratedValue(strategy = GenerationType.IDENTITY)

这个注解适用于主键自增的情况,支持下面这些数据库:

  • DB2: VALUES IDENTITY_VAL_LOCAL()
  • MYSQL: SELECT LAST_INSERT_ID()
  • SQLSERVER: SELECT SCOPE_IDENTITY()
  • CLOUDSCAPE: VALUES IDENTITY_VAL_LOCAL()
  • DERBY: VALUES IDENTITY_VAL_LOCAL()
  • HSQLDB: CALL IDENTITY()
  • SYBASE: SELECT @@IDENTITY
  • DB2_MF: SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
  • INFORMIX: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
  • JDBC:这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系数据库管理系统的自动递增字段)。

使用GenerationType.IDENTITY需要在全局配置中配置IDENTITY的参数值,并且需要根据数库配置ORDER属性。

举例如下:

//不限于@Id注解的字段,但是一个实体类中只能存在一个(继承关系中也只能存在一个)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; 

对应的XML形式为:

<insert id="insertAuthor">
    <selectKey keyProperty="id" resultType="int" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}) </insert> 

注意<selectKey>中的内容就是IDENTITY参数值对应数据库的SQL

 

3.@GeneratedValue(generator = "UUID")

//可以用于任意字符串类型长度超过32位的字段
@GeneratedValue(generator = "UUID") private String username; 

该字段不会回写。这种情况对应类似如下的XML:

<insert id="insertAuthor">
  <bind name="username_bind" value='@java.util.UUID@randomUUID().toString().replace("-", "")' /> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username_bind}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}) </insert> 

注意:这种方式不能回写


关于oracle序列:

    

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "select SEQ_ID.nextval from dual") private Integer id; 

使用Oracle序列的时候,还需要配置:

<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yuan.dao" />
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
ORDER=BEFORE
</value>
</property>
</bean>

因为在插入数据库前,需要先获取到序列值,否则会报错。
这种情况对于的xml类似下面这样:

<insert id="insertAuthor">
<selectKey keyProperty="id" resultType="int" order="BEFORE"> select SEQ_ID.nextval from dual </selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}) </insert>

不存在与数据库的属性用@Transient 注解表示


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值