zoukankan      html  css  js  c++  java
  • springboot集成通用mapper详细配置

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

    pom.xml配置

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

    定义通用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> {
    }
    

    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
    

    注:通用基类请不要与我们下面要定义的mapper位于同一包下,否则运行会报错。

    定义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);
    	}
    
    }
    

    注:此处MapperScan为tk.mybatis.spring.annotation.MapperScan,非org.mybatis.spring.annotation.MapperScan;

    定义实体类

    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() {
        }
    }
    

    注:如果bean实体中含有数据表中不存在的字段,使用@Transient注解

    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);
    }
    

    注:mapper中已经继承了TkMapper里面的所有单表操作的增删改查方法,上面的两个方法getAllgetById是我们自定义的方法,一种通过注解定义,另外一种是我们常用的在xml文件里面写方法。

    测试

    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);
    
        }
    }
  • 相关阅读:
    稳扎稳打Silverlight(13) 2.0交互之鼠标事件和键盘事件
    稳扎稳打Silverlight(17) 2.0数据之详解DataGrid, 绑定数据到ListBox
    再接再厉VS 2008 sp1 + .NET 3.5 sp1(2) Entity Framework(实体框架)之详解 Linq To Entities 之一
    稳扎稳打Silverlight(8) 2.0图形之基类System.Windows.Shapes.Shape
    稳扎稳打Silverlight(11) 2.0动画之ColorAnimation, DoubleAnimation, PointAnimation, 内插关键帧动画
    稳扎稳打Silverlight(21) 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应
    稳扎稳打Silverlight(16) 2.0数据之独立存储(Isolated Storage)
    稳扎稳打Silverlight(9) 2.0画笔之SolidColorBrush, ImageBrush, VideoBrush, LinearGradientBrush, RadialGradientBrush
    稳扎稳打Silverlight(23) 2.0通信之调用WCF的双向通信(Duplex Service)
    游戏人生Silverlight(1) 七彩俄罗斯方块[Silverlight 2.0(c#)]
  • 原文地址:https://www.cnblogs.com/yiweiblog/p/11196608.html
Copyright © 2011-2022 走看看