文章目录
1pom依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf-8&useSSL=false
3对象
public class Category {
private Integer id;
private Integer parentId;
private String name;
private Boolean status;
private Integer sortOrder;
private Date createTime;
private Date updateTime;
}
4dao
5测试
注意:
5.1 虽然能查到数据,但是有的属性是null.因为对象是驼峰形式,数据库是下划线parent_id
在yml文件中添加配置:
mybatis:
configuration:
map-underscore-to-camel-case: true
即:
mybatis默认是属性名和数据库字段名一一对应的,即
数据库表列:user_name
实体类属性:user_name
但是java中一般使用驼峰命名
数据库表列:user_name
实体类属性:userName
在Springboot中,可以通过设置map-underscore-to-camel-case属性为true来开启驼峰功能。
5.2若有很多的mapper,都需要家@mapper。不用每个都加,在启动类加一个即可。@mapper可以不加了
5xml
5.1dao层
5.2xml
注意:
1名称为全限定类名,否则会报错
2 当mapper和mapper.XML不在同一目录下,需要在yml文件中指定
mybatis:
configuration:
map-underscore-to-camel-case: true # 对象和数据库字段对应
# 控制台日志配置
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mappers/*.xml
3另一种打印日志
4将共有部分提取出来