Spring+SpringMvc+Mybatis 框架的搭建(二)

4.4 mybatis-config.xml

  这部分可以配置也可以不配置。

 1 <?xml version="1.0" encoding="UTF-8" ?> 
 2 <!DOCTYPE configuration 
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 
 6 <configuration>
 7 
 8     <settings>
 9         <setting name="cacheEnabled" value="true" />
10         <setting name="lazyLoadingEnabled" value="true" />
11         <setting name="aggressiveLazyLoading" value="true" />
12         <setting name="multipleResultSetsEnabled" value="true" />
13         <setting name="useColumnLabel" value="true" />
14         <setting name="useGeneratedKeys" value="false" />
15         <setting name="autoMappingBehavior" value="PARTIAL" />
16         <setting name="defaultExecutorType" value="SIMPLE" />
17         <setting name="defaultStatementTimeout" value="25000" />
18     </settings>
19 </configuration>    

4.5 spring-mvc.xml 配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
11     
12     <!-- 扫描控制器包 -->
13     <!-- @Service用于标注业务层组件、 @Controller用于标注控制层组件、@Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。-->
14     <context:component-scan base-package="com.vivebest.controller" />
15     
18     <!-- Enables the Spring MVC @Controller programming model -->
19     <!-- 启动springmvc注解 -->
20     <mvc:annotation-driven />
21         
22     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射,解决@ResponseBody乱码问题  -->
23     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
24         <property name="messageConverters">
25             <list>
26                 <bean class="org.springframework.http.converter.StringHttpMessageConverter">
27                     <property name="supportedMediaTypes">
28                         <list>
29                             <value>text/html;charset=UTF-8</value>
30                             <value>text/plain;charset=UTF-8</value>
31                             <value>application/json;charset=UTF-8</value>
32                         </list>
33                     </property>
34                 </bean>
35             </list>
36         </property>
37     </bean>    
38     <!--视图解析的配置-->
39     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
40     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
41         <property name="prefix" value="/jsp/" />
42         <property name="suffix" value=".jsp" /><!--controller层中返回页面是可以不加.jsp后缀-->
43     </bean>    
46 </beans>

5.Spring和Mybatis测试

  5.1 测试分为两种(1.使用junit测试   2.编写测试类)

    我用的是编写测试类测试    

 1 package com.vivebest.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 import com.vivebest.service.CustomerService;
 6 
 7 public class Test {
 8     private ApplicationContext ac = null; 
 9     public static void main(String[] args) {
10         Test test=new Test();
11         test.before();
12     }
13     public void before() {  
14       ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //引入配置文件 
15       CustomerService customerService= (CustomerService) ac.getBean("customerService");//需要注意这里的customerService 是在service中使用@Service(“customerService”)一致
16       System.out.println(customerService.getAllCustomer());
17     /*  CustomerService TellerService= (CustomerService) ac.getBean("TellerService");
18       System.out.println(TellerService);*/
19   } 
20 }

6.SSM整合测试

  编写controller层

 1 package com.vivebest.controller;
 2 
 3 import java.util.List;
 4 import javax.servlet.http.HttpServletRequest;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.bind.annotation.ResponseBody;
11 import com.vivebest.entity.Customer;
12 import com.vivebest.service.CustomerService;
13 
14 
15 @Controller//标明为控制层
16 @RequestMapping("/customer")//相当于路径
17 public class CustomerController{
18 
19     @Autowired
20     private CustomerService customerService;
21     
22     /**
23      * 客户登陆
24      */
25     @RequestMapping(value ="/customerLogin", method = RequestMethod.POST)
26     public String customerLogin(Model model,HttpServletRequest req){
27         List<Customer> cusList=customerService.getAllCustomer();
28         Customer custom=cusList.get(0);
29         req.setAttribute("custome", custom);
30         System.out.println(custom);
31         return "finally";
32     }
33 }

编写jsp页面,在jsp页面中我遇到了一个小问题,即 

 在使用jstl.jar中 因为

"org.apache.jasper.JasperException: This absolute uri (http://java.sun.com/jsp/jstl/core ) cannot be resolved in either web.xml or the jar files deployed with this application "

    由于JSTL1.0和JSTL1.1的声明语句不一样。

这个问题在另一边文章中我做了总结,大家可以去游览。

启动工程,测试成功!

原文地址:https://www.cnblogs.com/warnerY/p/6069744.html