SpringBoot+Maven项目实战(2):集成SpringBoot
来源:互联网 发布:db2 恢复数据库 编辑:程序博客网 时间:2024/04/30 15:13
项目结构图
1.pom文件添加SpingBoot依赖
<?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> <groupId>com.sbm</groupId> <artifactId>SpringBoot_Maven</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</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> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.43</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.3.RELEASE</version> </dependency> </dependencies><build><plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.3.RELEASE</version> </dependency> </dependencies> </plugin></plugins></build> <repositories><repository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url></repository></repositories> <pluginRepositories><pluginRepository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories></project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
2.编写启动类Applaction
package com.sbmimport org.apache.ibatis.session.SqlSessionFactoryimport org.apache.tomcat.jdbc.pool.DataSourceimport org.mybatis.spring.SqlSessionFactoryBeanimport org.mybatis.spring.annotation.MapperScanimport org.mybatis.spring.mapper.MapperScannerConfigurerimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.EnableAutoConfigurationimport org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.boot.context.properties.ConfigurationPropertiesimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.ComponentScanimport org.springframework.core.io.support.PathMatchingResourcePatternResolverimport org.springframework.jdbc.datasource.DataSourceTransactionManagerimport org.springframework.transaction.PlatformTransactionManagerimport org.springframework.transaction.annotation.EnableTransactionManagementimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.RestController@EnableAutoConfiguration@SpringBootApplication@ComponentScan//@RestControllerpublic class Applaction { @RequestMapping("/") public String index(){ return "Spring Boot Application..." } public static void main(String[] args) { SpringApplication.run(Applaction.class, args) }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
3.编写控制类Controller
package com.sbm.controllerimport com.sbm.service.IMessageServiceimport org.springframework.stereotype.Controllerimport org.springframework.ui.Modelimport org.springframework.web.bind.annotation.PathVariableimport org.springframework.web.bind.annotation.RequestMappingimport org.springframework.web.bind.annotation.ResponseBodyimport javax.annotation.Resourceimport java.util.Dateimport java.util.HashMapimport java.util.Map@Controllerpublic class HelloController { @RequestMapping("/hello/{name}") public String hello(@PathVariable("name") String name, Model model) { model.addAttribute("name", name) model.addAttribute("age","25") model.addAttribute("sex","man") model.addAttribute("birth",new Date()) return "hello" } @RequestMapping("/json") @ResponseBody public Map<String,Object> json(){ Map<String,Object> map = new HashMap<String,Object>() map.put("name","Flyat") map.put("age","25") map.put("sex","man") return map }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
4.测试页面访问看效果
前台页面渲染是使用的Freemarker模板,下节总结