[JAVA] Until data acquisition with Spring Boot + MyBatis + PostgreSQL

I didn't bother so much a while ago, I had a hard time doing it recently, so I used it as a memo for myself.

Preparation

  1. Java 8 installation
  2. eclipse installation
  3. Install PostgreSQL 11

Install Spring Tool 3 Add-On (STS) in eclipse

Search for sts in the eclipse marketplace and press the install button. スクリーンショット 2019-04-07 18.50.53.png

You can use a dedicated development tool (STS), This time we will use a plugin.

Spring Boot project creation

Select New → Project → Spring Starter Project and

スクリーンショット 2019-04-07 18.55.47.png

The dependency is like this.

スクリーンショット 2019-04-07 18.56.28.png

Project creation is OK by pressing Done.

Folder structure

スクリーンショット 2019-04-07 19.43.43.png I will go like this.

Creating a class

1-1 Controller creation

HelloController.java


@Controller
public class HelloController {

	@Autowired
	private HelloService helloService;

	@RequestMapping(value="hello")
	public String init(Model model) {

		List<HelloBean> list = helloService.selectName();
		model.addAttribute("list",list);

		return "hello";
	}
}

Service creation

HelloSerivce.java


@Service
public class HelloService {

	@Autowired
	private HelloMapper helloMapper;

	public List<HelloBean> selectName(){
		return helloMapper.selectEmpAll();
	}
}

Created by Dao.

python


CREATE TABLE emp_name(
id int,
name varchar(20)
)

Create a table with the above contents. Since it is for testing, neither PRIMARY nor UNIQUE is set for the time being.

HelloMapper.java


@Mapper
public interface HelloMapper {

	List<HelloBean> selectEmpAll();
}

HelloMapper.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.mapper.HelloMapper">
	<select id="selectEmpAll" resultType="com.demo.bean.HelloBean">
		select * from
		emp_name
	</select>
</mapper>

html creation

hello.html


<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>employee number</th>
			<th>Employee name</th>
		</tr>
		<tr th:each="emp : ${list}">
			<td th:text="${emp.id}"></td>
			<td th:text="${emp.name}"></td>
		</tr>
	</table>
</body>
</html>

Environmental setting

Create mybatis config. If the column name of the table is snake case, convert it to camel case and Added a setting that associates with the bean variable name. I won't use it this time, but I want to remember it.

mybatis-config.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
</configuration>

Load the created mybatis-config

SampleApplication.java


package com.demo;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

@SpringBootApplication
public class SampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(SampleApplication.class, args);
	}

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        //Read config file
        sessionFactory.setConfigLocation(new ClassPathResource("/mybatis-config.xml"));

        return sessionFactory.getObject();
    }
}

Added connection settings with DB.

application.properties


spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/sample
spring.datasource.username=postgres    #According to your own environment
spring.datasource.password=postgres    #According to your own environment

That's it.

Run

Right click on the project and run → maven install When BUILD SUCCESS appears, right-click again and execute → Spring boot application selection Open http: // localhost: 8080 / hello from your browser.

スクリーンショット 2019-04-07 20.07.13.png

If it looks like this, it's OK.

Impressions

Is the setting reduced compared to Spring MVC? I think it will be easier to understand once you get used to it.

Recommended Posts

Until data acquisition with Spring Boot + MyBatis + PostgreSQL
Until "Hello World" with Spring Boot
Flow until output table data to view with Spring Boot
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
Implementation method for multi-data source with Spring boot (Mybatis and Spring Data JPA)
Download with Spring Boot
Compatibility of Spring JDBC and MyBatis with Spring Data JDBC (provisional)
Create Restapi with Spring Boot ((1) Until Run of App)
Until you start development with Spring Boot in eclipse 1
Until you start development with Spring Boot in eclipse 2
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Spring Boot Introductory Guide I tried [Accessing Data with JPA]
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
Use Basic Authentication with Spring Boot
Spring Boot + PostgreSQL error resolution method
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
How to set Spring Boot + PostgreSQL
Spring Boot programming with VS Code
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
See the behavior of entity update with Spring Boot + Spring Data JPA
Processing at application startup with Spring Boot
OR search with Spring Data Jpa Specification
Spring with Kotorin --2 RestController and Data Class
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose