0
点赞
收藏
分享

微信扫一扫

springboot集成通用mapper详细配置

攻城狮Chova 2021-09-29 阅读 30

       通常,我们利用mybatis写持久层方法。要么按照传统定义mapper方法,定义xml文件的方式,全部手写。要么需要通过mybatis-generator逆向工程插件生成大量的xxxExample文件,使得系统看起来比较臃肿。而通用mapper的引入,我们不需再生成大量的Example文件,并且通用mapper已经封装好了所有的单表操作。通用mapper与springboot项目集成配置如下:

1.pom.xml配置

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

2.定义通用Mapper基类

    package com.gogle.mgt.dataaccess.mybatis;
    
    import tk.mybatis.mapper.common.Mapper;
    import tk.mybatis.mapper.common.MySqlMapper;
    
    /**
     * @ClassName TkMapper
     * @Description TODO
     * @Date 2019/7/16 16:15
     * @Created by sunyiwei
     */
    public interface TkMapper<T> extends Mapper<T>,MySqlMapper<T> {
    }

3.yml文件配置

    mybatis:
      mapper-locations: classpath:mapper/*.xml #定义xml文件位置,不是必须的,如果需要在xml写sql请配置此选项
      type-aliases-package: com.gogle.mgt.domain  # 注意:对应实体类的路径
    mapper:
      mappers: com.gogle.mgt.dataaccess.mybatis.TkMapper #通用基类配置
      identity: MYSQL

4.定义MapperScan

    package com.gogle.mgt;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import tk.mybatis.spring.annotation.MapperScan;
    
    @EnableEurekaClient
    @EnableFeignClients
    @SpringBootApplication
    @MapperScan(basePackages = { "com.gogle.mgt.dataaccess.mybatis.dao" })
    public class VslmApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(VslmApplication.class, args);
        }
    
    }

5.定义实体类

    package com.gogle.mgt.domain;
    
    
    import javax.persistence.Column;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import java.util.Date;
    
    /**
     * @ClassName StreamLine
     * @Description TODO
     * @Date 2019/7/16 16:16
     * @Created by sunyiwei
     */
    @Table(name="stream_line")
    public class StreamLine {
        @Id
        String streamLineId;
        @Column(name = "name")
        String name;
        @Column(name = "area")
        String area;
        @Column(name = "spec_id")
        String specId;
        @Column(name = "vpl_id")
        String vplId;
        @Column(name = "status")
        byte status;
        @Column(name = "create_time")
        Date createTime;
        @Column(name = "modify_time")
        Date modifyTime;
        @Column(name = "modify_person")
        String modifyPerson;
    
        public StreamLine() {
        }
    }

6.mapper定义

    package com.gogle.mgt.dataaccess.mybatis.dao;
    
    import com.gogle.mgt.dataaccess.mybatis.TkMapper;
    import com.gogle.mgt.domain.StreamLine;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    /**
     * @ClassName StreamLineMapper
     * @Description TODO
     * @Date 2019/7/16 16:23
     * @Created by sunyiwei
     */
    @Repository
    public interface StreamLineMapper extends TkMapper<StreamLine> {
    
        /**
         * 注解sql
         * @return
         */
        @Select("select * from stream_line")
        List<StreamLine> getAll();
    
        /**
         * xml定义语句
         */
        StreamLine getById(String streamLineId);
    }

7.测试

    package com.gogle.mgt;
    
    import java.util.List;
    
    /**
     * @ClassName MapperTest
     * @Description TODO
     * @Date 2019/7/16 16:28
     * @Created by sunyiwei
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class MapperTest {
        @Autowired
        private StreamLineMapper mapper;
    
        /**
         * 继承自通用mapper的单表操作方法测试
         */
        @Test
        public void test(){
            StreamLine line = new StreamLine();
            line.setStreamLineId("001");
            line.setArea("萧山");
            int insert = mapper.insert(line);
            System.out.println(insert);
        }
    
        /**
         * 我们注解自定义的方法测试
         */
        @Test
        public void test2(){
            List<StreamLine> all = mapper.getAll();
            System.out.println(all);
    
        }
    
        /**
         * xml文件中定义接口测试
         */
        @Test
        public void test3(){
            StreamLine one = mapper.getById("001");
            System.out.println(one);
    
        }
    }
举报

相关推荐

0 条评论