最近在公司太清闲了,为了打发我过剩的精力,我逛起了微信上关注的几个关于java技术的公众号,有一篇文章上《Java工程师技能表》,上边的技能排名挺有意思的:
其他技能即使在公司项目中没用过,但是咱最少自学过,有点了解。但是Spring Boot竟然排名这么高,那我今天就来入入门。
什么是Spring Boot?特点是什么?
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
特点:
1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对XML没有要求配置
-------------------------------------
Spring Boot配置简单、功能齐全,如果我们只需要开发一个功能简单的程序,就可以使用Spring Boot,省去繁琐的框架整合时间。
下面就开始进行一个Spring Boot demo的创建吧!
第一步:下载压缩包,导入项目。
下载地址: http://start.spring.io/
下载获得: demo.zip 解压缩并导入到项目中。
第二步:编写相关代码。
1.pom.xml
1 24 4.0.0 5 6com.example 7demo 80.0.1-SNAPSHOT 9jar 10 11demo 12Demo project for Spring Boot 13 1415 20 21org.springframework.boot 16spring-boot-starter-parent 171.5.2.RELEASE 1819 22 26 27UTF-8 23UTF-8 241.8 2528 61 6229 32 33org.springframework.boot 30spring-boot-starter 3134 38 39 40org.springframework.boot 35spring-boot-starter-test 36test 3741 44 45 46org.springframework.boot 42spring-boot-starter-web 4347 51org.mybatis.spring.boot 48mybatis-spring-boot-starter 491.1.1 5052 55mysql 53mysql-connector-java 5456 60org.springframework.boot 57spring-boot-devtools 58true 5963 70 71 7264 6965 68org.springframework.boot 66spring-boot-maven-plugin 67
2.application.properties
1 mybatis.type-aliases-package=com.example.myproject.entity2 3 spring.datasource.driverClassName = com.mysql.jdbc.Driver4 spring.datasource.url = jdbc:mysql://localhost:3306/tao?useUnicode=true&characterEncoding=utf-85 spring.datasource.username = root6 spring.datasource.password = 123
3.实体类
1 package com.example.myproject.entity; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private String username; 7 private String password; 8 private String name; 9 private String email;10 public String getUsername() {11 return username;12 }13 public void setUsername(String username) {14 this.username = username;15 }16 public String getPassword() {17 return password;18 }19 public void setPassword(String password) {20 this.password = password;21 }22 public String getName() {23 return name;24 }25 public void setName(String name) {26 this.name = name;27 }28 public String getEmail() {29 return email;30 }31 public void setEmail(String email) {32 this.email = email;33 }34 35 }
4.DAO层代码
1 package com.example.myproject.dao; 2 3 import java.util.List; 4 5 import org.apache.ibatis.annotations.Delete; 6 import org.apache.ibatis.annotations.Insert; 7 import org.apache.ibatis.annotations.Select; 8 import org.apache.ibatis.annotations.Update; 9 10 import com.example.myproject.entity.User;11 12 public interface UserDao {13 @Select("SELECT * FROM user")14 ListgetAll();15 16 @Select("SELECT * FROM user WHERE username = #{username}")17 User getOne(String username);18 19 @Insert("INSERT INTO user(username,password,name,email) VALUES(#{username}, #{password}, #{name}, #{email})")20 void insert(User user);21 22 @Update("UPDATE user SET name=#{name} WHERE username =#{username}")23 void updateNameByUsername(User user);24 25 @Delete("DELETE FROM user WHERE username =#{username}")26 void delete(String username);27 }
5.service层代码
1 package com.example.myproject.service; 2 3 import java.util.List; 4 5 import com.example.myproject.entity.User; 6 7 public interface UserService { 8 /** 9 * 查询全部user10 * @return11 */12 ListgetAll();13 /**14 * 根据username查询一条user15 * @param username16 * @return17 */18 User getOne(String username);19 /**20 * 插入一条user21 * @param user22 */23 void insert(User user);24 /**25 * 根据username查到user,修改name26 * @param user27 */28 void updateNameByUsername(User user);29 /**30 * 删除user31 * @param username32 */33 void delete(String username);34 }
1 package com.example.myproject.service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.example.myproject.dao.UserDao; 9 import com.example.myproject.entity.User;10 import com.example.myproject.service.UserService;11 12 @Service("userService")13 public class UserServiceImpl implements UserService {14 15 16 @Autowired17 private UserDao userDao;18 19 @Override20 public ListgetAll() {21 return userDao.getAll();22 }23 24 @Override25 public User getOne(String username) {26 return userDao.getOne(username);27 }28 29 @Override30 public void insert(User user) {31 userDao.insert(user);32 }33 34 @Override35 public void updateNameByUsername(User user) {36 userDao.updateNameByUsername(user);37 }38 39 @Override40 public void delete(String username) {41 userDao.delete(username);42 }43 44 }
6.controller代码:
1 package com.example.myproject.action; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 import org.springframework.web.bind.annotation.RestController;10 11 import com.example.myproject.entity.User;12 import com.example.myproject.service.UserService;13 14 @Controller15 public class HelloWorldController {16 17 18 @Autowired(required = false)19 private UserService userService;20 21 22 @RequestMapping("/getAll")23 public @ResponseBody ListgetAll() {24 return userService.getAll();25 }26 27 @RequestMapping("/getOne")28 public @ResponseBody User getOne(String username) {29 return userService.getOne(username);30 }31 32 @RequestMapping("/insert")33 public @ResponseBody User insert() {34 User user = new User();35 user.setUsername("zs");36 user.setPassword("123456");37 user.setName("zhangsan");38 user.setEmail("123@qq.com");39 userService.insert(user);40 return userService.getOne("zs");41 }42 43 @RequestMapping("/updateNameByUsername")44 public @ResponseBody User updateNameByUsername() {45 User user = new User();46 user.setUsername("zs");47 user.setName("zs1111");48 userService.updateNameByUsername(user);49 return userService.getOne("zs");50 }51 52 @RequestMapping("/delete")53 public @ResponseBody List delete() {54 userService.delete("zs");55 return userService.getAll();56 }57 }
7.运行代码修改:
1 package com.example; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 //地址到dao层接口处,否则报错 9 @MapperScan("com.example.myproject.dao")10 public class DemoApplication {11 12 public static void main(String[] args) {13 SpringApplication.run(DemoApplication.class, args);14 }15 }
至此,只要运行这个main方法,访问 localhost:8080/getAll 就可以运行了。
简单的试用了一下Spring Boot,确实被它简单的配置给惊到了,看来以后自己学点学习的demo时,就用它吧!哈哈。。。
感谢以下博文:
http://www.cnblogs.com/ityouknow/p/6037431.html
http://www.cnblogs.com/ityouknow/p/5662753.html