springBoot+mybatisPlus小demo
2018-10-24 08:45:55来源:博客园 阅读 ()
项目介绍:采用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#/,显示下图效果即配置成功。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:Shiro框架原理分析
下一篇:Java基本修饰符
- 项目经理说这种代码必须重构,我同意了,这代码是写的是有多 2020-06-11
- eclipse下创建Maven项目(包含webapp目录结构) 2020-06-09
- 阿里巴巴26个屌炸天的开源项目,你知道几个? 2020-06-09
- Idea实现SpringBoot外置Tomcat的Web项目热部署(包含静态文 2020-06-04
- 10 个牛逼的后台开源项目,接私活赚钱必备! 2020-06-03
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash