Using Mapper with Java (Spring)

What i did

I tried to handle the database using Mapper in Java (Spring).

Preparation

Download the jar file so that Mapper can be used. If you are using Spring Tool Suite, select MyBatis when creating your project!

Folder hierarchy

スクリーンショット 2020-06-25 18.59.00.png

The project name is hidden because it is somewhat embarrassing.

code

Write in the mapper class like this.


@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();
}

Write in the Controller class like this.

@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";
	}
}

At the end

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

Using Mapper with Java (Spring)
Java Config with Spring MVC
Spring Java
Using Java with AWS Lambda-Eclipse Preparation
Html5 development with Java using TeaVM
[Java] LINE integration with Spring Boot
Using proxy service with Java crawling
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
About Spring Dependency Injection using Java, Kotlin
Using JupyterLab + Java with WSL on Windows 10
Try using Spring Boot with VS Code
Game development with two people using java 2
I tried using OpenCV with Java + Tomcat
Game development with two people using java 1
[Spring] Obfuscate specific fields using annotations [Java]
Asynchronous processing with Spring Boot using @Async
Game development with two people using java 3
Try using the Wii remote with Java
[Java] Spring DI ③
Interact with LINE Message API using Lambda (Java)
Specify ClassPath when using jupyter + Java with WSL
Using Gradle with VS Code, build Java → run
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
[Java] Article to add validation with Spring Boot 2.3.1.
Install java with Homebrew
Sorting using java comparator
Change seats with java
Install Java with Ansible
Using Pair with OpenJDK
Spring with Kotorin --- 5. Actuator
Comfortable download with JAVA
Scraping practice using Java ②
Switch java with direnv
Scraping practice using Java ①
Download Java with Ansible
Self-made Validation with Spring
Let's scrape with Java! !!
Build Java with Wercker
Spring with Kotorin ―― 1. SPRING INITIALIZR
Download with Spring Boot
Try using Spring JDBC
Endian conversion with JAVA
Create a portfolio app using Java and Spring Boot
Getting started with Java programs using Visual Studio Code
Try using DI container with Laravel and Spring Boot
[Note] Configuration file when using Logback with Spring Boot
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
[Java] How to omit spring constructor injection with Lombok
Try using OpenID Connect with Keycloak (Spring Boot application)
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
Try to work with Keycloak using Spring Security SAML (Spring 5)
[Java] Development with multiple files using package and import
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
[Java / Kotlin] Escape (sanitize) HTML5 support with unbescape [Spring Boot]
Try using RocksDB in Java