第三步:创建相应的包(dao,entity,mapper等): 第四步:配置文件application.ymlorg.springframework.boot spring-boot-starter-webtk.mybatis mapper-spring-boot-starter2.1.5 org.mybatis.spring.boot mybatis-spring-boot-starter2.2.0 com.alibaba druid-spring-boot-starter1.1.22 mysql mysql-connector-java8.0.17 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testorg.projectlombok lombok1.18.22 junit junit
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/shopmall?serverTimezone=GMT%2B8&useUnicode=true&&characterEncoding=UTF-8
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
mybatis:
mapper-locations: classpath:mappers/*Mapper.xml
type-aliases-package: com.abtu.entity
server:
port: 8083
Dao层代码:需要继承(Mapper<>,MySqlMapper<>)
@Repository public interface UserDao extends Mapper启动类需要添加MapperScan注解, MySqlMapper { }
注意MapeperScande注解一定要映入tkMapper的:tk.mybatis.spring.annotation.MapperScan;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.abtu.dao")
public class TkmapperDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TkmapperDemoApplication.class, args);
}
}
编写测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
class TkmapperDemoApplicationTests {
@Autowired
UserDao userDao;
@Test
public void contextLoads() {
Users user = new Users();
user.setUsername("5654654654");
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);
}
}
完成!!!!



