内容简介:版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/85089264
版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/85089264
近期用了下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 @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; } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning XML with DOM and Ajax
Sas Jacobs / Apress / 2006-06-05 / USD 39.99
Don't waste time on 1,000-page tomes full of syntax; this book is all you need to get ahead in XML development. Renowned web developer Sas Jacobs presents an essential guide to XML. Beginning XML with......一起来看看 《Beginning XML with DOM and Ajax》 这本书的介绍吧!