mybatis通用mapper使用

1 添加相关maven文件

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

2、application.yml 添加相关配置

1.添加数据库相关配置

springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中, sqlSessionFactory会自动注入到Mapper中,对了你一切都不用管了,直接拿起来使用就行了

   spring:
    datasource:
        url: jdbc:mysql://localhost:3306/ms_datadictionary?useUnicode=true&characterEncoding=UTF8
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver

2.添加对mapper和xml文件的扫描

   mybatis:
    basepackage: com.yss.ms.mapper
    xmlLocation: classpath:mapper/**/*.xml

3.开发

1.创建实体

在entity里面 新建Datadict类

@Table(name="t_datadict")
public class Datadict {
    @Id
    private String fid;

    @Column(name="FDICTATEGORY_id")
    private String fdictategoryId;

    private String fname;

    private String fcode;

    @Column(name="fcreator_id")
    private String fcreatorId;
    .......set/get方法略

2.创建mapper

在mapper 里面新建一个Interface

public interface DatadictMapper extends Mapper<Datadict> {

}

注意:该接口必须继承Mapper接口并传入相应实体。

Mapper中封装了简单的增删改成方法,可以直接调用。

/**
 * 通用Mapper接口,其他接口继承该接口即可
 * <p/>
 * <p>这是一个例子,自己扩展时可以参考</p>
 * <p/>
 * <p>项目地址 : <a href="https://github.com/abel533/Mapper" target="_blank">https://github.com/abel533/Mapper</a></p>
 *
 * @param <T> 不能为空
 * @author liuzh
 */
public interface Mapper<T> extends
        BaseMapper<T>,
        ExampleMapper<T>,
        RowBoundsMapper<T>,
        Marker {

}
public interface BaseMapper<T> extends
        BaseSelectMapper<T>,
        BaseInsertMapper<T>,
        BaseUpdateMapper<T>,
        BaseDeleteMapper<T> {

}

上面就基本完成了相关dao层开发,使用的时候当作普通的类注入进入就可以了

猜你喜欢

转载自blog.csdn.net/weixin_37509652/article/details/80093984