创建Spring Boot项目
使用材料:IDEA spring initializr,或者 maven项目直接构建
建立好的项目结构如图
所有组件包需要和Application同一级别目录。
新建步骤:
1.使用IDEA Spring initializr
2.配置一些基础信息
3.确定初期导入的包,建议项目推荐两个,一个是web模块,一个是jdbc模块。
4.完成创建
完了之后IDEA会自动生成一个Spring项目,查看pom文件,并添加jackson,作为rest端口的Map to Json 转换
<?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.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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> <!-- jackson包版本 --> <jackson.version>2.5.0</jackson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <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>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build> </project>
最后编写一个最简易的数据接口。
@RestController public class FirstController { @Autowired private TestDao testDao; @RequestMapping(value="/hello",produces="text/html;charset=UTF-8",method = RequestMethod.GET) public String hello(){ return "hello"; } @RequestMapping(value="/address/{id}",method = RequestMethod.GET) public Map<String,Object> address(@PathVariable long id){ Map<String,Object> result = testDao.testSelect(id); if(result == null) { result = new HashMap<>(); System.out.println("为空"); result.put("status","null"); } return result; } }
@Repository public class TestDao { @Autowired private JdbcTemplate jdbcTemplate; public Map<String,Object> testSelect(long id){ String sql = "select * from address where id = ?"; try{ return jdbcTemplate.queryForMap(sql,id); }catch (Exception e){ return null; } } }
注意要配置数据库信息到application.properties文件中
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=密码 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
全部完成之后,点击运行。
运行之后出现Spring的标志
启动成功提示
使用postman mock接口
到此新建Spring Boot项目完成。
如果不想要Spring initializr启动,则新建一个Maven普通项目,并配置pom文件导入Spring Boot的组件,最后手动新建目录(不知道会不会生成,没试过)。