Model
model은 Servlet의 request.setAttribute()역할을 함, key와 value로 이루어진 hashmap형태이다 -> 추후 key값으로 value에 접근 가능
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model, HttpServletRequest req)
RequestMapping을 통해 매핑을 한 메서드의 파라미터값으로 model 객체를 전달한다
@RequestMapping
클라이언트에게 요청받은 주소를 클래스와 연결 시켜주는 어노테이션(@붙인 주석같은 역할)
패키지명 잘못쓰면 걍 바꾸지말고 걍 쓰셈
@RequestMapping(value = "/", method = RequestMethod.GET)
--> get방식으로 "/" 라는 요청이 들어오면 아래 메서드를 실행해라
만약 method부분을 지우면 dual로 get과 post를 다 받을 수 있음
자바 버전 바꿀때
pom.xml에서 바꾸기 (03_Calc) 참고
project facet에서 바꾸기
mybatis
데이터베이스를 보다 쉽게 사용할 수 있게 해주는 프레임워크
이걸 사용하려면 3개 라이브러리가 필요함
라이브러리는 https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.2 여기서
필요한 라이브러리 있는지 확인 후
얘는 스프링 버전이랑 맞춰야함
한글깨짐 방지
servlet-context
servlet-context를 통해 db에 연결하기
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- servlet-context는 서버에 첫 요청이 들어오면 읽는 파일 -->
<!-- @Controller 인식하는 설정 -->
<annotation-driven />
<!-- 이미지나 css를 불러올 때 사용할 설정(나중에 자세히...) -->
<resources mapping="/resources/**" location="/resources/" />
<!-- @Controllers 에서 문자열로 반환하면 해당 jsp로 찾아가는 설정-->
<!-- home이라고 치면 -> /WEB-INF/views/home.jsp 라고 함 -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- 우리가 사용하는 어노테이션 인식 범위, 이 범위 밑에 있으면 모두 인식해줌-->
<context:component-scan base-package="kr.co.gudi" />
<!-- data source 작성(접속정보) -->
<beans:bean name = "datasource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- org.apache.commons.dbcp.BasicDataSource를 복사해서 datasource에 넣는다
객체화라고 생각해 -->
<beans:property name="driverClassName" value="org.mariadb.jdbc.Driver"/>
<beans:property name="url" value="jdbc:mariadb://localhost:3306/mydb"/>
<beans:property name="username" value="web_user"/>
<beans:property name="password" value="pass"/>
</beans:bean>
<!-- mybatis 설정 -->
<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 이 클래스엔 속성이 들어갈건데, 1. 연결할 DB정보 -->
<beans:property name="dataSource" ref="datasource"/>
<!-- 2. 쿼리문이 있을 장소 -->
<beans:property name="mapperLocations" value="classpath:kr/co/gudi/dao/*.xml"/>
</beans:bean>
<!-- mybatis를 별다른 코드없이 사용할 수 있는 어노테이션 등록 -->
<mybatis-spring:scan base-package="kr.co.gudi.dao"/>
</beans:beans>
mapper namespace
<mapper namespace="kr.co.gudi.dao.MemberDAO">
<!-- id="연결할 인터페이스 메서드명" -->
<!-- connection을 열고 prepareStatement -> 물음표 대응해주고 -> 쿼리실행하고->자원반납 -->
<insert id="join" parameterType="hashmap">
INSERT INTO member(id,pw,name,age,gender,email)
VALUES(#{id},#{pw},#{name},#{age},#{gender},#{email})
</insert>
여기서 namespace를 이 mapper.xml의 interface 이름과 똑같이 해주면 mapper.xml에 있는 설정과 interface에 있는 설정을 같이 사용할 수 있게 된다
ps
spring 처음 세팅하기
src/test/어쩌고 무시
스프링에서는 복잡한 프로그램이라 내가 작성한 내용을 톰캣에 전송하지 못할 때가 많음 (오류주의)
maven 저장소 다운로드 받아줘 하면
pom.xml로 들어감
java를 위한 실행압축파일 : jar
웹을 실행하기 위한 압출파일 : war
'Spring' 카테고리의 다른 글
[SPRING] controller 부분 조금 설명 (0) | 2023.03.31 |
---|---|
[SPRING] spring 특징, framework란?, 프로젝트 처음 만들었을 때 설정값 (0) | 2023.03.29 |
국비코딩 [Maria DB] DB활용한 회원가입, 로그인 코드리뷰 (0) | 2023.03.27 |
국비코딩 [Maria DB] (0) | 2023.03.24 |
국비코딩 [Maria DB] (0) | 2023.03.23 |