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.xml 内容:
4.0.0
com.beg5
issue
0.0.1
pom
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-maven-plugin
二、创建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文件
4.0.0
com.beg5
issue
0.0.1
com.beg5
issue-entity
0.0.1
jar
issue-entity
Demo project for Spring Boot
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-maven-plugin
三、创建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文件
4.0.0
com.beg5
issue
0.0.1
com.beg5
issue-dao
0.0.1
issue-dao
http://maven.apache.org
UTF-8
com.beg5
issue-entity
0.0.1
com.alibaba
druid
1.1.8
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
mysql
mysql-connector-java
5.1.45
org.springframework.boot
spring-boot-maven-plugin
四、创建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文件
4.0.0
com.beg5
issue
0.0.1
com.beg5
issue-service
0.0.1
issue-service
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
com.beg5
issue-dao
0.0.1
五、创建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文件
4.0.0
com.beg5
issue
0.0.1
com.beg5
issue-controller
0.0.1
issue-controller
http://maven.apache.org
UTF-8
com.beg5
issue-service
0.0.1
六、创建启动应用服务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文件
4.0.0
com.beg5
issue
0.0.1
com.beg5
issue-application
0.0.1
jar
issue-application
Demo project for Spring Boot
UTF-8
UTF-8
1.8
com.beg5
issue-controller
0.0.1
org.springframework.boot
spring-boot-maven-plugin
测试服务:
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 , 直接运行主函数。效果如下图:
浏览器输入:http://localhost:8080/issue/user/add
好了 ,整合springboot,mybatis 和 数据源整合完成。
我是double,坐标魔都,QQ群:692595133
如果读完觉得有收获的话,欢迎点赞加关注。
github博客: https://fukailong.github.io
版权归作者所有,转载请联系作者获得授权。