mysql 通用mapper_MyBatis & 通用Mapper

MyBatis使用简单、灵活,但是有一些特别常用的简单SQL也要手写,很浪费时间。使用通用Mapper后可以免去这种工作,提高开发效率。

快速使用

引入Jar(pom.xml)

org.springframework.boot

spring-boot-starter

tk.mybatis

mapper-spring-boot-starter

2.1.5

mysql

mysql-connector-java

8.0.13

spring.datasource.url=jdbc:mysql://localhost:3306/tenmao?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8

spring.datasource.username=tenmao_user

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

实体类 (Person.java)

@Data

@Table(name = "person")

@NameStyle(Style.camelhumpAndLowercase)

public class Person {

@javax.persistence.Id

@KeySql(useGeneratedKeys = true)

private Integer id;

private String name;

private Integer age;

private Boolean gender;

@ColumnType(jdbcType = JdbcType.VARCHAR, typeHandler = StringListTypeHandler.class)

private List hobbies;

}

Mapper接口(PersonMapper.java)

package com.tenmao.tmapper.mapper;

import com.tenmao.tmapper.domain.Person;

import org.apache.ibatis.annotations.Param;

import org.apache.ibatis.annotations.Select;

import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@org.apache.ibatis.annotations.Mapper

public interface PersonMapper extends Mapper {

@Select("SELECT * FROM person WHERE name=#{name}")

Person selectByName(@Param("name") String name);

List selectByAge(@Param("age") int age);

}

使用代码

Person person = personMapper.selectByPrimaryKey(1);

优点

为最常见的语句提供了内置接口,不需要写任何SQL语句。比如:

selectOne

select

selectAll

selectCount

selectByPrimrayKey

方法太多,省略其他...

针对一些稍微高级一点的查询,可以使用Example机制

Example example = new Example(Person.class);

example.createCriteria().andGreaterThan("age", 18);

List people = personMapper.selectByExample(example);

对应的SQL语句是

SELECT id,name,age,gender FROM person WHERE ( ( age > ? ) )

也支持标准的MyBatis的接口使用方式

注解方式

@org.apache.ibatis.annotations.Mapper

public interface PersonMapper extends Mapper {

@Select("SELECT * FROM person WHERE name=#{name}")

Person selectByName(@Param("name") String name);

}

XML文件方式

mapper/person_mapper.xml

SELECT * FROM person WHERE age=#{age}

application.properties

mybatis.mapper-locations=classpath:mapper/*_mapper.xml

其他配置

与数据库没有对应关系的字段

@Transient

private String otherThings; //非数据库表中字段

配置TypeHandler

@ColumnType(typeHandler = AddressTypeHandler.class)

private Address address;

application.properties

mybatis.type-handlers-package=com.tenmao.handler

常见错误

personMapper.selectByPrimaryKey(1)执行的SQL语句是

Preparing: SELECT id,name,age,gender FROM person WHERE id = ? AND name = ? AND age = ? AND gender = ?

Parameters: 1(Integer), 1(Integer), 1(Integer), 1(Integer)

因为没有配置PrimaryKey,所以在Person的id上配置注解@javax.persistence.Id

@Data

public class Person {

@javax.persistence.Id @GeneratedValue(generator = "JDBC")

private Integer id;

private String name;

private Integer age;

private Boolean gender;

}

提醒:如果实体类中没有一个标记 @Id 的字段,当你使用带有 ByPrimaryKey 的方法时,所有的字段会作为联合主键来使用,也就会出现类似 where id = ? and countryname = ? and countrycode = ? 的情况。

推荐实践

简单的SQL语句就直接使用通用mapper提供的接口

另外一些特别简单的SQL也可以通过Example扩展机制完成

更复杂的SQL语句,建议使用标准的MyBatis的实现方式

参考

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值