代码实现| springboot+Mybatis注解版轻松集成

        springboot集成Mybatis有两种方式,一种是配置xml方式,另一种是使用注解方式,本文主要讲解一下如何轻松快速的实现注解版的springboot+mybatis继承,开发本不易,绕过xml配置文件更健康啊!

        关于两个框架的理论部分本文暂时不讲,默认都会,否则也不会搜到这个破文章(嘿嘿!)

轻松集成:

1、添加pom依赖

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        版本号大家随意。

2、添加数据源

        打开application.properties文件,添加内容:

#数据库配置
mybatis.type-aliases-packaage=com.xx.xxx.model
spring.datasource.url=jdbc:mysql://xxx:3306/xx?characterEncoding=UTF8&socketTimeout=60000
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3、编写实体类POJO

        这个操作也是老生常谈了,其实可以用Mybatis逆向工具生成代码,不过都说了是轻松实现了,就不扯到其他工具了(用工具不又得配置generator的xml文件嘛,写xml文件哪有POJO轻松,活着不好嘛哈哈)。


import java.util.Date;

public class Video {

    private Integer id;
    private String title;
    private Integer authorID;
    private Date time;
    private String describ;
    private Integer cateID;
    private Integer tagID;
    private String filePath;
    private String thumbNail;
    private Integer viewCount;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getAuthorID() {
        return authorID;
    }

    public void setAuthorID(Integer authorID) {
        this.authorID = authorID;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getDescrib() {
        return describ;
    }

    public void setDescrib(String describ) {
        this.describ = describ;
    }

    public Integer getCateID() {
        return cateID;
    }

    public void setCateID(Integer cateID) {
        this.cateID = cateID;
    }

    public Integer getTagID() {
        return tagID;
    }

    public void setTagID(Integer tagID) {
        this.tagID = tagID;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getThumbNail() {
        return thumbNail;
    }

    public void setThumbNail(String thumbNail) {
        this.thumbNail = thumbNail;
    }

    public Integer getViewCount() {
        return viewCount;
    }

    public void setViewCount(Integer viewCount) {
        this.viewCount = viewCount;
    }

    @Override
    public String toString() {
        return "Video{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", authorID=" + authorID +
                ", time=" + time +
                ", describ='" + describ + '\'' +
                ", cateID=" + cateID +
                ", tagID=" + tagID +
                ", filePath='" + filePath + '\'' +
                ", thumbNail='" + thumbNail + '\'' +
                ", viewCount=" + viewCount +
                '}';
    }
}

        这个是我的POJO类,仅供参考,字段啥的该换就换。这里值得说一下的就是:可能会纠结实体类的属性名和数据表中的字段要不要一样,后来发现可以不一样,只要后面再加个映射就好了

4、编写DAO接口类

@Mapper
public interface VideoMapper {

    @Select("SELECT * FROM videos")
    @Results(id="videoMap",  value = {
            @Result(id = true, column = "_id", property = "id"),
            @Result(column = "_title", property = "title"),
            @Result(column = "_author_id", property = "authorID"),
            @Result(column = "_time", property = "time"),
            @Result(column = "_describ", property = "describ"),
            @Result(column = "_cate_id", property = "cateID"),
            @Result(column = "_tag_id", property = "tagID"),
            @Result(column = "_file_path", property = "filePath"),
            @Result(column = "_thumbnail", property = "thumbNail"),
            @Result(column = "_view_count", property = "viewCount"),
    })
    List<Video> getAll();
}

        这里有个@Results注解就是实现实体类和数据表字段映射的作用,如果这个接口下面还要其他方法只需要使用@ResultMap("videoMap")就可以轻松映射了,还是很方便的。类注解@Mapper也可以通过在启动类添加包扫描注解@MapperScan替换,使用包扫描的好处就是不需要在每个Mapper类上都添加@Mapper注解(其实工作量也不大,添加又如何哈哈)。

@SpringBootApplication
@MapperScan("com.x.xx.mapper")
public class VideoAdminApplication {

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

}

其实,到此我们就已经实现了springboot+Mybatis注解版的集成了,通过@Autowired将Mapper类自动注入就可以使用了,现在来测试一下:

@SpringBootTest
class VideoAdminApplicationTests {
    @Autowired
    private VideoMapper videoMapper;


    @Test
    void TestMybatis(){
        System.out.println(videoMapper.getAll().toString());
    }

}
查询结果

 

        是不是发现很简单,自从不需要写xml文件后,腰也不酸了腿也不疼了,一口气能写五行代码,专为懒癌患者提供的数据库操作方案,适合躺着使用哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值