最近有項目須要使用java語言開發數據接口,總體框架須要符合微服務架構,在網上查找了相關資料,最終選定了Springcloud+Springboot的架構,此文主要記錄Mybatis在Springboot下的使用,一方面做爲學習日記,另外一方面也但願對正在學習springboot的朋友們有必定幫助。
全文包含3部份內容:java
項目搭建mysql
數據庫配置web
數據操做層編寫spring
本文中使用的編輯器爲Intellij Idea,建立流程也是基於Intellij,具體步驟以下:sql
在New Project面板中中選擇Spring Initializr,點擊Next按鈕,以下圖:
數據庫
填寫項目的相關信息,Type一行根據使用習慣選擇Maven或者Gradle就好。apache
選擇項目的相關依賴,該文章中Web選項裏選擇了Web,Sql選項裏面勾選Mybatis及Mysql後按着嚮導完成項目建立,以下圖:
api
最終生成的項目目錄結構以下圖:
springboot
以下爲項目的依賴配置文件代碼:mybatis
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在Springboot中,數據庫的配置很是簡單,在resources文件夾下的application.propertites編寫相關配置便可,喜歡yml格式的也能夠換成application.yml,具體以下:
mybatis.type-aliases-package=com.aryan.entity spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = demopc
需注意第一行配置,com.aryan.entity指定了後面的mybatis數據庫實體從哪一個包進行查找。
在main的src目錄下新建package,命名爲entity,做爲mybatis的數據庫實體層,與前面數據庫配置相對應。
建立Product實體,代碼以下:
package com.aryan.entity; import java.io.Serializable; /** * Created by Aryan on 2017/6/25. */ public class ProductEntity implements Serializable { private Long id; private String name; private String code; public ProductEntity() { super(); } public ProductEntity(Long id, String name, String code) { super(); this.id = id; this.name = name; this.code = code; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Override public String toString() { return "ProductEntity{" + "id=" + id + ", name='" + name + '\'' + ", code='" + code + '\'' + '}'; } }
在main目錄的src文件夾下新建package,命名爲mapper,該package做爲項目的dao層映射,將sql語句與調用的方法關聯起來,總體使用起來比較靈活。
在mapper包下新建ProductMapper接口,編寫相關數據庫操做方法,具體代碼以下:
package com.aryan.mapper; import com.aryan.entity.ProductEntity; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import java.util.List; /** * Created by Aryan on 2017/6/25. */ public interface ProductMapper { @Select("SELECT * FROM products") List<ProductEntity> getAll(); @Select("SELECT * FROM Products WHERE id = #{id}") ProductEntity getOne(Long id); @Insert("INSERT INTO products(name,code) VALUES(#{name}, #{code})") void insert(ProductEntity product); }
在main目錄src文件夾下新建package,命名爲controller,該package做爲MVC中的控制器,主要編寫了產品添加及查詢的路由,具體代碼以下:
package com.aryan.controller; import com.aryan.entity.ProductEntity; import com.aryan.mapper.ProductMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by Aryan on 2017/6/25. */ @RestController public class ProductController { @Autowired private ProductMapper productMapper; @RequestMapping(value = "/products") public List<ProductEntity> getAll() { return productMapper.getAll(); } @RequestMapping(value = "/product/{id}") public ProductEntity getOne(@PathVariable Long id) { return productMapper.getOne(id); } @RequestMapping(value = "/product/add", method = RequestMethod.POST) public void insert(@RequestBody ProductEntity product) { productMapper.insert(product); } }
運行項目前首先須要在Application入口文件中加上dao層配置,添加@MapperScan("com.aryan.mapper")註解,代碼以下:
package com.aryan; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.aryan.mapper") public class SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMybatisApplication.class, args); } }
直接運行該項目, 運行成功後,在進行api訪問時別忘記先把數據庫表生成好, 而後訪問http://localhost:8080/products 查看數據庫數據,經過Postman或者Fiddler採用Post訪問http://localhost:8080/product... 並傳遞產品數據,成功後能夠查看錶數據確認接口是否運行正常。
最後,整個項目完成後目錄結構以下: