pom.xml文件:存放maven相关的依赖配置
application.yml文件:整个工程的配置文件,这里用了.yml文件,比.properties文件的语言更简洁,配置代码结构更有层次感。
MingxinApplication.java文件:springboot的工程启动入口。
1、新建一个springboot的maven工程
File --> new --> project–>next进入如下界面:
配置好包名和项目名next
接着添加相关的依赖
搭建完成初始项目
1、配置pom.xml文件
添加swagger、连接池druid的依赖
在properties中配置编码集UTF-8
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mingxin</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<!-- swagger ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
2、新建application.yml
#开发配置
spring:
#数据源配置
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#公共配置与profiles选择无关
mybatis:
typeAliasesPackage: com.example.second.demosecond.entity
mapperLocations: classpath:mapper/*.xml
3、在resources目录下创建mapper目录下得UserMapper.xml
<?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.UserMapper" >
<sql id="Base_Column_List" >
user_name,user_id,password
</sql>
<select id="selectUser" parameterType="java.util.HashMap" resultType="java.util.HashMap">
select
<include refid="Base_Column_List" />
from sys_user
where user_name = #{user_name}
</select>
</mapper>
4、新建dao目录和UserMapper.java
package com.example.demo.dao;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* @author LIUSHUJIE
* @date 2019/10/8 14:10
*/
@Repository
public interface UserMapper {
List<Map<String,Object>> selectUser(Map<String,Object> map);
}
5、新建service目录和下面的 UserService
package com.example.demo.service;
import java.util.List;
import java.util.Map;
/**
* @author LIUSHUJIE
* @date 2019/10/8 14:14
*/
public interface UserService {
public List<Map<String,Object>> selectUser(Map<String,Object> map);
}
6、在service目录下新建serviceImpl目录的UserServiceImpl.java文件
package com.example.demo.service.impl;
import com.example.demo.dao.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @author LIUSHUJIE
* @date 2019/10/8 14:14
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public List<Map<String, Object>> selectUser(Map<String, Object> map) {
List<Map<String, Object>> mapList = userMapper.selectUser(map);
return mapList;
}
}
7、新建controller目录和下面的UserController.java文件
package com.example.demo.controller;
import com.example.demo.controller.base.BaseController;
import com.example.demo.service.UserService;
import com.example.demo.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
/**
* @author LIUSHUJIE
* @date 2019/10/8 14:25
*/
@RestController
@EnableAutoConfiguration
@Api(description = "用户接口")
@RequestMapping(value = "/user")
public class UserController extends BaseController {
@Autowired
UserService userService;
@ApiOperation(value = "查询用户", notes = "查询用户用户")
@PostMapping(value = "getUser")
public List<Map<String, Object>> getUser(@RequestBody Map<String, Object> map) {
String user_name = map.get("user_name").toString();
System.out.println("user_name: "+user_name);
List<Map<String, Object>> mapList = userService.selectUser(map);
return mapList;
}
}
8、在controller目录下新建base目录和BaseContrller.java
package com.example.demo.controller.base;
/**
* @author LIUSHUJIE
* @date 2019/10/8 14:26
*/
public class BaseController {
}
9、新建config目录和下面的Swagger2Config
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author LIUSHUJIE
* @date 2019/10/8 14:34
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboordemo")
.description("Springboot整合Demo")
.version("0.0.1")
.build(); // 这部分信息其实可以自定义到配置文件中读取
}
}
10、修改MingzinApplication.java文件
添加mapper的映射路径
@MapperScan(“com.example.demo.dao”)
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class MingxinApplication {
public static void main(String[] args) {
SpringApplication.run(MingxinApplication.class, args);
}
}
11、在mysql数据库新建表
drop table if exists sys_user;
create table sys_user (
user_id bigint(20) not null auto_increment comment '用户ID',
dept_id bigint(20) default null comment '部门ID',
login_name varchar(30) not null comment '登录账号',
user_name varchar(30) not null comment '用户昵称',
user_type varchar(2) default '00' comment '用户类型(00系统用户)',
email varchar(50) default '' comment '用户邮箱',
phonenumber varchar(11) default '' comment '手机号码',
sex char(1) default '0' comment '用户性别(0男 1女 2未知)',
avatar varchar(100) default '' comment '头像路径',
password varchar(50) default '' comment '密码',
salt varchar(20) default '' comment '盐加密',
status char(1) default '0' comment '帐号状态(0正常 1停用)',
del_flag char(1) default '0' comment '删除标志(0代表存在 2代表删除)',
login_ip varchar(50) default '' comment '最后登陆IP',
login_date datetime comment '最后登陆时间',
create_by varchar(64) default '' comment '创建者',
create_time datetime comment '创建时间',
update_by varchar(64) default '' comment '更新者',
update_time datetime comment '更新时间',
remark varchar(500) default null comment '备注',
primary key (user_id)
) engine=innodb auto_increment=100 comment = '用户信息表' DEFAULT CHARSET=utf8;
insert into sys_user values(1, 103, 'admin', 'admin', '00', '[email protected]', '15888888888', '1', '', '123456', '111111', '0', '0', '127.0.0.1', '2018-03-16 11-33-00', 'admin', '2018-03-16 11-33-00', 'ry', '2018-03-16 11-33-00', '管理员');
insert into sys_user values(2, 105, 'ry', 'admin', '00', '[email protected]', '15666666666', '1', '', '123456', '222222', '0', '0', '127.0.0.1', '2018-03-16 11-33-00', 'admin', '2018-03-16 11-33-00', 'ry', '2018-03-16 11-33-00', '测试员');
1、访问接口页面http://localhost:8080/swagger-ui.html
2、传参数
3、返回结果,测试成功