tk-mybatis :单表操作是无敌的
一)引入依赖
tk.mybatis
mapper-spring-boot-starter
2.1.5
二)配置appliction.yml
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///testdb?useSSL=true
username: root
password:123456
mapper:
identity: MYSQL
not-empty: true
mappers:
- tk.mybatis.mapper.common.Mapper
- tk.mybatis.mapper.common.MySqlMapper
#启用日志打印SQL语句 level是mapper的包路径
logging:
level:
com.example.bootssm2.mapper: debug
三)
/**
- 项目中所有mapper类的父类
- @param
*/
public interface BaseMapper extends Mapper , MySqlMapper {
}
四)
由于项目mapper文件很都,可以在启动类中用
@MapperScan(“可以多个包路径,用逗号隔开”)
注意用tk-mybatis中的@MapperScan
public interface BookInfoMapper extends BaseMapper {
}
五)
@SpringBootApplication
@MapperScan(“com.example.bootssm2.mapper”)
public class BootSsm2Application {
public static void main(String[] args) {
ConfigurableApplicationContext
context = SpringApplication.run(BootSsm2Application.class, args);
BookInfoMapper mapper = context.getBean(BookInfoMapper.class);
BookInfo book1 = mapper.selectByPrimaryKey("5");
mapper.insertList(Arrays.asList(book1));
}
}
六)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = “t_book”) //映射表
public class BookInfo {
@Id //定义主键
private Integer bookId;
private String bookName;
private String bookAuthor;
private BigDecimal bookPrice;
}
//坑:注意要定义主键