SpringCloud(第 043 篇)鏈接Mysql數(shù)據(jù)庫簡單的集成Mybatis框架訪問數(shù)據(jù)庫
一、大致介紹
1寝并、訪問數(shù)據(jù)庫,自然少不了一些持久化框架,而我本身也是Mybatis框架的支持者惦界;
2、Mybatis是那種專注于sql的框架咙冗,對象映射以及輸入輸出都是由框架自動完成沾歪;
3、因此本章節(jié)就簡單小試牛刀嘗試下如何使用Mybatis持久化框架進行對數(shù)據(jù)的操作雾消;
二灾搏、實現(xiàn)步驟
2.1 添加 maven 引用包
<?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>
<artifactId>springms-provider-user-mysql-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.springms.cloud</groupId>
<artifactId>springms-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 訪問數(shù)據(jù)庫模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- web模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Mybatis依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
2.2 添加應用配置文件(springms-provider-user-mysql-mybatis\src\main\resources\application.yml)
server:
port: 8325
spring:
application:
name: springms-provider-user-mysql-mybatis #全部小寫
#####################################################################################################
# mysql 屬性配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://ip:3306/hmilyylimh
username: username
password: password
# jpa:
# hibernate:
# #ddl-auto: create #ddl-auto:設為create表示每次都重新建表
# ddl-auto: update #ddl-auto:設為update表示每次都不會重新建表
# show-sql: true
#####################################################################################################
#####################################################################################################
# 打印日志
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
com.springms: DEBUG
#####################################################################################################
2.3 添加實體用戶類User(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/entity/User.java)
package com.springms.cloud.entity;
import javax.persistence.*;
/**
*
* Mybatis 需要加上這些注解才可以使用挫望,不然啟動都會報錯;
*
*/
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Integer age;
@Column
private String balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getBalance() {
return this.balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
}
2.4 添加用戶mapper接口(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/mapper/IUserMapper.java)
package com.springms.cloud.mapper;
import com.springms.cloud.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 用戶 mybatis 映射文件狂窑。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017-10-19
*
*/
public interface IUserMapper {
@Select("select * from user where id = #{id}")
User findUserById(Long id);
@Select("select * from user")
List<User> findAllUsers();
@Insert("INSERT INTO user(username, name, age, balance) VALUES(#{username}, #{name}, #{age}, #{balance})")
int insertUser(User user);
}
2.5 添加用戶mapper接口實現(xiàn)類(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/mapper/user/UserMapper.java)
package com.springms.cloud.mapper.user;
/**
*
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017-10-19
*
*/
public class UserMapper {
}
2.6 添加用戶Service接口類(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/service/IUserService.java)
package com.springms.cloud.service;
import com.springms.cloud.entity.User;
import java.util.List;
/**
* 簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(通過@Service注解標注該類為持久化操作對象)媳板。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017-10-19
*
*/
public interface IUserService {
User findUserById(Long id);
List<User> findAllUsers();
int insertUser(User user);
}
2.7 添加用戶Service接口實現(xiàn)類(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/service/impl/UserServiceImpl.java)
package com.springms.cloud.service.impl;
import com.springms.cloud.entity.User;
import com.springms.cloud.mapper.IUserMapper;
import com.springms.cloud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(通過@Service注解標注該類為持久化操作對象)。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017-10-19
*
*/
@Service
public class UserServiceImpl implements IUserService {
@Autowired
IUserMapper iUserMapper;
@Override
public User findUserById(Long id) {
return iUserMapper.findUserById(id);
}
@Override
public List<User> findAllUsers() {
return iUserMapper.findAllUsers();
}
@Override
public int insertUser(User user) {
return iUserMapper.insertUser(user);
}
}
2.8 添加用戶Web訪問層Controller(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/controller/ProviderUserMysqlMybatisController.java)
package com.springms.cloud.controller;
import com.springms.cloud.entity.User;
import com.springms.cloud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用戶微服務Controller泉哈。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017-10-19
*
*/
@RestController
public class ProviderUserMysqlMybatisController {
@Autowired
private IUserService iUserService;
@GetMapping("/simple/{id}")
public User findUserById(@PathVariable Long id) {
return this.iUserService.findUserById(id);
}
@GetMapping("/simple/list")
public List<User> findUserList() {
return this.iUserService.findAllUsers();
}
/**
* 添加一個student,使用postMapping接收post請求
*
* http://localhost:8310/simple/addUser?username=user11&age=11&balance=11
*
* @return
*/
@PostMapping("/simple/addUser")
public User addUser(@RequestParam(value = "username", required=false) String username, @RequestParam(value = "age", required=false) Integer age, @RequestParam(value = "balance", required=false) String balance){
User user=new User();
user.setUsername(username);
user.setName(username);
user.setAge(age);
user.setBalance(balance);
int result = iUserService.insertUser(user);
if(result > 0){
return user;
}
user.setId(0L);
user.setName(null);
user.setUsername(null);
user.setBalance(null);
return user;
}
}
2.9 添加微服務啟動類(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/MsProviderUserMysqlMybatisApplication.java)
package com.springms.cloud;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 鏈接Mysql數(shù)據(jù)庫簡單的集成Mybatis框架訪問數(shù)據(jù)庫蛉幸。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017-10-19
*
*/
@SpringBootApplication
@MapperScan("com.springms.cloud.mapper.**")
public class MsProviderUserMysqlMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MsProviderUserMysqlMybatisApplication.class, args);
System.out.println("【【【【【【 鏈接MysqlMybatis數(shù)據(jù)庫微服務 】】】】】】已啟動.");
}
}
三、測試
/****************************************************************************************
注意:Mybatis 需要加上 entity 等注解才可以使用丛晦,不然啟動都會報錯奕纫;
@MapperScan("com.springms.cloud.mapper.**") 或者在每個 Mapper 接口文件上添加 @Mapper 也可以;
一烫沙、簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(通過 mybatis 鏈接 mysql 編寫數(shù)據(jù)訪問):
1若锁、啟動 springms-provider-user-mysql-mybatis 模塊服務,啟動1個端口斧吐;
2又固、在瀏覽器輸入地址 http://localhost:8325/simple/10 可以看到用戶ID=10的信息成功的被打印出來;
3煤率、使用 IDEA 自帶工具 Test Restful WebService 發(fā)送 HTTP POST 請求,并添加 username仰冠、age、balance三個參數(shù)蝶糯,然后執(zhí)行請求洋只,并去 mysql 數(shù)據(jù)庫查看數(shù)據(jù)是否存在,正常情況下 mysql 數(shù)據(jù)庫剛剛插入的數(shù)據(jù)成功了:
4昼捍、使用 REST Client 執(zhí)行 "/simple/list" 接口识虚,也正常將 mysql 數(shù)據(jù)庫中所有的用戶信息打印出來了;
****************************************************************************************/
四妒茬、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關注担锤,您的肯定是對我最大的支持!!!