SpringBoot——Mybatis-注解(十五)

Mapper配置类

注解方式是将xml转化为配置类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.qn.mapper;

import com.qn.eneity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapperAnocation {

@Select("SELECT * FROM user")
List<User> getAllUsers();
}

@Mapper注解或是启动类扫描

业务类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.qn.service;

import com.qn.eneity.User;
import com.qn.mapper.UserMapperAnocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceAnocation {
@Autowired
UserMapperAnocation userMapperAnocation;

List<User> getAllUsers() {
return userMapperAnocation.getAllUsers();
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.qn.service;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
class UserServiceAnocationTest {

@Autowired
UserServiceAnocation userServiceAnocation;

@Test
void getAllUsers() {
userServiceAnocation.getAllUsers()
.forEach(user -> System.out.println(user.toString()));
}
}

SpringBoot——Mybatis-注解(十五)_2020-05-21-10-24-32.png

文章目录
  1. 1. Mapper配置类
  2. 2. 业务类
  3. 3. 测试类
|