스프링04_mybatis update()

charl hi·2022년 1월 15일
0

Spring

목록 보기
5/25

mybatis

다운 : pom.xml

		<!-- mybatis -->
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis</artifactId>
		    <version>3.5.6</version>
		</dependency>
		
		<!-- mybatis-spring -->
		<dependency>
		    <groupId>org.mybatis</groupId>
		    <artifactId>mybatis-spring</artifactId>
		    <version>2.0.6</version>
		</dependency>

추가



등록 : servlet-context.xml

	<!-- SqlSessionFactory -->
	<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<beans:property name="dataSource" ref="dbcpSource"></beans:property>
		<beans:property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></beans:property>
		<beans:property name="mapperLocations" value="classpath:/mybatis/**/*-mapper.xml"></beans:property>
	</beans:bean>
	
	<!-- sqlSessionTemplate -->
	<beans:bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<beans:constructor-arg index="0" ref="sqlSessionFactory"/>
	</beans:bean>



mybatis 설정파일

  • ✨📢 src/main/java 에 만든다!!

또는

여기에

✨✨ 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="cacheEnabled" value="true"/>
		<setting name="autoMappingBehavior" value="FULL"/>
	</settings>
	<typeAliases>
		<!-- 경로의 별칭 지정 -->
		<typeAlias type="com.kh.app10.menu.model.vo.MenuVo" alias="menuVo"/>
	</typeAliases>
</configuration>


✨✨ member-mapper.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="menu">
	<!-- parameterType="별칭" -->
	<insert id="insertMenu" parameterType="menuVo">
		INSERT INTO MENU
		(
			MENU
		, 	PRICE)		
		VALUES(
			#{menu}
		,	#{price}
		)
	</insert>

</mapper>

classpath



MemberVo.java

package com.kh.app09.member.model.vo;

public class MemberVo {
	private String id;
	private String pwd;
	private String nick;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
}


package com.kh.app10.menu.model.vo;

public class MenuVo {
	
	private String menu;
	private int price;
	
	public String getMenu() {
		return menu;
	}
	public void setMenu(String menu) {
		this.menu = menu;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}

}


✨✨ MenuController

@Controller
@RequestMapping("/menu")
public class MenuController {
//	1.
//	@Autowired
//	private JdbcTemplate jt;
	
//	2.
	@Autowired
	private SqlSession sqlSession;

	@GetMapping("/insert")
	public String insert() {
		return "menu/insert";
	}
	
	@PostMapping("/insert")
	public String insert(MenuVo m) {
//		1. JdbcTemplate
//		String sql = "INSERT INTO MENU(MENU, PRICE) VALUES(?,?)";
//		Object[] params = /* new Object[] */{menu, price};
//		jt.update(sql, params);
		
//		2. SqlSession
//		menu-mapper 에서 "namespace.insert id"
		sqlSession.insert("menu.insertMenu", m);
		return "redirect:/menu/insert";
	}
}

0개의 댓글