项目介绍:采用restful api进行接口规范 / 项目框架SpringBoot+mybatis Plus / 采用mysql进行数据存储 / 采用swaggerUI进行前后端业务分离式开发。
开发环境:JDK1.8+Mysql8.0.12+IDEAL
实现功能:springboot搭建整体框架,MybatisPlus动态生成Dao+Services+Entity+Controller结构
项目介绍:无实际的业务操作,都是测试功能。其中为了区别mybastis和mybatisPlus,特意写了两个接口分别采用mapper.xml进行dao层操作和采用Iservice提供的CRUD操作进行数据查询。
ps:如果对springboot和mybayisPlus已了解,可直接访问码云进行demo下载 https://gitee.com/xc199534/SpringBootMybatisPlus/
----------------------------------------------------------分割线---------------------------------------------
1.项目整体结构
其中Config中包括MybatisPlusConfig和SwaggerConfig两个类,分别用于初始化MybatisPlus和Swagger基础设置。
数据库结构,采用navicat进行可视化管理。
2.配置文件application.properties
也可采用.yml格式进行配置文件设置,本项目是使用.properties。
spring.application.name=spring-boot-config
server.port=8080
server.context-path=/ #mybatis mapper文件的位置
mybatis.mapper-locations=classpath*:mapper/*.xml
#扫描pojo类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写pojo类的全路径名了
mybatis.type-aliases-package=com.example.demo
#可以通过mybatis.config-location属性来指定mybatis的配置文件的位置,
#mybatis.config-location=classpath:mybatis-config.xml
#数据库参数设置
jdbc.type=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.pom.xml内容(所有不上项目完整pom.xml的demo都是耍流氓)
<dependencies>
<!-- Springboot-->
<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>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<!-- <version>2.1.3</version> -->
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<!-- <version>3.0.9</version> -->
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<scope>provided</scope>
</dependency> <!-- 阿里巴巴druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency> <!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- mybatisplus与springboot整合 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3</version>
</dependency> <!-- 模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency> <!--swagger-->
<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>
3.自动生成代码(Dao/service/Entity)
调用MpGenerator.java的主函数生成定义好MVC层结构(MpGenerator定义了文件位置,文件名称,依赖关系等属性)。
Dao层:生成*Dao.java文件和*Dao.xml,后者可理解为Dao层的数据库映射操作,mybatis支持在.xml文件中写数据库CURD语句,但myatisPlus支持使用内置的CRUD操作,避免了复杂的数据库语句。
Service层 :生成一个接口文件,一个接口实现类。
Entity层:生成实体类,对应到数据库的表结构。
SchoolDao.java
package com.example.demo.dao; import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.example.demo.entity.School;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface SchoolDao extends BaseMapper<School> {
School getSchoolById(@Param("id") int id);
}
SchoolService.java
import com.baomidou.mybatisplus.service.IService;
import com.example.demo.entity.School;
import org.elasticsearch.index.query.QueryStringQueryBuilder; public interface SchoolService extends IService<School> {
public School getSchoolById(int id);
// public Iterable<School> searchByES(QueryStringQueryBuilder builder); }
SchoolServiceImpl.java
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.demo.dao.SchoolDao;
import com.example.demo.entity.School;
import com.example.demo.services.SchoolService;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class SchoolServiceImpl extends ServiceImpl<SchoolDao, School> implements SchoolService {
@Override
public School getSchoolById(int id) {
return baseMapper.selectById(id);
}
}
School.java
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import org.springframework.data.elasticsearch.annotations.Document; import java.io.Serializable; @TableName("school")
@Document(indexName = "education", type = "school")
public class School implements Serializable {
@TableId("ID")
private int id; @TableField("NAME")
private String name; @TableField("RANGE")
private int range; @TableField("AGE")
private int age; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRange() { return range; }
public void setRange(int range) { this.range = range; }
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "School{" +
"id=" + id +
", name='" + name +
", range=" + range +
", age=" + age +
'}';
}
}
4、Controller层
@RestController
@RequestMapping("/")
public class SampleController {
@Autowired
private UserService userService; @Autowired
private SchoolService schoolService; @Autowired
private testDao testdao; @Autowired
private ElasticsearchTemplate elasticsearchTemplate; @ApiOperation(value = "测试")
@GetMapping("/home")
String home() {
return "Hello World!";
} @RequestMapping(value = "/getUser", method = RequestMethod.GET)
public User helloUser() {
User user = userService.getUserById(1);
System.out.println(user);
return user;
} @ApiOperation(value = "采取mybatis方式查询")
@ApiImplicitParam(name = "id", value = "用户id", paramType = "path", required = true)
@GetMapping("/getSchool/maybatis/{id}")
public School helloSchool1(@PathVariable("id") int id) {
School school = schoolService.getSchoolById(id);
System.out.println(school);
return school;
} @ApiOperation(value = "采取mybatis-plus方式查询")
@ApiImplicitParam(name = "id", value = "用户id", paramType = "path", required = true)
@GetMapping("/getSchool/maybatis-plus/{id}")
public School helloSchool2(@PathVariable("id") int id) {
School school = schoolService.selectById(id);
System.out.println(school);
return school;
} @ApiOperation(value = "插入数据")
@ApiImplicitParam(name = "school", value = "学校信息", paramType = "body", required = true)
@PostMapping("/insert/school")
public Boolean insertSchool(@RequestBody School school) {
Boolean tag = schoolService.insert(school);
System.out.println(tag);
return tag;
}
}
5、调用主程序启动
@SpringBootApplication
@MapperScan("com.example.demo.dao")
@ComponentScan(basePackages = {
"com.example.demo.config",
"com.example.demo.controller",
"com.example.demo.services"})
public class DemoApplication { public static void main(String[] args) { //System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(DemoApplication.class, args);
}
}
启动成功后访问:http://localhost:8080/swagger-ui.html#/,显示下图效果即配置成功。