spring boot项目搭建

一、开发环境的安装

JDK maven IDEA

idea 安装 lombok、free mybatis plugin插件

二、IDEA创建spring boot项目

File->new->project

选择spring initializr ->next

spring initializr project settings ->修改你要修改的->next

选择依赖:

spring boot devtools

lombok

spring configuration processor

spring web

mysql driver

mybatis framework

选择完依赖后next

选择项目存储目录,然后点击完成

三、添加druid连接池

pom.xml添加至dependencies

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>版本号</version>
</dependency>

application.properties 添加数据库配置

1
2
3
4
5
6
7
8
9
10
11
12
spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/crm?useSSL=true\
&useUnicode=true\
&characterEncoding=UTF-8\
&useServerPrepStmts=true\
&serverTimezone=Asia/Shanghai\
&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.druid.username=root
spring.datasource.druid.password=你的数据库密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.type-aliases-package=com.example.demo.dao
mybatis.mapper-locations=classpath:mapper/*.xml

在启动类添加

1
2
3
4
5
6
7
8
9
@MapperScan("com.example.demo.dao")
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

四、编写model、dao、mapper、service、controller

创建database,连接并生成代码 model、dao、mapper

model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* crm_student
* @author
*/
@Data
public class CrmStudent implements Serializable {
private Integer id;

/**
* 姓名
*/
private String name;

/**
* 性别
*/
private String gender;

private static final long serialVersionUID = 1L;
}

mapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.CrmStudentDao">
<resultMap id="BaseResultMap" type="com.example.demo.model.CrmStudent">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="gender" jdbcType="VARCHAR" property="gender" />
</resultMap>

<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.example.demo.model.CrmStudent" useGeneratedKeys="true">
insert into crm_student (`name`, gender)
values (#{name,jdbcType=VARCHAR}, #{gender,jdbcType=VARCHAR})
</insert>

</mapper>

dao

1
2
3
4
5
public interface CrmStudentDao {

int insert(CrmStudent record);

}

service

1
2
3
4
5
6
7
8
9
10
11
@Service
public class CrmStudentService {

@Autowired
private CrmStudentDao crmStudentDao;

public void save(CrmStudent crmStudent) {
crmStudentDao.insert(crmStudent);
}

}

controller

1
2
3
4
5
6
7
8
9
10
11
12
13
@RequestMapping("/crmstudent")
@RestController
public class CrmStudentController {

@Autowired
private CrmStudentService crmStudentService;

@PostMapping("/save")
public void save(@RequestBody CrmStudent crmStudent) {
crmStudentService.save(crmStudent);
}

}

五、启动并测试

1
2
3
4
5
6
7
http://localhost:8080/crmstudent/save
POST
Content-Type : application/json
{
"name" : "abc",
"gender" : "男"
}

spring boot项目搭建
http://hanqichuan.com/2019/09/03/spring/spring_boot项目搭建/
作者
韩启川
发布于
2019年9月3日
许可协议