新手初用mybatis

网友投稿 296 2022-11-04


新手初用mybatis

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

一、代码结构

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

二、映射

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

我用的是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@Mapperpublic interface FishboatMapper { //返回单表多条记录 @Select("select * from fishboat_radar where sequence=(select max(sequence) from fishboat_radar)") List 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 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=oraclespring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriverspring.datasource.url=jdbc:oracle:thin:@192.168.0.22:1522/pdbzjfwptspring.datasource.username=jczsspring.datasource.password=jczsspring.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 getList() { List boats = boatMapper.getNewList(); return boats; } @RequestMapping(value = "/full", method = RequestMethod.GET) public List getFullList() { List 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; }}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:律师查询API(律师查询公民个人信息)
下一篇:详解shrio的认证(登录)过程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~