當前位置:
首頁 > 最新 > Spring boot學習

Spring boot學習

前段時間剛剛把iOS基礎學完,感覺沒太大意思。現在開始玩微服務。在Java圈內,大家都知道,國外的Spring Cloud和國內的Dubbo是兩大框架,不過Dubbo社區活躍度沒有Spring Cloud這麼高。而且嚴格來講,Dubbo只能算是Spring Cloud服務管理的一部分。具體的情況等學完再來評論。當然,微服務還是選擇Spring Cloud啊,Spring在Java Web的影響已經無需言語了。Spring boot只能算是微服務的一個小部分,慢慢來。與正常開發的SSM這些比較而言,Spring boot做了封裝,簡化了很多。並且配置文件也沒那麼一大串了,每次寫項目copy配置文件都煩了。

創建項目

官方已經介紹的很好了。我們直接來建立項目。

加入maven依賴 org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin 創建Application@SpringBootApplication public class Application{ public static void main(String[] args){ SpringApplication.run(Application.class,args); } }創建介面@Controller @RequestMapping("/user/") public class UserController{ @RequestMapping(value = "info.do",method = RequestMethod.GET) @ResponseBody publicUsergetUser(){ User user = new User(); user.setId(1); user.setUserName("哈哈哈"); user.setPhone("15708443946"); return user; } }試試看

用命令試試看:

有沒有很爽,簡直爽的不要不要的,再也沒有一大堆的配置。乾乾淨淨。強迫症患者剛需。

Mybatis支持

集成Mybatis簡直累死我了,網上一大堆那種把xml和mapper介面寫一起,看起來很不爽,畢竟這樣展現不出mybatis的舒爽啊。想分離開,看了好久才配置正確。我們來配配

添加依賴 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0 mysql mysql-connector-java 5.1.21 配置application.properties

創建一個配置文件,加入mysql和mybatis的配置

# mybatis # 配置model mybatis.type-aliases-package=tech.jiangtao.boot.pojo # 配置mapper xml文件目錄 mybatis.mapper-locations=classpath:mappers/*.xml # mysql spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mmall?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = root加入掃描器掃描mapper

在啟動類Application加入

開發Controller/** * 登錄介面 * @param username * @param password * @param session * @return */ @RequestMapping(value = "login.do",method = RequestMethod.POST) @ResponseBody publicServerResponselogin(String username,String password,HttpSession session){ ServerResponse response = iUserService.login(username,password); // todo 將用戶信息放入session中 if (response.isSuccess()) { session.setAttribute(Const.CURRENT_USER,response.getData()); } return response; }開發Service@Override publicServerResponselogin(String username, String password){ int resultCount = userMapper.checkUserName(username); if (resultCount == 0) { return ServerResponse.createByErrorCodeMsg(400, "用戶名不存在"); } //todo 密碼登錄md5 String md5Password = MD5Util.MD5EncodeUtf8(password); User user = userMapper.selectLogin(username, md5Password); if (user == null) { return ServerResponse.createByErrorCodeMsg(401, "密碼錯誤"); } // todo 在這兒就把置空,不要將密碼發給移動端 user.setPassword(StringUtils.EMPTY); return ServerResponse.createByMsgAndDataSuccess("登錄成功", user); }

mapper和xml文件就不貼了,跟平常的ssm開發一模一樣。最後看看返回的數據

結果

是不是想抽根煙慶祝一下。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 推酷 的精彩文章:

perseus:基於 Mybatis+Spring 的讀寫分離方案
深度學習思考
This 帶來的困惑
親手教的AI總是放心些
淺析Node與Element

TAG:推酷 |

您可能感興趣

Spring data MongoDB 之 MongoRepository
Spring中BeanFactory和ApplicationContext 的區別
深入 SpringBoot : 怎樣排查 expectedsinglematchingbeanbutfound 2 的異常
帶著新人學springboot的應用04(springboot+mybatis+redis 完)
Spring Boot 入門學習
Spring Boot 配置文件 yml與properties
SpringBoot:SpringDataRedis緩存改造
Spring Security 5.0 的 DelegatingPasswordEncoder 詳解
springboot:使用Spring Boot Actuator監控應用
Spring Boot與Kotlin使用Spring-data-jpa簡化數據訪問層
Springboot2.X之切換使用Servlet容器Jetty、Tomcat、Undertow
深入 JVM 分析 spring-boot 應用 hibernate-validatorNoClassDefFoundError
Spring Cloud Alibaba Sentinel 整合 Feign 的設計實現
Spring4+hibernate+SpringMvc整合
spring cloud netflix:斷路器之Hystrix Dashboard
Spring整合Hibernate.Final
回顧La Perla』s spring/summer 2018 collections at The Venetian
Dropwizard與Spring Boot比較
網關 Spring-Cloud-Gateway 源碼解析——路由之RouteDefinitionLocator一覽
SpringBoot伺服器壓測對比(jetty、tomcat、undertow)