配置数据库连接信息
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&serverTimezone=GMT%2B8
username: xxxx # 你的用户名
password: xxxxxx # 你的密码
新建pojo实体类
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("m_blog")
public class Blog implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private Long userId;
@NotBlank(message = "标题不能为空")
private String title;
@NotBlank(message = "摘要不能为空")
private String description;
@NotBlank(message = "内容不能为空")
private String content;
@JsonFormat(pattern="yyyy-MM-dd")
private LocalDateTime created;
private Integer status;
}
新建mapper,里面得类要加上@mapper
和@repository
注解
在resources下建立mapper文件夹
- 注意: namespace 对应得mapper 是全限定类名
在配置文件中加上
mybatis.type-aliases-package=com.xx.xx
mybatis.mapper-locations=mybatis/mapper/*.xml