MySQL是一個關係型數據庫管理系統,由瑞典MySQL AB 公司開發,目前屬於 Oracle 旗下產品。MySQL 是最流行的關係型數據庫管理系統之一,在 WEB 應用方面,MySQL是最好的 RDBMS (Relational Database Management System,關係數據庫管理系統) 應用軟件之一。
MySQL是一種關係數據庫管理系統,關係數據庫將數據保存在不一樣的表中,而不是將全部數據放在一個大倉庫內,這樣就增長了速度並提升了靈活性。
MySQL所使用的 SQL 語言是用於訪問數據庫的最經常使用標準化語言。MySQL 軟件採用了雙受權政策,分爲社區版和商業版,因爲其體積小、速度快、整體擁有成本低,尤爲是開放源碼這一特色,通常中小型網站的開發都選擇 MySQL 做爲網站數據庫。mysql---引用自百度百科web
MySQL的安裝能夠是直接ZIP解壓安裝,手動配置後就可使用,還能夠經過安裝包安裝,本機爲win10的操做系統,採用MySQL Installer來安裝便可,安裝的版本爲MySQL Installer 8.0.16,以下,直接下載32bit版本便可,暗轉的時候安裝導航會自動識別幫助安裝64bit,若是你的機器是64位的話;spring
進入安裝界面,選擇Custom,用於開發的話,不少東西咱們不須要安裝;sql
選擇要安裝的包,記住勾選workbench,這是MySQL官方出的GUI管理工具shell
下一步數據庫
下一步json
開始進行配置選擇,單體應用的話選第一個選項瀏覽器
選擇開發電腦,端口號默認便可,儘可能不改安全
安全性考慮,選擇須要輸入帳號密碼
選擇建立Mysql服務
建立成功,輸入帳號密碼check一下
安裝成功了
打開剛纔勾選安裝的shell,看看版本信息是否安裝成功了,以下,MySQL Shell 的版本爲8.0.16
打開workbench,界面很清爽,看起來還不錯吧,能夠看到已經建立了一個默認的實例MySQL80
打開這個是庫裏面的,打開world這個shema,隨便選一個表來看,就city好了,有name字段/countrycode字段等等
這裏不詳細敘述spring boot2或Mybatis怎麼用,僅僅是寫個小代碼測試下MySQL的使用;
首先咱們要建立一個庫,咱給他的編碼格式設爲utf8的,避免中文亂碼,以下
CREATE database mytest01 DEFAULT CHARACTER SET utf8; use mytest01;
再建立一張測試表,也是utf8的格式,這裏建立一張產品表,就定個名稱、描述、價格3個字段,簡單搞搞
CREATE TABLE product( id int(11) not null PRIMARY KEY auto_increment, name varchar(128) DEFAULT null, description varchar(1000) DEFAULT null, price DOUBLE DEFAULT null )DEFAULT charset=utf8;
再插入幾條測試數據
INSERT INTO product(name,description,price) VALUES('小蘋果','一種熟透了的水果',6.99); INSERT INTO product(name,description,price) VALUES('orange','yellow fruit',5.99); INSERT INTO product(name,description,price) VALUES('rice','a kind of food',3.99); INSERT INTO product(name,description,price) VALUES('櫻桃','女友非要買的很貴的水果',55.99);
先講下pom.xml,把必要的包依賴進來,我這裏用了德魯伊的數據源,引入fastjson是爲了用Object快轉json
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </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> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency>
數據庫配置以下,最後mybatis.mapperLocations是指定我mapper的xml位置
# 數據源配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql:///mytest01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 mybatis.mapperLocations= classpath:mapper/*.xml
實體類ProductPO
/** * product實體 * * @author : spzh * @since : 2019-06-19 22:59 */ public class ProductPO implements Serializable { private Integer id; private String name; private String description; private Double price; 省略set/get }
Dao層,能夠以Dao結尾或者Mapper均可以,@Mapper註解下,不須要寫實現類
/** * product Dao * * @author : spzh * @since : 2019-06-19 23:02 */ @Mapper public interface IProductDao { List<ProductPO> getAllProducts(); }
Service層,爲了簡單起見,我這裏就沒有寫異常處理了
/** * product service * * @author : spzh * @since : 2019-06-19 23:13 */ @Service public class ProductService { @Autowired private IProductDao productDao; public List<ProductPO> getAllProducts() { return productDao.getAllProducts(); } }
Controller層,簡單映射到products這個路徑下便可
/** * product controller * * @author : spzh * @since : 2019-06-19 23:14 */ @RestController public class ProductController { @Autowired private ProductService productService; @RequestMapping("/products") public String getAllProducts(){ List<ProductPO> allProducts = productService.getAllProducts(); return JSON.toJSONString(allProducts); } }
mapper裏面的sql很簡單,也就是一次性所有查出來,我mapper放在了resouces/mapper下了,須要在properties裏面指定位置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.zhoukk.demo.product.IProductDao"> <select id="getAllProducts" resultType="com.zhoukk.demo.product.ProductPO"> select * from product </select> </mapper>
編譯運行springboot,在瀏覽器中輸入:http://localhost:8080/products
瀏覽器中即刻顯示
本文由周框框創做, 可自由轉載、引用,但需署名做者且註明文章出處。
做者簡介:某廠的一枚程序汪,愛生活愛運動愛交流愛寫點小代碼,歡迎你任何渠道找我聊天交流,來玩哦~
創做不易,若是以爲本文對有你有幫助,能夠隨便點一下推薦,或者粉一個,或者打個賞唄~