MySQL安裝教程並使用springboot2和Mybatis測試



MySQL是什么

1561121843986

MySQL是一個關系型數據庫管理系統,由瑞典MySQL AB 公司開發,目前屬於 Oracle 旗下產品。MySQL 是最流行的關系型數據庫管理系統之一,在 WEB 應用方面,MySQL是最好的 RDBMS (Relational Database Management System,關系數據庫管理系統) 應用軟件之一。
MySQL是一種關系數據庫管理系統,關系數據庫將數據保存在不同的表中,而不是將所有數據放在一個大倉庫內,這樣就增加了速度並提高了靈活性。
MySQL所使用的 SQL 語言是用於訪問數據庫的最常用標准化語言。MySQL 軟件采用了雙授權政策,分為社區版和商業版,由於其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,一般中小型網站的開發都選擇 MySQL 作為網站數據庫。

---引用自百度百科

MySQL安裝

MySQL的安裝可以是直接ZIP解壓安裝,手動配置后就可以使用,還可以通過安裝包安裝,本機為win10的操作系統,采用MySQL Installer來安裝即可,安裝的版本為MySQL Installer 8.0.16,如下,直接下載32bit版本即可,暗轉的時候安裝導航會自動識別幫助安裝64bit,如果你的機器是64位的話;

1561122270595

進入安裝界面,選擇Custom,用於開發的話,很多東西我們不需要安裝;

mysql安裝1

選擇要安裝的包,記住勾選workbench,這是MySQL官方出的GUI管理工具

mysql安裝2

下一步

mysql安裝4mysql安裝3

下一步

mysql安裝5

開始進行配置選擇,單體應用的話選第一個選項

mysql安裝6

選擇開發電腦,端口號默認即可,盡量不改mysql安裝7

安全性考慮,選擇需要輸入賬號密碼

mysql安裝8

mysql安裝9

選擇創建Mysql服務

mysql安裝10

mysql安裝11

創建成功,輸入賬號密碼check一下

mysql安裝12

mysql安裝13

安裝成功了

mysql安裝14

開始使用一下MySQL

打開剛才勾選安裝的shell,看看版本信息是否安裝成功了,如下,MySQL Shell 的版本為8.0.16

mysql安裝15

打開workbench,界面很清爽,看起來還不錯吧,可以看到已經創建了一個默認的實例MySQL80

mysql安裝16

打開這個是庫里面的,打開world這個shema,隨便選一個表來看,就city好了,有name字段/countrycode字段等等

mysql安裝17

用spring boot2+Mybatis試試MySQL

這里不詳細敘述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);

拉通spring boot2+mybatis

先講下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

1561127919240

瀏覽器中即刻顯示

1561127983873


本文由周框框創作, 可自由轉載、引用,但需署名作者且注明文章出處。

作者簡介:某廠的一枚程序汪,愛生活愛運動愛交流愛寫點小代碼,歡迎你任何渠道找我聊天交流,來玩哦~

創作不易,如果覺得本文對有你有幫助,可以隨便點一下推薦,或者粉一個,或者打個賞唄~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM