I tried to handle the database using Mapper in Java (Spring).
Download the jar file so that Mapper can be used. If you are using Spring Tool Suite, select MyBatis when creating your project!
The project name is hidden because it is somewhat embarrassing.
@Mapper
public interface UserMapper {
@Insert("INSERT INTO users (userId, username, password) VALUES (#{userId}, #{username}, #{password})")
void insertUser(User user);
@Select("SELECT * FROM user")
List<user> getUserList();
}
@Controller
public class SampleController {
private final UserMapper userMapper;
public UtilityController(UserMapper userMapper) {
this.userMapper = userMapper;
}
@GetMapping("/insertUser")
public String insertUser() {
User user = new User("sampleId", "sampleName", "samplePassword");
utilityMapper.insertUser(user);
return "newUser";
}
@GetMapping("/getUser")
public String getUser() {
List<User> userList = utilityMapper.getUserList();
return "newUser";
}
}
It's really easy to work with databases. It became ridiculous to make Service classes and Dao.
that's all. Thank you for reading to the end.
Recommended Posts