新手初用mybatis

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

内容简介:版权声明:本文为博主原屙文章,喜欢你就担走。 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;
    }
}

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

查看所有标签

猜你喜欢:

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

黑客大曝光

黑客大曝光

Joel Scambray、Vincent Liu、Caleb Sima / 姚军 / 机械工业出版社华章公司 / 2011-10 / 65.00元

在网络技术和电子商务飞速发展的今天,Web应用安全面临着前所未有的挑战。所有安全技术人员有必要掌握当今黑客们的武器和思维过程,保护Web应用免遭恶意攻击。本书由美国公认的安全专家和精神领袖打造,对上一版做了完全的更新,覆盖新的网络渗透方法和对策,介绍如何增强验证和授权、弥补Firefox和IE中的漏洞、加强对注入攻击的防御以及加固Web 2.0安全,还介绍了如何将安全技术整合在Web开发以及更广泛......一起来看看 《黑客大曝光》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具