<!-- 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>
추가
<!-- 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>
src/main/java
에 만든다!!또는
여기에
<?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>
<?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>
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;
}
}
@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"; } }