千峰商城-springboot项目搭建-12-tkMapper整合
1.创建新工程tkmapper-demo。
勾选依赖
编写application.yml:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/fmmall2?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
mybatis.type-aliases-package=com.qfedu.tkmapperdemo.beans
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
在com/qfedu/tkmapperdemo下创建包bean、dao,resource下创建包mapper。
添加tkMapper依赖
<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency>
2.在启动类中使用tkMapper的@MapperScan注释,引用包名。
package com.qfedu.tkMapperdemo; import tk.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.qfedu.tkmapperdemo.dao") public class TkmapperDemoApplication { public static void main(String[] args) { SpringApplication.run(TkmapperDemoApplication.class, args); } }
3.创建数据表
4.创建实体类
beans-User.java:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "User对象",description = "买家信息")
public class User {
private int userId;
private String username;
private String password;
private String nickname;
private String realname;
private String userImg;
private String userMobile;
private String userEmail;
private String userSex;
private Date userBirth;
private Date userRegtime;
private Date userModtime;
}
5.创建DAO接口
tkMapper已经完成了对单表的通用操作的封装。封装在Mapper接口和MySqlMapper接口中。因此如果我们想要完成对单表的操作,只需自定义dao接口继承这两个接口。
dao-UserDAO.java:
public interface UserDAO extends Mapper<User>,MySqlMapper<User>{ }
6.测试
@RunWith(SpringRunner.class) @SpringBootTest(classes = TkmapperDemoApplication.class) public class UserDAOTest{ @Autowired private UserDAO userDAO; @Test public void test(){ User user = new User(); user.setUsername("aaaa"); user.setPassword("1111"); user.setUserImg("img/default.png"); user.setUserRegtime(new Date()); user.setUserModtime(new Date()); int i = userDAO.insert(user); System.out.println(i) } }