mybatis tkMapper使用——详细版

spring boot 配置文件

#数据库配置
spring.datasource.url=jdbc:mysql://ip地址:3306/dcpp?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatisMapper
#mybatis映射地址
mybatis.type-aliases-package=com.core.entity
mybatis.mapper-locations=classpath:mapper/*.xml
#TkMapper地址
mapper.mappers=com.common.core.tkMapper.TkMapper
mapper.identity=MYSQL
#分页助手
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

一、设置TkMapper单独放在一个包:这个包只有TkMapper一个文件

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface TkMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

二、继承TkMapper使用

public interface TestMapper extends TkMapper<TestBean>{

}


TestBean :
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "表名")
public class TestBean{
 //主键自动驼峰映射
 @Id     //主键ID 表中字段 test_id
 private String testId; 
 @Id     //主键ID 表中字段 test_no
 private String testNo;
 @Column(name = "name_no")
 private String nameNo;
 get/set
}

三、在启动类添加扫描TestMapper

TestMapper包路径

@MapperScan(basePackages = { "xx.xx.xx" }) 

四、使用

@Autowired

private TestMapper mapper;

mapper.方法

等号的CRUD:

 * List<T> select(T record); 根据实体中的属性值进行查询,查询条件使用等号
 * T selectByPrimaryKey(Object key); 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
 * List<T> selectAll(); 查询全部结果,select(null)方法能达到同样的效果
 * T selectOne(T record); 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
 * int selectCount(T record); 根据实体中的属性查询总数,查询条件使用等号
 * int insert(T record); 保存一个实体,null的属性也会保存,不会使用数据库默认值
 * int insertSelective(T record); 保存一个实体,null的属性不会保存,会使用数据库默认值
 * int updateByPrimaryKey(T record); 根据主键更新实体全部字段,null值会被更新
 * int updateByPrimaryKeySelective(T record); 根据主键更新属性不为null的值
 * int delete(T record); 根据实体属性作为条件进行删除,查询条件使用等号
 * int deleteByPrimaryKey(Object key); 根据主键字段进行删除,方法参数必须包含完整的主键属性
 * 
 * 条件的CRUD:
 * List<T> selectByCondition(Object condition); 根据Condition条件进行查询
 * int selectCountByCondition(Object condition); 根据Condition条件进行查询总数
 * int updateByCondition(@Param("record") T record, @Param("example") Object condition); 根据Condition条件更新实体record包含的全部属性,null值会被更新
 * int updateByConditionSelective(@Param("record") T record, @Param("example") Object condition); 根据Condition条件更新实体record包含的不是null的属性值
 * int deleteByCondition(Object condition); 根据Condition条件删除数据

五、带条件的SQL:

Example example = new Example(TransDetailEntity.class);
example.createCriteria().andLike("datetime", "%2018-05-20%").andEqualTo("direct", "1");
transList  = mapper.selectByExample(example);

六、自定义SQL:

public interface TestMapper extends TkMapper<TestBean>{
 //传入一个参数
 @Select(" sql语句 where name= #{name} ")
 TestBean findExchRela(@Param("name") String name);
 //传入bean
 @Select(" sql语句 where name= #{name} ")
 TestBean findExchRela(TestBean testBean);
}

七、POM

<!-- mybatis -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
 </dependency>
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>1.1.1</version>
 </dependency>
 <dependency>
     <groupId>tk.mybatis</groupId>
     <artifactId>mapper-spring-boot-starter</artifactId>
     <version>2.0.0</version>
 </dependency>
 <dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper-spring-boot-starter</artifactId>
     <version>1.2.3</version>
 </dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值