服務(wù)器這邊想做一個(gè)簡(jiǎn)單的點(diǎn)菜系統(tǒng),打算采用mysql+mybatis方式來(lái)做數(shù)據(jù)之間的操作。
1、創(chuàng)建項(xiàng)目
通過(guò)網(wǎng)站創(chuàng)建一個(gè)spring boot項(xiàng)目

創(chuàng)建完成后,使用IntelliJ IDEA打開項(xiàng)目,完成自動(dòng)導(dǎo)包過(guò)程。
2、創(chuàng)建數(shù)據(jù)庫(kù)
進(jìn)入終端
mysql -u root -p
create database OrderDB;
3、創(chuàng)建一張用戶表user,表內(nèi)字段有id、name、age、tel、pwd

手動(dòng)插入一條測(cè)試數(shù)據(jù)

4、開發(fā)前的準(zhǔn)備工作
在resources目錄下創(chuàng)建mybatis文件夾,里面創(chuàng)建兩個(gè)文件,mybatis.properties和mybatis-config.xml文件。
mybatis.properties里面存放鏈接數(shù)據(jù)庫(kù)的4大要素

mybatis-config.xml里存放mybatis的全局配置信息,打開官網(wǎng),可以從Getting Started和Configuration XML,找到所有需要的配置信息。注:這里有中文版可以選擇,下圖是最基本也是最重要的幾個(gè)配置項(xiàng),至于其它配置項(xiàng),請(qǐng)參考文檔,還有就是注意每個(gè)配置項(xiàng)的順序問(wèn)題。

由上圖可以看出,我們要綁定一個(gè)mapper,因此需要在resources/mybatis下創(chuàng)建一個(gè)mapper文件夾,在里面再創(chuàng)建一個(gè)UserMapper.xml文件

這里需要注意一下mapper的namespace,需要?jiǎng)?chuàng)建一個(gè)接口類,接口方法名要跟當(dāng)前UserMapper.xml的sql一致。

接口類里先寫一個(gè)通過(guò)id查詢User的方法,User是一個(gè)User表的一個(gè)實(shí)體對(duì)象,里面的字段與表一致。

這里需要自己把每條屬性的get、set方法補(bǔ)齊,如果需要打印,再把toString方法補(bǔ)上。
接下來(lái)為了業(yè)務(wù)需要,最好寫一個(gè)UserServiceImpl,用來(lái)實(shí)現(xiàn)接口的業(yè)務(wù)邏輯。

如果是新手朋友,這里要注意UserMapper上面的@Autowired,還有類上面的@Service。
5、編寫功能接口
創(chuàng)建UserController類

在運(yùn)行前,記得要在Application類里把Mapper掃包的功能加上,不然可運(yùn)行不起來(lái)的

基本的目錄結(jié)構(gòu)請(qǐng)查看下圖

6、運(yùn)行時(shí)出現(xiàn)的錯(cuò)誤及解決辦法
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
當(dāng)出現(xiàn)這樣的信息時(shí),需要在application.properties里配置spring.datasource.driver-class-name、spring.datasource.url、spring.datasource.username、spring.datasource.password。
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.hailong.order.mapper.UserMapper.getUserById
當(dāng)出現(xiàn)這樣的信息時(shí),說(shuō)明我們的工程沒(méi)有找到UserMapper里的方法,首先檢查一下在UserMapper.xml里mapper的namespace路徑對(duì)不對(duì),如果沒(méi)問(wèn)題,那就可能是根本沒(méi)有找到UserMapper這個(gè)接口類,那我們就再檢查一下mybatis-config.xml這個(gè)全局配置文件是否正確,如果mapper的resource路徑也沒(méi)問(wèn)題,那基本就可以確定了,我們好像忘記了沒(méi)有把這個(gè)xml綁定,在application.properties里加入
mybatis.config-location=classpath:mybatis/mybatis-config.xml
再運(yùn)行一下

ok,完全沒(méi)問(wèn)題。
7、數(shù)據(jù)庫(kù)操作時(shí)遇到的問(wèn)題
????7.1、Post過(guò)來(lái)多個(gè)參數(shù)時(shí),如果UserMapper.java類里,方法的接收參數(shù)沒(méi)有使用@Param注解,那么在UserMapper.xml的Sql里要對(duì)應(yīng)寫成#{arg0},#{arg1}這樣。不然會(huì)報(bào)以下錯(cuò)誤。
"nested exception is org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg3, arg2, arg1, arg0, param3, param4, param1, param2]"
? ? 如果有@Param注解,寫成#{name},#{age}就不會(huì)有問(wèn)題了。
????7.2、像寫insert或update這種Sql的時(shí)候,如果表里帶有自增的key的話,需要把對(duì)應(yīng)的key和value都寫上否則會(huì)提示錯(cuò)誤
### Error updating database. Cause: java.sql.SQLException: Column count doesn't match value count at row 1\n### The error may involve defaultParameterMap\n### The error occurred while setting parameters\n### SQL: INSERT INTO USER VALUES (?, ?, ?, ?)\n### Cause: java.sql.SQLException: Column count doesn't match value count at row 1\n; bad SQL grammar []; nested exception is java.sql.SQLException: Column count doesn't match value count at row 1
????7.3、插入數(shù)據(jù)庫(kù)的數(shù)據(jù)為中文,存入數(shù)據(jù)庫(kù)的是亂碼,在網(wǎng)上找了好多方法各種試,有些命令不好使,有些文件不存在,如果你試過(guò)各種方法仍然不起作用的話,請(qǐng)參考這里,直接在/etc下創(chuàng)建my.cnf文件,并寫入以下內(nèi)容,親測(cè)可用。
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
8、密碼相關(guān)的都是密文的,應(yīng)該做加密處理后再存入數(shù)據(jù)庫(kù)。就需要?jiǎng)?chuàng)建一個(gè)Md5的工具類,度娘一搜很多,就不貼在這了,見下圖

9、還需要填補(bǔ)的接口
addUser、removeUser、updateUser,還有菜單相關(guān)的表等等
畢竟是一個(gè)點(diǎn)菜系統(tǒng)嘛,大家可以在后續(xù)補(bǔ)充的時(shí)候,盡情發(fā)揮自己的想像,盡量把功能做的完善一些,在此就不往上堆沒(méi)用的東西了。
請(qǐng)自行完成 ^-^