本项目使用的环境:
开发工具:Intellij IDEA 2017.1.3
jdk:1.8.0_161
maven:3.3.9
额外功能
PageHelper 分页插件
mybatis generator 自动生成代码插件
步骤:
1.创建一个springboot项目:
这里写图片描述
2.创建项目的文件结构以及jdk的版本
这里写图片描述
3.选择项目所需要的依赖
这里写图片描述
这里写图片描述
然后点击finish
5.看一下文件的结构:
这里写图片描述
6.查看一下pom.xml:
4.0.0 com.winter springboot-mybatis-demo0.0.1-SNAPSHOT jar springboot-mybatis-demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent1.5.6.RELEASE UTF-8 UTF-8 1.7 org.mybatis.spring.boot mybatis-spring-boot-starter1.3.0 org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest mysql mysql-connector-java5.1.35 com.fasterxml.jackson.core jackson-corecom.fasterxml.jackson.core jackson-databindcom.fasterxml.jackson.datatype jackson-datatype-jodacom.fasterxml.jackson.module jackson-module-parameter-namescom.github.pagehelper pagehelper-spring-boot-starter1.1.2 com.alibaba druid-spring-boot-starter1.1.0 org.springframework.boot spring-boot-maven-pluginorg.mybatis.generator mybatis-generator-maven-plugin1.3.2 ${basedir}/src/main/resources/generator/generatorConfig.xml true true
7.项目不使用application.properties文件 而使用更加简洁的application.yml文件:
将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件,
文件的内容如下:
server: port: 8080spring: datasource: name: test url: jdbc:mysql://127.0.0.1:3306/depot username: root password: root
# 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20mybatis: mapper-locations: classpath:mapping@Controller@RequestMapping(value = "/user")public class UserController { @Autowired
private UserService userService; @ResponseBody
@RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"}) public int addUser(User user){ return userService.addUser(user);
} @ResponseBody
@RequestMapping(value = "/all/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"}) public Object findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){ return userService.findAllUser(pageNum,pageSize);
}
}UserService.java
package com.winter.service;import com.winter.model.User;import java.util.List;public interface UserService { int addUser(User user); List findAllUser(int pageNum, int pageSize);
} UserServiceImpl.java
package com.winter.service.impl;import com.github.pagehelper.PageHelper;import com.winter.mapper.UserMapper;import com.winter.model.User;import com.winter.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Service(value = "userService")public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper;//这里会报错,但是并不会影响
@Override
public int addUser(User user) { return userMapper.insertSelective(user);
}
@Override
public List findAllUser(int pageNum, int pageSize) { //将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize); return userMapper.selectAllUser();
}
} 如果强迫症看不下去那个报错:(解决方法)
这里写图片描述
测试我使用了idea一个很用心的功能。
可以发http请求的插件:
这里写图片描述
这里写图片描述
点击左侧的运行按钮就可以发送请求了;
如果返回值正确 说明你已经搭建成功了!!
作者:Winter_Chen
链接:https://www.jianshu.com/p/1601f466fa90



