以写一个controller文件为例子,代码如下
@RestController @RequestMapping("/cont/helloworld") public class HelloWorldController { @GetMapping("/greet") public String greet(){ return "hello"; } }
1 application启动类要加入一个扫描的注解
@ComponentScan("com.example")
com.example是因为pom.xml配置
<groupId>com.example</groupId>
2 在不同的包中要导入
<dependency> <groupId>com.example</groupId> <artifactId>content</artifactId> <!-- 这个版本号在主程序下的pom.xml中修改的,所以之后使用的时候用这个写死的形式--> <version>${project.version}</version> </dependency>
3 感觉代码没有问题,把项目重构一下,build,还有地址要记得改
使用两个插件lombok和free mybatis plugin
lombok的作用是可以打印日志@Slf4j,@Data可以自动获得属性的get, set方法就不用人工写了,@AllArgsConstructor 可以生成一个具有该所有属性的构造方法。
学习序列化(要做的事情)
数据库用的是Postgresql, 连接用的是alibaba的数据库连接池,mybatis用的是com.baomidou/mybatis-plus-boot-starter
主要是配置信息
url: jdbc:postgresql://localhost:5432/postgres username: postgres password: 150920
1 写一些返回前台固定的信息,需要有一个枚举类,在response文件夹下
2 MybatisPlusConfiguration类,配置文件
@Configuration @MapperScan(basePackages = "com.example.*.mapper") public class MybatisPlusConfiguration { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL)); return interceptor; } }
3 代码生成器使用的是mybatisplus,可以自动生成controller, service, entity, mapper的代码。CodeGenerator
有一个生成模型的父类BaseModel里面是createBy,...updateTime.
运行CodeGenerator,在控制台输入表名,就可以了
现在我们就可以开始写业务逻辑了