1. STS로 Spring MVC Project 생성 후 접속 확인
- 한글이 깨져서 나오니 home.jsp 파일에2. pom.xml에 dependency 추가
3. root-context.xml에 bean 추가
4. mybatis-config.xml 생성
(src/main/resources/mybatis/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>
<mappers >
<mapper resource ="/mybatis/mapper-sample.xml"/>
</mappers >
</configuration>
5. mapper-sample.xml 생성
(src/main/resources/mybatis/mapper-sample.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= "userControlMapper" >
<select id ="selectSample" parameterType="java.util.HashMap" resultType= "java.util.HashMap">
SELECT *
FROM TEST_USERS
WHERE NAME = #{name}
</select>
<insert id ="insertTable01" parameterType="java.util.HashMap" >
INSERT INTO TEST_USERS
(NAME, EMAIL)
VALUES
( #{name}, #{email} )
</insert>
</mapper>
6. MySQL에 테스트 table과 data 준비
CREATE TABLE `스키마이름`.`TEST_USERS` (`NO` INT NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(100) NULL,
`EMAIL` VARCHAR(100) NULL,
PRIMARY KEY (`NO`),
UNIQUE INDEX `NO_UNIQUE` (`NO` ASC));
INSERT INTO TEST_USERS (NAME, EMAIL) VALUES ("han", "han@test.com");
7. HomeController.java 수정
@Controllerpublic class HomeController {
@Controller
public class HomeController {
// <--- 추가
@Autowired
private SqlSession sqlSession;
// 추가 --->
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
// <--- 추가
HashMap<String, String> input = new HashMap<String, String>();
input.put("name", "han");
List<HashMap<String, String>> outputs = sqlSession.selectList("userControlMapper.selectSample", input);
System.out.println(outputs.toString());
// 추가 --->
return "home";
}
}
8. 브라우저로 server 접속 후 Console 출력 확인
- [{NO=1, EMAIL=han@test.com, NAME=han}]참고 :
http://blog.naver.com/refreshin/150170189512
http://hangaebal.blogspot.kr/2014/08/spring-spring-tool-suitests-mysql.html
'Blogger 이사' 카테고리의 다른 글
[Toad] Table Space 및 사용자 계정 만들기 (0) | 2016.04.04 |
---|---|
[css] favicon, apple-touch-icon 설정 (0) | 2016.04.04 |
[JavaScript] KakaoTalk App share url (카톡 url 공유) (0) | 2016.04.04 |
[JavaScript] Facebook, Twitter share url (0) | 2016.04.04 |
[JavaScript] LINE App share url (LINE url 공유) (0) | 2016.04.04 |