Spring_Boot专栏 | ||
---|---|---|
上一篇 | 主目录 | 下一篇 |
【前言】
使用easycode
插件快速生成代码
使用easycode自动生成代码
有一款插件神奇easycode,可以直接对数据的表生成entity,controller,service,dao,mapper
,无需任何编码,简单而强大。先安装插件:
1 安装easycode
2 在IDEA配置连接数据库
【注意】数据库(表)的创建可以使用1节中的方法,也可以使用自己数据库中的表。有了表进行以下操作即可:
3 引入maven依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
4 自动生成代码
生成的结果为:
5 修改配置文件&加注解
在application.yml
文件中新增:
mybatis:
mapper-locations: classpath:/mapper/*Dao.xml
typeAliasesPackage: com.shane.ems.entity
在dao层加上@mapper
注解
在启动类里面加上@MapperScan("com.shane.ems.dao")
注解。
6 测试
此时创建的数据库还是空的,先插入数据方便测试查询:
insert into department values(1,"D-AA");
insert into employee values(1,"shane","shaneholmes@qq.com",1,1);
将主类启动,输入:http://localhost:8080/department/selectOne?id=1
这个请求其实是在自动生成代码的时候有的:
@RestController
@RequestMapping("department")
public class DepartmentController {
/**
* 服务对象
*/
@Resource
private DepartmentService departmentService;
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("selectOne")
public Department selectOne(Integer id) {
Department department = this.departmentService.queryById(id);
System.out.println(department);
return department;
}
}