Sep 17, 2014

spring MVC project using mybatis







1. Add dependency in pom.xml file








Searching and add mybatis



Searching and add mybatis-spring



Searching and add mysql-connector-java 


Searching and add spring-jdbc 


2. Add namespace in root-context.xml file.



Click to  src->main->webapp->WEB-INF->spring->root-context.xml  and Namespaces tab


3. Add bean 

Click to Source tab



Add this code

  1.     <bean id="dataSource"
  2.         class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
  3.         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  4.         <property name="url" value="jdbc:mysql://localhost/netplug"></property>
  5.         <property name="username" value="root"></property>
  6.         <property name="password" value="1234"></property>
  7.     </bean>
  8.        
  9.     <bean id="sqlSessionFactory"
  10.         class="org.mybatis.spring.SqlSessionFactoryBean">
  11.         <property name="dataSource" ref="dataSource"></property>
  12.         <property name="configLocation"
  13.             value="classpath:mybatis/mybatis-config.xml">
  14.         </property>
  15.     </bean>
  16.    
  17.     <bean id="transactionManager"
  18.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  19.         <property name="dataSource" ref="dataSource"></property>
  20.     </bean>
  21.    
  22.     <bean id="sqlSession"
  23.         class="org.mybatis.spring.SqlSessionTemplate">
  24.         <constructor-arg  ref="sqlSessionFactory"></constructor-arg>
  25.     </bean>
Line4~6 : your mysql setting.

My case schema is netplug and table is 0000dc180000





4. mapper config

Make a package for mapper to src->main->resources



package name is mybatis 

make xml files 

file name is mapper.xml and mybatis-config.xml




mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD config 3.0//EN"
  3.          
  4. <configuration>
  5.     <mappers>
  6.         <mapper resource="mybatis/mapper.xml" />
  7.     </mappers >
  8. </configuration>

mapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. <mapper namespace"userControlMapper" >
  4.     <select id ="selectSample" parameterType="java.util.HashMap" resultType"java.util.HashMap">
  5.         select *
  6.         from 0000dc180000
  7.        
  8.     </select >
  9. </mapper>

5. Java coding

Click to locate src->main->java->com->answerofgod->spring->HomeController.java

Maybe different folder.

com.answerofgod.spring is my package name.

HomeController.java

  1. @Controller
  2. public class HomeController {
  3.    
  4.     private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  5.    
  6.     @Autowired
  7.       private SqlSession sqlSession;
  8.    
  9.     /**
  10.      * Simply selects the home view to render by returning its name.
  11.      */
  12.     @RequestMapping(value = "/", method = RequestMethod.GET)
  13.     public String home(Locale locale, Model model) {
  14.         logger.info("Welcome home! The client locale is {}.", locale);
  15.        
  16.         Date date = new Date();
  17.         DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONGDateFormat.LONG, locale);
  18.        
  19.         String formattedDate = dateFormat.format(date);
  20.        
  21.         model.addAttribute("serverTime", formattedDate );
  22.        
  23.         List<HashMap<String, String>> outputs = sqlSession.selectList("userControlMapper.selectSample");
  24.         model.addAttribute("showDB", outputs.toString());
  25.        
  26.        
  27.         return "home";
  28.     }
  29.    
  30. }

add Line 6~7 and Line 23~24 

6. JSP coding

Click to src->main->webapp->WEB-INF->views->home.jsp

home.jsp

  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  2. <%@ page session="false" %>
  3. <%@ page contentType="text/html; charset=UTF-8" %>
  4. <html>
  5. <head>
  6.     <title>Home</title>
  7. </head>
  8. <body>
  9. <h1>
  10.     Hello world!  
  11. </h1>
  12. <P>  The time on the server is ${serverTime}. </P>
  13. <p> This is my Database </p>
  14. <p> ${showDB}</p>
  15. </body>
  16. </html>

add Line 14~15

This is my project directory.



run as-> run on server









No comments:

Post a Comment