摘要:添加用戶接口實(shí)現(xiàn)類簡(jiǎn)單用戶鏈接數(shù)據(jù)庫(kù)微服務(wù)通過(guò)注解標(biāo)注該類為持久化操作對(duì)象。添加一個(gè)使用接收請(qǐng)求添加微服務(wù)啟動(dòng)類鏈接數(shù)據(jù)庫(kù)簡(jiǎn)單的集成框架訪問(wèn)數(shù)據(jù)庫(kù)。
SpringCloud(第 043 篇)鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis框架訪問(wèn)數(shù)據(jù)庫(kù)
-
一、大致介紹1、訪問(wèn)數(shù)據(jù)庫(kù),自然少不了一些持久化框架,而我本身也是Mybatis框架的支持者; 2、Mybatis是那種專注于sql的框架,對(duì)象映射以及輸入輸出都是由框架自動(dòng)完成; 3、因此本章節(jié)就簡(jiǎn)單小試牛刀嘗試下如何使用Mybatis持久化框架進(jìn)行對(duì)數(shù)據(jù)的操作;二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包
2.2 添加應(yīng)用配置文件(springms-provider-user-mysql-mybatissrcmainresourcesapplication.yml)4.0.0 springms-provider-user-mysql-mybatis 1.0-SNAPSHOT jar com.springms.cloud springms-spring-cloud 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1
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:設(shè)為create表示每次都重新建表 # ddl-auto: update #ddl-auto:設(shè)為update表示每次都不會(huì)重新建表 # 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 添加實(shí)體用戶類User(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/entity/User.java)
package com.springms.cloud.entity; import javax.persistence.*; /** * * Mybatis 需要加上這些注解才可以使用,不然啟動(dòng)都會(huì)報(bào)錯(cuò); * */ @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") List2.5 添加用戶mapper接口實(shí)現(xiàn)類(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/mapper/user/UserMapper.java)findAllUsers(); @Insert("INSERT INTO user(username, name, age, balance) VALUES(#{username}, #{name}, #{age}, #{balance})") int insertUser(User user); }
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; /** * 簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò)@Service注解標(biāo)注該類為持久化操作對(duì)象)。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017-10-19 * */ public interface IUserService { User findUserById(Long id); List2.7 添加用戶Service接口實(shí)現(xiàn)類(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/service/impl/UserServiceImpl.java)findAllUsers(); int insertUser(User user); }
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; /** * 簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò)@Service注解標(biāo)注該類為持久化操作對(duì)象)。 * * @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 List2.8 添加用戶Web訪問(wèn)層Controller(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/controller/ProviderUserMysqlMybatisController.java)findAllUsers() { return iUserMapper.findAllUsers(); } @Override public int insertUser(User user) { return iUserMapper.insertUser(user); } }
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; /** * 用戶微服務(wù)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 List2.9 添加微服務(wù)啟動(dòng)類(springms-provider-user-mysql-mybatis/src/main/java/com/springms/cloud/MsProviderUserMysqlMybatisApplication.java)findUserList() { return this.iUserService.findAllUsers(); } /** * 添加一個(gè)student,使用postMapping接收post請(qǐng)求 * * 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; } }
package com.springms.cloud; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis框架訪問(wèn)數(shù)據(jù)庫(kù)。 * * @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ù)庫(kù)微服務(wù) 】】】】】】已啟動(dòng)."); } }三、測(cè)試
/**************************************************************************************** 注意:Mybatis 需要加上 entity 等注解才可以使用,不然啟動(dòng)都會(huì)報(bào)錯(cuò); @MapperScan("com.springms.cloud.mapper.**") 或者在每個(gè) Mapper 接口文件上添加 @Mapper 也可以; 一、簡(jiǎn)單用戶鏈接Mysql數(shù)據(jù)庫(kù)微服務(wù)(通過(guò) mybatis 鏈接 mysql 編寫數(shù)據(jù)訪問(wèn)): 1、啟動(dòng) springms-provider-user-mysql-mybatis 模塊服務(wù),啟動(dòng)1個(gè)端口; 2、在瀏覽器輸入地址 http://localhost:8325/simple/10 可以看到用戶ID=10的信息成功的被打印出來(lái); 3、使用 IDEA 自帶工具 Test Restful WebService 發(fā)送 HTTP POST 請(qǐng)求,并添加 username、age、balance三個(gè)參數(shù),然后執(zhí)行請(qǐng)求,并去 mysql 數(shù)據(jù)庫(kù)查看數(shù)據(jù)是否存在,正常情況下 mysql 數(shù)據(jù)庫(kù)剛剛插入的數(shù)據(jù)成功了: 4、使用 REST Client 執(zhí)行 "/simple/list" 接口,也正常將 mysql 數(shù)據(jù)庫(kù)中所有的用戶信息打印出來(lái)了; ****************************************************************************************/四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/67841.html
摘要:添加用戶接口類實(shí)現(xiàn)類簡(jiǎn)單用戶鏈接數(shù)據(jù)庫(kù)微服務(wù)通過(guò)注解標(biāo)注該類為持久化操作對(duì)象。添加一個(gè)使用接收請(qǐng)求添加微服務(wù)啟動(dòng)類鏈接數(shù)據(jù)庫(kù)簡(jiǎn)單的集成框架采用訪問(wèn)數(shù)據(jù)庫(kù)。 SpringCloud(第 044 篇)鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis框架采用MapperXml訪問(wèn)數(shù)據(jù)庫(kù) - 一、大致介紹 1、前面章節(jié)講解的是在方法上面添加sql語(yǔ)句操作,雖然說(shuō)僅僅只是一種簡(jiǎn)單的操作,在測(cè)試期間可...
摘要:添加用戶接口類簡(jiǎn)單用戶鏈接數(shù)據(jù)庫(kù)微服務(wù)通過(guò)注解標(biāo)注該類為持久化操作對(duì)象。查找用戶數(shù)據(jù)保存用戶數(shù)據(jù)更新用戶數(shù)據(jù)刪除用戶數(shù)據(jù)這是清除緩存添加緩存配置緩存配置。對(duì)象是否永久有效,一但設(shè)置了,將不起作用。設(shè)置對(duì)象在失效前允許存活時(shí)間單位秒。 SpringCloud(第 045 篇)鏈接Mysql數(shù)據(jù)庫(kù)簡(jiǎn)單的集成Mybatis、ehcache框架采用MapperXml訪問(wèn)數(shù)據(jù)庫(kù) - 一、大致介紹...
閱讀 2937·2021-11-23 09:51
閱讀 3109·2021-11-15 11:39
閱讀 2992·2021-11-09 09:47
閱讀 2538·2019-08-30 13:49
閱讀 2122·2019-08-30 13:09
閱讀 3107·2019-08-29 16:10
閱讀 3511·2019-08-26 17:04
閱讀 999·2019-08-26 13:57