Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
特点
1. 轻松创建独立的Spring应用程序
2. 嵌入的Tomcat、jetty等web容器,无需部署WAR文件
3. 提供一系列的“starter” 来简化的Maven配置
4. 开箱即用,尽可能自动配置Spring
环境搭建
用IDEA创建一个新项目File-->New Project-->Spring Initializr
Next-->填写Group和Artifact
Next-->选择项目需要的依赖
Next-->填写完项目名称和选择项目位置之后点击Finish
等待编译完成,运行,出现如下图说明搭建成功
完善一下目录结构:
创建一个Hello World Controller作为项目的入口
1 @Controller2 @EnableAutoConfiguration3 public class HelloWorld {4 @RequestMapping("/")5 @ResponseBody6 String home(){7 return "Hello World!";8 }9 }
运行http://localhost:8080/出现
JDBC连接数据库
项目不使用application.properties文件 而使用更加简洁的application.yml文件: 将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件, 文件的内容如下:
server: port: 9091spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&characterEncoding=UTF-8 password: root username: root driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSourcemybatis: type-aliases-package: com.example.demo.model mapper-locations: classpath:mappers/*.xml
pom.xml
4.0.0 com.example springbootdemo 0.0.1-SNAPSHOT jar springbootdemo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 com.alibaba druid 1.1.8 com.alibaba fastjson 1.2.47 mysql mysql-connector-java runtime org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
使用mybatis generator 自动生成代码:/src/main/resources/generator/generatorConfig.xml
Mybatisgenerator
public class MybatisGenerator { public static void main(String[] args) throws Exception{ generator("D:\\springbootdemo\\src\\main\\resources\\generator\\generatorConfig.xml"); } public static void generator(String path) throws Exception{ Listwarnings = new ArrayList (); boolean overwrite = true; File configFile = new File(path); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); }}
运行Mybatisgenerator
项目启动类
@SpringBootApplication@MapperScan("com.example.demo.mapper")public class SpringbootdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); }}
Mapper
public interface GreenIndexSystemMapper { GreenIndexSystem selectByPrimaryKey(Integer id); ListselectAll();}
映射文件Mapper.xml
Service
public interface GreenIndexService { ListselectAll();}
ServiceImpl
@Service(value = "greenService")public class GreenIndexServiceImpl implements GreenIndexService { @Autowired private GreenIndexSystemMapper greenIndexSystemMapper; @Override public ListselectAll() { return greenIndexSystemMapper.selectAll(); }}
Controller
@RestController@RequestMapping("/green/")public class GreenIndexController { @Autowired private GreenIndexService greenService;@RequestMapping("getinfo")public ListgetGreenAll(){ return greenService.selectAll();}}
运行启动类查询到结果