SpringBoot之集成通用Mapper

第一种:

1.引入POM坐标,需要同时引入通用mapper和jpa

<dependency>
    <groupId>tk.mybatis</groupId>  
    <artifactId>mapper</artifactId>  
    <!-- 建议使用最新版本,最新版本请从项目首页查找 -->  
    <version>3.4.0</version>  
</dependency>  
<dependency>  
    <groupId>javax.persistence</groupId>  
    <artifactId>persistence-api</artifactId>  
    <version>1.0</version>  
</dependency>

2.将自己的mapper文件继承通用mapper的BaseMapper

@Repository
public interface RatWaiterHitRewardMapper extends BaseMapper<RatWaiterHitReward>{

}

3.编写JAVA BEAN配置类

@Configuration
public class TkMybatisConfig {

    @Bean(name="mapperHelper")
    public MapperScannerConfigurer mapperHelper(){
        Properties properties = new Properties();
        properties.setProperty("mappers",BaseMapper.class.getName());
        properties.setProperty("IDENTITY","MYSQL"); // 数据库方言(主要用于:取回主键的方式)
        properties.setProperty("notEmpty","false"); // insert和update中,是否判断字符串类型!='',少数方法会用到
        properties.setProperty("style", Style.camelhump.name());

        MapperScannerConfigurer scan = new MapperScannerConfigurer();
        scan.setSqlSessionFactoryBeanName("sqlSessionFactory"); // 多数据源时,必须配置
        scan.setBasePackage("com.eparty.ccp.rate.mapper");//mapper.java文件的路径
        scan.setMarkerInterface(BaseMapper.class); // 直接继承了BaseDao接口的才会被扫描,basePackage可以配置的范围更大。
        scan.setProperties(properties);

        return scan;
    }
}

 

第二种(推荐):

1、配置mybatis

application.properties(配置文件)

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=false&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 指向mapper的xml文件位置
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml

 

2、引入依赖(springboot专用)

 <dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>RELEASE</version>
</dependency>

 

3、配置启动类

import tk.mybatis.spring.annotation.MapperScan;     // 注意:这里是导入通用mapper的MapperScan
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "cn.yujiago.springboot.mapper") // mapper接口的路径
public class BootApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }

}

 

4、model类的配置

@Table(name = "test_table")
public class TestTableVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "JDBC")
    private Long id;

    @Transient
    private String userId;

    private String name;

    private Timestamp createTime;

    private String createUserId;

    private Timestamp updateTime;

    private String updateUserId;

    private Integer isDelete;

    // 省略get、set...
}

 

说明:

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

5、mapper接口的配置

import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

@Repository
public interface UserMapper extends Mapper<User> {

}

 

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条件删除数据

下面演示大概的写法: 
新增

TestTableVO vo = new TestTableVO();
// 省略为vo设置属性...
int row = testTableDao.insertSelective(vo);

修改

TestTableVO vo = new TestTableVO();
// 省略为vo设置属性...
int row = testTableDao.updateByPrimaryKeySelective(vo);

 

单个查询

TestTableVO vo = new TestTableVO();
vo.setId(123L);
TestTableVO result = testTableDao.selectOne(vo);

 

条件查询

// 创建Example
Example example = new Example(TestTableVO.class);
// 创建Criteria
Example.Criteria criteria = example.createCriteria();
// 添加条件
criteria.andEqualTo("isDelete", 0);
criteria.andLike("name", "%abc123%");
List<TestTableVO> list = testTableDao.selectByExample(example);

 

posted @ 2019-06-10 15:02  一心二念  阅读(6933)  评论(0编辑  收藏  举报