spring 微服务实战(1) Spring Boot 快速入门

2018-02-27 11:44:34来源:https://www.jianshu.com/p/181906950084作者:f_double人点击

分享


Spring Boot是啥?

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的Spring Boot应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑。


Spring Boot有啥好处和缺点?

1、开发简单:简化Maven及Gradle配置,尽可能的自动化配置Spring,减少大量的xml配置


2、部署简单:嵌入式Tomcat,Jetty容器,无需部署WAR包,创建独立Spring应用程序


3、监控简单:提供完善的监控服务actuator


缺点呢? 一个,你必须认同spring。


先来个例子看看效果,你会发现原来如此简单...

例子将使用spring boot + mybatis + druid + mysql
创建一个issue的maven模块项目,共有:
issue-entity,
issue-dao,
issue-service,
issue-controller,
issue-application
五个模块。


一、创建父POM:issue


创建父POM

pom.xml 内容:


<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>
<groupId>com.beg5</groupId>
<artifactId>issue</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

二、创建issue-entity子模块

创建实体类User


package com.beg5.entity;
import java.io.Serializable;
/**
*
* @ClassName: User
* @Description: TODO(用户实体类)
* @author fukailong
* @date 2018年2月13日 下午10:42:47
*
*/
public class User implements Serializable{
private static final long serialVersionUID = 1L;

private Integer id;

private String username;

private String password;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}

issue-entity的pom文件


<?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>
<parent>
<groupId>com.beg5</groupId>
<artifactId>issue</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.beg5</groupId>
<artifactId>issue-entity</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>issue-entity</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

三、创建entity-dao
package com.beg5.dao;
import com.beg5.entity.User;
/**
*
* @ClassName: UserMapper
* @Description: TODO(DAO接口)
* @author fukailong
* @date 2018年2月13日 下午10:45:36
*
*/
public interface UserMapper {
public void add(User user);
}

package com.beg5;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/**
*
* @ClassName: MybatisConfig
* @Description: TODO(mybatis配置)
* @author fukailong
* @date 2018年2月13日 下午10:46:23
*
*/
@Configuration
public class MybatisConfig {
@Value("${mybatis.config-location}")
private String mapperLocationPattern;
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return new com.alibaba.druid.pool.DruidDataSource();
}
@Bean(name="sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocationPattern));
return sqlSessionFactoryBean.getObject();
}
}

package com.beg5;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
/**
*
* @ClassName: MybatisMapperScannerConfig
* @Description: TODO(mapper扫描)
* @author fukailong
* @date 2018年2月13日 下午10:47:22
*
*/
@Configuration
@AutoConfigureAfter(MybatisConfig.class)
@MapperScan("com.beg5.dao")
public class MybatisMapperScannerConfig {
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.beg5.dao");
return mapperScannerConfigurer;
}
}

issue-dao的pom文件


<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.beg5</groupId>
<artifactId>issue</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.beg5</groupId>
<artifactId>issue-dao</artifactId>
<version>0.0.1</version>
<name>issue-dao</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.beg5</groupId>
<artifactId>issue-entity</artifactId>
<version>0.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

四、创建service模块
package com.beg5.service;
import com.beg5.entity.User;
/**
*
* @ClassName: UserService
* @Description: TODO(用户服务接口)
* @author fukailong
* @date 2018年2月13日 下午10:50:07
*
*/
public interface UserService {
public void add(User user);
}

package com.beg5.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beg5.dao.UserMapper;
import com.beg5.entity.User;
import com.beg5.service.UserService;
/**
*
* @ClassName: UserServiceImpl
* @Description: TODO(用户服务实现)
* @author fukailong
* @date 2018年2月13日 下午10:50:36
*
*/
@Service
public class UserServiceImpl implements UserService{

@Autowired
UserMapper userMapper;
@Override
public void add(User user) {
userMapper.add(user);
}
}

issue-service的pom文件


<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.beg5</groupId>
<artifactId>issue</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.beg5</groupId>
<artifactId>issue-service</artifactId>
<version>0.0.1</version>
<name>issue-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.beg5</groupId>
<artifactId>issue-dao</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>

五、创建controller模块
package com.beg5.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.beg5.entity.User;
import com.beg5.service.UserService;
/**
*
* @ClassName: UserController
* @Description: TODO(用户控制器)
* @author fukailong
* @date 2018年2月13日 下午10:53:33
*
*/
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("add")
public User add() {
System.out.println("000000000");
User user = new User();
user.setPassword("000");
user.setUsername("hello");
userService.add(user);
return user;
}
}

issue-controller的pom文件


<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.beg5</groupId>
<artifactId>issue</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.beg5</groupId>
<artifactId>issue-controller</artifactId>
<version>0.0.1</version>
<name>issue-controller</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.beg5</groupId>
<artifactId>issue-service</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>

六、创建启动应用服务application模块
package com.beg5;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
*
* @ClassName: WebApplication
* @Description: TODO(启动类)
* @author fukailong
* @date 2018年2月13日 下午10:55:32
*
*/
@ComponentScan({"com.beg5"})
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}

issue-application的pom文件


<?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>
<parent>
<groupId>com.beg5</groupId>
<artifactId>issue</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.beg5</groupId>
<artifactId>issue-application</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>issue-application</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.beg5</groupId>
<artifactId>issue-controller</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

测试服务:


package com.beg5;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.beg5.dao.UserMapper;
import com.beg5.entity.User;
/**
*
* @ClassName: WebApplicationTest
* @Description: TODO(测试类)
* @author fukailong
* @date 2018年2月13日 下午10:57:35
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTest {

@Autowired
private UserMapper userMapper;
@Test
public void test(){
User user = new User();
user.setPassword("a");

userMapper.add(user);
}
}

运行测试类:可以看到数据库插入了数据。





插入效果

启动,应用服务WebApplication , 直接运行主函数。效果如下图:





image.png

浏览器输入:http://localhost:8080/issue/user/add




image.png

好了 ,整合springboot,mybatis 和 数据源整合完成。


我是double,坐标魔都,QQ群:692595133
如果读完觉得有收获的话,欢迎点赞加关注。

github博客: https://fukailong.github.io
版权归作者所有,转载请联系作者获得授权。




公众号欢迎关注







最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台