關於數據存儲,最經常使用的方式就是存到數據庫,此篇以MySQL數據庫爲例,以mybatis框架完成數據庫的操做。java
1、添加對應依賴mysql
<!-- 數據庫:MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <!-- 數據庫操做:mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot}</version> </dependency>
(等待依賴庫下載完成之後,再繼續) spring
2、properties文件、數據庫表sql
(1)properties文件數據庫
## 數據源配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/apidemo?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=your db password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=com.univalsoft.api.springbootapimaster.entity
mybatis.mapperLocations=classpath:mapper/*.xml
(記住,在properties文件中添加了相應的配置之後,mybatis會自動讀取並完成初始化)apache
(2)數據庫表api
3、編寫對應的service/serviceImpl/dao/mapper文件springboot
(1)service(com.univalsoft.springbootapimaster.api.service.AccontService)mybatis
package com.univalsoft.springbootapimaster.api.service; import java.util.HashMap; /** * 帳號業務邏輯接口類 * <p> * Created by bysocket on 07/02/2017. */ public interface AccountService { /** * 根據用戶名查詢 * * @param username 用戶名 * @param password 密碼 */ HashMap<String, Object> findUser(String username, String password); }
(2)serviceImpl(com.univalsoft.springbootapimaster.api.service.impl.AccountServiceImpl)app
package com.univalsoft.springbootapimaster.api.service.impl; import com.univalsoft.springbootapimaster.api.dao.AccountDao; import com.univalsoft.springbootapimaster.api.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.HashMap; @Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; @Override public HashMap<String, Object> findUser(String username, String password) { return accountDao.findUserByAccount(username, password); } }
(3)dao(com.univalsoft.springbootapimaster.api.dao.AccountDao)
package com.univalsoft.springbootapimaster.api.dao; import org.apache.ibatis.annotations.Param; import java.util.HashMap; /** * 帳號 DAO 接口類 */ public interface AccountDao { /** * 查詢用戶 * * @param username 用戶名 * @param password 密碼 */ HashMap<String, Object> findUserByAccount(@Param("username") String username, @Param("password") String password); }
(4)mapper(src/main/resources/mapper/AccountMapper.xml)
<?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.univalsoft.springbootapimaster.api.dao.AccountDao">
<!-- 經過用戶名、密碼查找用戶 -->
<select id="findUserByAccount" resultType="hashmap">
select * from t_user
where 1=1
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="password != null and password != ''">
and password = #{password}
</if>
limit 1
</select>
</mapper>
(注意:紅色標註出來的,對應的dao文件,路徑必定要正確)
4、在SpringbootApiMasterApplication.java添加@MapperScan註解
5、修改Controller文件
@RequestMapping(value = "/api/test/login", method = RequestMethod.POST) public APIResponse login( @RequestParam(value = "username") String username /* 帳號 */, @RequestParam(value = "password") String password /* 密碼 */ ) { // 當前版本,username爲手機號, if (username.length() < 11) { return this.fail("請輸入正確的手機號"); } try { // password每每會加密,這裏不作加密處理 HashMap user = accountService.findUser(username, password); if (user != null) { return this.success(user); } else { return this.fail("帳號、密碼錯誤"); } } catch (Exception e) { return this.fail(""); } }
仍是那句話,看不懂的,自覺找個地方面壁去!!!
6、Postman測試一下
經過Mybatis鏈接MySQL數據庫的部分,到此結束了。
最後補充一個「招式」,經過IDEA的DB功能,加快SQL語句的書寫
(1)配置DataBase
這樣配置好之後,當在Mapper.xml中寫SQL的時候,IDEA會給出不少友好的提示,nice~~~