摘要:上一篇學(xué)習(xí)了服務(wù)提供者,但是并不是單單就學(xué)習(xí)了服務(wù)提供者。中間還穿插使用了數(shù)據(jù)源和整合。但是上篇使用時(shí)還是沿用了老的方式,需要配置對(duì)應(yīng)的文件。
1、上一篇學(xué)習(xí)了服務(wù)提供者provider,但是并不是單單就學(xué)習(xí)了服務(wù)提供者。中間還穿插使用了Hikari數(shù)據(jù)源和spring cloud整合mybatis。但是上篇使用mybatis時(shí)還是沿用了老的方式,需要配置mapper對(duì)應(yīng)的xml文件。先來看看上篇使用mybatis的主要步驟
一、 pom.xml文件引用
org.mybatis mybatis-spring 1.3.2 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2
二、 application.yml配置文件加入mybtias配置項(xiàng)
mybatis: mapperLocations: classpath:sc/provider/dao/*.xml #configLocation: classpath:mybatis-config.xml
三、 編寫mapper文件user-mapper.xml
insert into t_user ( id, userName, age, position ) values ( #{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{position,jdbcType=VARCHAR} ) update t_user set userName = #{userName,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, position = #{position,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} delete from t_user where id = #{id,jdbcType=INTEGER}
四、 編寫UserDao.java
package sc.provider.dao; import java.util.List; import sc.provider.model.User; public interface UserDao { User getUser(Long id); ListlistUser(); int addUser(User user); int updateUser(User user); int deleteUser(Long id); }
五、 在ProviderApplication.java添加@MapperScan(basePackages="sc.provider.dao")
經(jīng)過上面五個(gè)步驟才能使用mybatis。本篇將和大家看看不能簡化spring cloud 整合mybatis的步驟(在sc-eureka-client-provider工程上改造)
一、 依賴必不可少
org.mybatis mybatis-spring 1.3.2 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2
二、 刪除application.yml關(guān)于mybatis的配置
三、 刪除mapper文件user-mapper.xml文件
四、 改造UserDao.java類
package sc.provider.dao; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import sc.provider.model.User; @Mapper public interface UserDao { @Select(value="select id, userName, age, position from t_user where id = #{id,jdbcType=INTEGER}") User getUser(Long id); @Select(value="select id, userName, age, position from t_user") ListlistUser(); @Insert(value="insert into t_user (id, userName, age, position) values ( #{id,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR},#{age,jdbcType=INTEGER},#{position,jdbcType=VARCHAR})") int addUser(User user); @Update(value="update t_user set userName = #{userName,jdbcType=VARCHAR},age = #{age,jdbcType=INTEGER},position = #{position,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER}") int updateUser(User user); @Delete(value=" delete from t_user where id = #{id,jdbcType=INTEGER}") int deleteUser(Long id); }
五、 @MapperScan注解必不可少
package sc.provider; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @MapperScan(basePackages="sc.provider.dao") public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
經(jīng)過以上步驟就把使用xml方式的mybatis改造成使用annotation方式的mybatis了。
2、啟動(dòng)注冊(cè)中心sc-eureka-server,啟動(dòng)sc-eureka-client-provider-annotation(使用sc-eureka-client-provider項(xiàng)目改造),驗(yàn)證是否改造成功
方式一:
方式二:
圈住的名字是在application.yml配置的
3、使用postman方法相應(yīng)restful接口,這里就不一一訪問了,可以參考上一篇文章的訪問方式
添加:
http://127.0.0.1:8300/user/addUser
查詢:
http://127.0.0.1:8300/user/getUser/4
列表:
http://127.0.0.1:8300/user/listUser
更新:
http://127.0.0.1:8300/user/updateUser
刪除:
http://127.0.0.1:8300/user/deleteUser/2
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/69367.html
摘要:而在這個(gè)微服務(wù)下,同樣需要進(jìn)行數(shù)據(jù)操作,我不可能還要在下再一次進(jìn)行集成,這樣大大的增加了代碼量。其次,是將有關(guān)數(shù)據(jù)操作的都單獨(dú)部署成一個(gè)模塊,比如我集成的模塊,集成的模塊,使用作為內(nèi)存緩存模塊。 前言 相對(duì)于 spring 對(duì) mybatis 以及 redis 等的整合所需要的各種配置文件,在 springboot 下,已經(jīng)大大的簡化了,你可能只是需要增加個(gè)依賴,加個(gè)注解,然后在配置文...
摘要:哪吒社區(qū)技能樹打卡打卡貼函數(shù)式接口簡介領(lǐng)域優(yōu)質(zhì)創(chuàng)作者哪吒公眾號(hào)作者架構(gòu)師奮斗者掃描主頁左側(cè)二維碼,加入群聊,一起學(xué)習(xí)一起進(jìn)步歡迎點(diǎn)贊收藏留言前情提要無意間聽到領(lǐng)導(dǎo)們的談話,現(xiàn)在公司的現(xiàn)狀是碼農(nóng)太多,但能獨(dú)立帶隊(duì)的人太少,簡而言之,不缺干 ? 哪吒社區(qū)Java技能樹打卡?【打卡貼 day2...
摘要:服務(wù)的指定位置不同,是在注解上聲明,則是在定義抽象方法的接口中使用聲明。調(diào)用方式不同需要自己構(gòu)建請(qǐng)求,模擬請(qǐng)求然后使用發(fā)送給其他服務(wù),步驟相當(dāng)繁瑣。 1.什么是Springboot? 用來簡化spring應(yīng)用的初始搭建以及開發(fā)過程 使用特定的方式來進(jìn)行配置(properties或yml文件) 創(chuàng)建獨(dú)立的spring引用程序 main方法運(yùn)行 嵌入的Tomcat 無需部署war文件 簡...
閱讀 3188·2021-11-24 09:38
閱讀 1415·2021-09-22 15:27
閱讀 3045·2021-09-10 10:51
閱讀 1576·2021-09-09 09:33
閱讀 991·2021-08-09 13:47
閱讀 2163·2019-08-30 13:05
閱讀 955·2019-08-29 15:15
閱讀 2624·2019-08-29 12:21