新手初用mybatis

栏目: Java · 发布时间: 5年前

内容简介:版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/85089264

版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/85089264

近期用了下mybatis,感觉不错,比起hibernate来,好像简单不少。使用方法总结如下:

一、代码结构

要有实体类,映射类。映射在于决定如何访问数据库,实体类在于接收查询返回值。

新手初用mybatis

二、映射

最关键的地方在于映射了吧。

我用的是spring boot,sql定义采用的是注解的方式。

package api.mapper;

import api.entity.BootFull;
import api.entity.Fishboat_Radar;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface FishboatMapper {

	//返回单表多条记录
    @Select("select * from fishboat_radar where sequence=(select max(sequence) from fishboat_radar)")
    List<Fishboat_Radar> getNewList();

	//返回多表多条记录
    @Select("select t1.*,\n" +
            "t3.imo,\n" +
            "t3.length,\n" +
            "t3.wide,\n" +
            "t3.mothershipmmsi, \n" +
            "t3.destination,\n" +
            "t3.vendorid,\n" +
            "t3.callsign,\n" +
            "t3.shipclass,\n" +
            "t3.shiptype,\n" +
            "t3.vesselname \n" +
            "from jczs.fishboat_radar t1\n" +
            "join (select max(sequence) as sequence from jczs.fishboat_radar) t2 on t1.sequence=t2.sequence\n" +
            "left outer join jczs.yb_hlx_ais t3 on t1.guid=t3.guid")
    List<BootFull> getFullNewList();

	//返回单表单条记录
    @Select("select * from fishboat_radar where GUID=#{guid}")
    Fishboat_Radar findByGUID(@Param("guid") String guid);
}

三、实体类

实体类的属性对应数据表字段,但似乎大小写没有啥关系,mybatis会自动匹配。

package api.entity;

import java.util.Date;
import lombok.Getter;
import lombok.Setter;

public class Fishboat_Radar{
    private @Getter @Setter String GUID;
    private @Getter @Setter long sequence;
    private @Getter @Setter long ID;
    private @Getter @Setter int type;
    private @Getter @Setter double latitude;
    private @Getter @Setter double longitude;
    private @Getter @Setter double speed;
    private @Getter @Setter double direction;
    private @Getter @Setter Date create_date;
    private @Getter @Setter int MMSI;
    private @Getter @Setter String targetName;
    private @Getter @Setter String timestamp;
}
package api.entity;

import lombok.Getter;
import lombok.Setter;

public class BootFull extends Fishboat_Radar {
    private @Getter @Setter int IMO;
    private @Getter @Setter long length;
    private @Getter @Setter long wide;
    private @Getter @Setter int motherShipMMSI;
    private @Getter @Setter String destination;
    private @Getter @Setter String vendorID;
    private @Getter @Setter String callSign;
    private @Getter @Setter String shipClass;
    private @Getter @Setter String shipType;
    private @Getter @Setter String vesselName;
}

四、相关配置文件

build.gradle

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")

    //部署到外部tomcat
    providedCompile("org.springframework.boot:spring-boot-starter-tomcat")

    //thymeleaf
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")

    //oracle
    compile("com.oracle:ojdbc7:12.1.0.1")

    //mybatis
    compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0")
    testCompile('org.mybatis.spring.boot:mybatis-spring-boot-starter-test:1.3.0')
}

application.properties

spring.jpa.database=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.0.22:1522/pdbzjfwpt
spring.datasource.username=jczs
spring.datasource.password=jczs
spring.jpa.hibernate.ddl-auto=update

#网上有些教程说需要指明实体类所在路径,事实上不需要
#mybatis.typeAliasesPackage=api.entity

五、调用

从代码来看,应用mybatis,代码会得到简化,因为Repository与mapper合在一起了,如果是hibernate,映射归映射,仓库归仓库。

package api.controller;

import api.entity.Author;
import api.entity.BootFull;
import api.entity.Fishboat_Radar;
import api.mapper.FishboatMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value="/api/boats")
public class BoatController {

    @Autowired
    private FishboatMapper boatMapper;

    @RequestMapping(value = {"/",""}, method = RequestMethod.GET)
    public List<Fishboat_Radar> getList() {
        List<Fishboat_Radar> boats = boatMapper.getNewList();
        return boats;
    }
    @RequestMapping(value = "/full", method = RequestMethod.GET)
    public List<BootFull> getFullList() {
        List<BootFull> boats = boatMapper.getFullNewList();
        return boats;
    }
    @RequestMapping(value = "/{guid}", method = RequestMethod.GET)
    public Fishboat_Radar findByGUID(@PathVariable String guid) {
        Fishboat_Radar boat = boatMapper.findByGUID(guid);
        return boat;
    }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

新媒体十讲

新媒体十讲

范卫锋 / 中信出版社 / 2015-8 / 38.00元

“新媒体参谋长”范卫锋解答:媒体人如何转型?怎么创业?如何看准新媒体项目进行投资? 作为“新媒体的参谋长”,本书作者范卫锋将十余年从业亲历的经验教训、行业内幕串联成册,从定位、突破、扩张、商业模式、价值几个方面剖析新媒体策略,解密国内媒体圈转型、创业、投资的实操法则。案例信手拈来,观点鞭辟入里,打造出国内第一本由专业新媒体投资人撰写的新媒体实战兵法,涉及媒体人转型、新媒体实操、媒体公关营销、......一起来看看 《新媒体十讲》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具