Spring

Sprint Boot, Sprint Boot + MyBatis

greenyellow-s 2024. 11. 14. 06:35
728x90
반응형
Sprint Boot + MyBatis

 

 

Sprint Boot + MySQL + MyBatis + Web

 

application.properties 또는 application.yml 사용

 

Spring Framework -> servlet-context.xml(웹) / root-context.xml(웹X)

Spring Boot -> application.properties 또는 application.yml

 

*,yml : 단계를 잘 맞추어 줘야한다. (줄 간격 .. )

 

Spring Boot에서는 JSP를 권장하지 않는다.

JSP를 사용하려면 의존성을 따로 추가해야한다.

 

 

 

Spring Boot css, js, img 경로 추가하는 방법

 

src/main/resources/static 안에 넣으면 된다.

 

[ 이전 ]

<resources location=“/WEB-INF/css” mapping=“css/**”></resources>

 

이 작업을 의존관계에 의해서 src/main/resources/static 안에 넣으면 알아서 해주기 때문에 따로 선언할 필요가 없다.

 

 

 

 

 


Project : Chapter02MySQL

 

Type : Gradle – Grovy

Available : Lombok, Spring Boot DevTools, Spring Web, Thymeleaf, MySQL Driver, MyBatis Framework

 

 

 

static : 정적인 파일 -> 고정된 파일 / 데이터를 가져와서 변경되는 것이 아니다. 즉, 요청만 하겠다.

templates : 동적인 파일 / 데이터를 요청받아서 응답하겠다.

 

src/main/rescoures 안에 static 안에 html을 넣으면 controller에 선언하지 않아도 http://localhost:[포트번호]/ 로 간다?

src/main/rescoures

      static

          index.html

 

 

 

 

Server – Spring Boot App 시작

 

[에러문구]

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:

If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

 

[이유]

MySQL이랑 MyBatis를 의존성을 추가했는데 아무런 설정을 안해놨기 때문에 뜨는 오류이다.

 

[해결방법]

1. DB 설정을 해준다

 

2. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

(데이터 소스를 읽지 않겠다는 의미)

 

** DB연동할 때는 빼야된다.

 

 

 


DB 연동하기
파일 세팅

 

src/main/java

            com.example.demo

                        Chapter02MySqlApplication.java(메인 메소드)

            user.controller

                        UserController.java

            user.service

                        UserService.interface

            user.service.impl

                        UserServiceImpl.java

            user.dao

                        UserDAO.interface

 

src/main/resources

            mapper

            templates

                        user.folder

                                    writeForm.html

            static

                        css

                        js

                        index.html

 

            application.properties / application.yml

 

 

 

( * JSP를 걷어내고 Thymeleaf로 넘어왔다. )

 

UserDAO

 

@Mapper

 

main메소드

 

@MapperScan("user.dao")

@EnableTransactionManagement

추가

 

UserServiceImpl.java

 

@Transactional

추가

 

Mapper

 

src/main/resources

            mapper

                    ** 이 위치에 Mapper 파일 추가

<?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="[@Mapper 파일 위치]">
</mapper>

 

application.properties - sqlSessionFactory, sqlSession 필요

 

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Seoul
spring.datasource.username=root
spring.datasource.password=1234

# MyBatis - configration 안만들겠다 –> 스프링이 알아서 만듬
# mybatis.config-location=classpath:spring/mybatis-config.xml
# mybatis.mapper-locations=classpath:mapper/userMapper.xml

# -> mapper 파일 안에 어떤 폴더가 있던 그 안에 있는 모든 Mapper.xml 로 끝나는 파일
mybatis.mapper-locations=classpath:mapper/**/*Mapper.xml
mybatis.type-aliases-package=user.bean

 

 

* application.properties / application.yml

application.yml은 탭과 스페이스바를 다르게 본다.

 

 

728x90
반응형