글목록

2014. 11. 19.

Spring + Mybatis + Mysql Setting




1. MySql DB Setting and Table Setting




2. Make Spring Project




3. Select "Spring MVC Project"



4. Add PackageName



5. Add Server



6. Select Apache -> Tomacat Server X.0 Server


7. Tomact Path Setting


8. Project Add



9. pom.xml

  

  
  
   org.mybatis
   mybatis
   3.2.2
  

  
  
   org.mybatis
   mybatis-spring
   1.2.0
  

  
  
   mysql
   mysql-connector-java
   5.1.25
  

  
  
   org.springframework
   spring-jdbc
   3.2.3.RELEASE
  



10. root-context.xml

  



 
 
 
        
        
        
        
        
    
    
        
        
        
    
    
        
    
    
        
    
 

 



11.  Server Setting


12.  Make Package



13.  Add Package Name



14. New -> Other



15. XML -> XML FiLE



16. Make mybatis-config.xml



17.  Setting mybatis-config.xml 
 
  





    
        
    
 



18. New -> Other



19. XML -> XML FiLE



20. Make Mapper.xml



21.  Setting Mapper.xml

  





    
 




22.  Setting HomeController.java

  

package com.blogspot.toplogical;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@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 ModelAndView home(Locale locale, Model model) {

                logger.info("Welcome home! The client locale is {}.", locale);
                // 메시지 모델에 추가.
                String sMessage = "http://toplogical.blogspot.kr/";
                model.addAttribute("sMessage", sMessage);

                // 날짜 부분 출력 및 모델에 추가
                Date date = new Date();
                DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
                String formattedDate = dateFormat.format(date);
                model.addAttribute("serverTime", formattedDate);

              
                // mybatis를 활용 db조회 모델에 추가
                HashMap< String,  String > executeQuery = new HashMap< String, String >();
                executeQuery.put("name", "shin");
                List< HashMap < String, String >> resultSet = sqlSession.selectList("userControlMapper.userlist", executeQuery);
                model.addAttribute("rs", resultSet);


               System.out.print(model.toString());
                

                // view에 모델 넘겨 넘겨
                ModelAndView mav = new ModelAndView("home", "model", model);
                return mav;

        }



} 


23.  Setting home.jsp

  

<%@ page language= "java" contentType ="text/html; charset=UTF-8" pageEncoding ="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>


    Home


    

Hello world!

${model.sMessage}

The time on the server is ${model.serverTime}.

uid name age gender


24.  Run as -> Run on Sever



25.  Select Tomcat Server



26.  Select Project



27.  Success

Forder Tree


댓글 없음:

댓글 쓰기