Skip to content

Commit e638938

Browse files
authored
Merge pull request #17 from kento-kotlin-sandbox/future/add_spring_test
Add mybatis logic and rest routes
2 parents 2b5c2d8 + 9a1c2e9 commit e638938

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@
102102
<artifactId>spring-security-test</artifactId>
103103
<scope>test</scope>
104104
</dependency>
105+
<!-- Spring Boot MyBatis -->
106+
<dependency>
107+
<groupId>org.mybatis.spring.boot</groupId>
108+
<artifactId>mybatis-spring-boot-starter</artifactId>
109+
<version>1.3.2</version>
110+
</dependency>
105111
</dependencies>
106112

107113
<build>

src/main/java/com/example/demo/login/controller/UserRestController.java

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Qualifier;
67
import org.springframework.web.bind.annotation.DeleteMapping;
78
import org.springframework.web.bind.annotation.GetMapping;
89
import org.springframework.web.bind.annotation.PathVariable;
@@ -18,6 +19,7 @@
1819
@RestController
1920
public class UserRestController {
2021
@Autowired
22+
@Qualifier("RestServiceMybatisImpl")
2123
RestService service;
2224

2325
// ユーザー全件取得
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.example.demo.login.domain.repository.mybatis;
2+
3+
import java.util.List;
4+
5+
import org.apache.ibatis.annotations.Delete;
6+
import org.apache.ibatis.annotations.Insert;
7+
import org.apache.ibatis.annotations.Mapper;
8+
import org.apache.ibatis.annotations.Select;
9+
import org.apache.ibatis.annotations.Update;
10+
11+
import com.example.demo.domain.model.User;
12+
13+
@Mapper
14+
public interface UserMapper {
15+
16+
// 登録用メソッド
17+
@Insert("INSERT INTO m_user ("
18+
+ "user_id,"
19+
+ "password,"
20+
+ "user_name,"
21+
+ "birthday,"
22+
+ "age,"
23+
+ "marriage,"
24+
+ "role)"
25+
+ " VALUES ("
26+
+ "#{userId},"
27+
+ "#{password},"
28+
+ "#{userName},"
29+
+ "#{birthday},"
30+
+ "#{age},"
31+
+ "#{marriage},"
32+
+ "#{role})")
33+
public boolean insert(User user);
34+
35+
// 1件検索用メソッド
36+
@Select("SELECT user_id AS userId,"
37+
+ "password,"
38+
+ "user_name AS userName,"
39+
+ "birthday,"
40+
+ "age,"
41+
+ "marriage,"
42+
+ "role"
43+
+ " FROM m_user"
44+
+ " WHERE user_id = #{userId}")
45+
public User selectOne(String userId);
46+
47+
// 全件検索用メソッド
48+
@Select("SELECT user_id AS userId,"
49+
+ "password,"
50+
+ "user_name AS userName,"
51+
+ "birthday,"
52+
+ "age,"
53+
+ "marriage,"
54+
+ "role"
55+
+ " FROM m_user")
56+
public List<User> selectMany();
57+
58+
// 1件更新メソッド
59+
@Update("UPDATE m_user SET"
60+
+ "password = #{password},"
61+
+ "user_name = #{userName},"
62+
+ "birthday = #{birthday},"
63+
+ "age = #{age},"
64+
+ "marriage = #{marriage},"
65+
+ "WHERE user_id = #{userId}")
66+
public boolean updateOne(User user);
67+
68+
// 1件削除用メソッド
69+
@Delete("DELETE FROM m_user WHERE user_id = #{userId}")
70+
public boolean deleteOne(String userId);
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.example.demo.login.domain.service.mybatis;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.annotation.Transactional;
8+
9+
import com.example.demo.domain.model.User;
10+
import com.example.demo.domain.service.RestService;
11+
import com.example.demo.login.domain.repository.mybatis.UserMapper;
12+
13+
14+
@Transactional
15+
@Service("RestServiceMybatisImpl")
16+
public class RestServiceMybatisImpl implements RestService {
17+
18+
@Autowired
19+
UserMapper userMapper;
20+
21+
@Override
22+
public boolean insert(User user) {
23+
// insert実行
24+
return userMapper.insert(user);
25+
}
26+
27+
@Override
28+
public User selectOne(String userId) {
29+
// select実行
30+
return userMapper.selectOne(userId);
31+
}
32+
33+
@Override
34+
public List<User> selectMany() {
35+
// select実行
36+
return userMapper.selectMany();
37+
}
38+
39+
@Override
40+
public boolean update(User user) {
41+
// update実行
42+
return userMapper.updateOne(user);
43+
}
44+
45+
@Override
46+
public boolean delete(String userId) {
47+
// delete実行
48+
return userMapper.deleteOne(userId);
49+
}
50+
}

0 commit comments

Comments
 (0)