一. 简单项目搭建
1. 右键目录空白new project
选择Maven Project
![](https://img.haomeiwen.com/i2818652/3cc4a3cd15055ef4.png)
2. 创建一个简单的项目
![](https://img.haomeiwen.com/i2818652/e262698a237c347a.png)
3. Group Id
,Artifact Id
,Compiler Level
填写
Group Id
和Artifact Id
是为了保证项目唯一性而提出的,如果你要把你项目弄到maven
本地仓库去,你想要找到你的项目就必须根据这两个id
去查找。一般分为多个段,常用的为两段,第一段为域,第二段为公司名称。域又分为org、com、cn
等等许多,其中org
为非营利组织,com
为商业组织。列如tomcat
:这个项目的groupId
是org.apache
,它的域是org
(因为tomcat是非营利项目),公司名称是apache
,artigactId
是tomcat
。
Artifact Id
: 项目的名称
Compiler Level
: 选择jdk版本
![](https://img.haomeiwen.com/i2818652/127033d7545f72c9.png)
4. 点击完成
![](https://img.haomeiwen.com/i2818652/9f23a7661ea29bf4.png)
5. pom文件
<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.tkb.settle</groupId>
<artifactId>NCI_TKB_SETTLE_SSMK</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 添加parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
- 添加web支持
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
- 添加插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
添加之后:
<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.tkb.settle</groupId>
<artifactId>NCI_TKB_SETTLE_SSMK</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.创建Application.java类
![](https://img.haomeiwen.com/i2818652/df7f2e585680b9a0.png)
package controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//Spring Boot项目的核心注解,主要目的是开启自动配置
@SpringBootApplication
@Controller
public class Application {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello world";
}
//应用入口
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
7. 右键Run as
![](https://img.haomeiwen.com/i2818652/7c377e8477f433c4.png)
8. 浏览器输入localhost:8080/hello
回车测试效果
![](https://img.haomeiwen.com/i2818652/ec0b0fc74ecfe76e.png)
二. 添加MySQL支持
1. pom文件导入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2. 创建测试表,此处省略过程
![](https://img.haomeiwen.com/i2818652/66f0c9858097d67b.png)
3. 添加资源配置文件application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
4. 建立MySQL业务流程
![](https://img.haomeiwen.com/i2818652/bacc5f4b5d20de59.png)
Application.java
//Spring Boot项目的核心注解,主要目的是开启自动配置
@MapperScan("com.tkb.demo.dao")
@SpringBootApplication
@Controller
public class Application {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello world";
}
//应用入口
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
TestController.java
@RestController
@EnableAutoConfiguration
public class TestController {
@Resource
private UserTestService service;
@RequestMapping("/getUser/{id}")
@ResponseBody
String getUser(@PathVariable Long id) {
System.out.println("接收到浏览器的id为:" + id);
return service.getById(id).toString();
}
}
UserMapper.java
@Mapper
public interface UserMapper {
@Select("select id,name,age from usertest where id=#{id}")
User getById(@Param("id") Long id);
}
UserTestService.java
@Service
public class UserTestService {
@Autowired
private UserMapper userMapper;
public User getById(long id){
return userMapper.getById(id);
}
}
启动之后浏览器输入http://localhost:8080/getUser/1
![](https://img.haomeiwen.com/i2818652/014f660ab88196a4.png)
参考
SpringBoot学习-(一)如何在MyEclipse中创建SpringBoot项目
SpringBoot整合Mybatis完整详细版
网友评论