码迷,mamicode.com
首页 > Web开发 > 详细

Mybatis + String mvc 整合

时间:2015-08-31 21:42:25      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

 一、整理所需要的架包

  此次整合 只有Mybatis 和Spring mvc的架包  其他内容暂不讨论

  技术分享

 二、使用 mybatis 自动生成工具  生成  interface(dao层文件) mapper(SQL文件)  model(实体类) service(逻辑) 文件

      编写service 的实现类

 三、编写 Spring 配置文件

  Spring配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 4     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd 
 8         http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
10         http://www.springframework.org/schema/tx 
11         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
14         http://www.springframework.org/schema/context
15         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16         
17         <!-- 引入属性文件
18         <context:property-placeholder location="DataSourceConfig.properties"/>
19          -->
20         <!-- 自动扫描Service(自动注入) -->
21         <context:component-scan base-package="sy.service*"/>
22         
23         
24         
25 </beans>
26     

  Spring --Mvc 配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 4     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd 
 8         http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
10         http://www.springframework.org/schema/tx 
11         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
12         http://www.springframework.org/schema/aop 
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
14         http://www.springframework.org/schema/context
15         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
16 
17     <!-- 自动扫描Controller包下的所有的类,使其认为是Spring mvc 的控制器 -->
18     <context:component-scan base-package="sy.controller" />
19 
20     <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
21     <bean id="mappingJacksonHttpMessageConverter"
22         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
23         <property name="supportedMediaTypes">
24             <list>
25                 <value>text/html;charset=UTF-8</value>
26             </list>
27         </property>
28     </bean>
29 
30     <!-- 启动Spring mvc的注解功能,完成请求和注解POJO的映射
31     <bean
32         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
33         <property name="messageConverters">
34             <list>
35                 <ref bean="mappingjacksonHttpMessageConverter" />
36             </list>
37         </property>
38     </bean>
39  -->
40     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
41     <bean
42         class="org.springframework.web.servlet.view.InternalResourceViewResolver"
43         p:prefix="/" p:suffix=".jsp">
44     </bean>
45     
46     <!-- 文件上传相关 -->
47     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
48         <property name="defaultEncoding">
49             <value>UTF-8</value>
50         </property>
51         <property name="maxUploadSize">
52                 <value>32505856</value>        <!-- 上传文件大小限制为31M,31*1024*1024 -->
53         </property>
54         <property name="maxInMemorySize">
55             <value>4096</value>
56         </property>
57     </bean>
58  
59 
60 </beans>

  Spring -Mybaits 配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc
 7     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 8     http://www.springframework.org/schema/beans 
 9     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10     http://www.springframework.org/schema/tx 
11     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12     http://www.springframework.org/schema/aop 
13     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14     http://www.springframework.org/schema/context 
15     http://www.springframework.org/schema/context/spring-context-3.0.xsd"
16     default-autowire="byType">
17 
18     <!-- 引入资源文件 
19     <bean id="propertyConfig"
20         class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
21         <property name="location">
22             <value>
23                 classpath:DataSourceConfig.properties
24             </value>
25         </property>
26     </bean>
27     -->
28     <!-- 配置数据源 -->
29      <bean id="dateSource"
30         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
31         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
32         <property name="url" value="jdbc:mysql://127.0.0.1:3306/paynow"/>
33         <property name="username" value="root"/>
34         <property name="password" value="root"/>
35     </bean>
36     
37     <!-- my batis配置 -->
38     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
39         <property name="dataSource" ref="dateSource"/>
40         <property name="mapperLocations" value="classpath:sy/mapper/*.xml"/>
41     </bean>
42     
43     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
44         <property name="basePackage" value="sy.dao"/>
45         <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
46     </bean>
47 
48     <!-- 事务管管理器 -->
49     <bean id="transactionManager"
50         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
51         <property name="dataSource" ref="dateSource" />
52     </bean>
53 
54     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
55         <tx:attributes>
56             <tx:method name="get*" read-only="true" />
57             <tx:method name="query*" read-only="true" />
58             <tx:method name="find*" read-only="true" />
59             <tx:method name="load*" read-only="true" />
60             <tx:method name="select*" read-only="true" />
61             <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
62         </tx:attributes>
63     </tx:advice>
64     <aop:config>
65         <aop:pointcut id="transactionPointcut" expression="execution(* sy.service..*Impl.*(..))" />
66         <aop:advisor pointcut-ref="transactionPointcut"
67             advice-ref="transactionAdvice" />
68     </aop:config>
69 
70 
71 
72 
73 
74 </beans>

  WEB.xml 配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6     <display-name>在线订购</display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.jsp</welcome-file>
 9     </welcome-file-list>
10 
11     <!-- 加载Spring上下文 -->
12     <context-param>
13         <param-name>contextConfigLocation</param-name>
14         <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
15     </context-param>
16     <!-- UTF-8 过滤器 -->
17     <filter>
18         <description>字符集过滤器</description>
19         <filter-name>encodingFilter</filter-name>
20         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
21         <init-param>
22             <description>字符集编码</description>
23             <param-name>encoding</param-name>
24             <param-value>UTF-8</param-value>
25         </init-param>
26     </filter>
27     <filter-mapping>
28         <filter-name>encodingFilter</filter-name>
29         <url-pattern>/*</url-pattern>
30     </filter-mapping>
31     <listener>
32         <description>spring监听器</description>
33         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
34     </listener>
35     <!-- 防止spring内存溢出监听器 -->
36     <listener>
37         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
38     </listener>
39 
40     <!-- spring mvc servlet -->
41     <servlet>
42         <description>Spring mvc servlet</description>
43         <servlet-name>springMvc</servlet-name>
44         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
45         <init-param>
46             <description>Spring mvc配置文件</description>
47             <param-name>contextConfigLocation</param-name>
48             <param-value>classpath:spring-mvc.xml</param-value>
49         </init-param>
50         <load-on-startup>1</load-on-startup>
51     </servlet>
52     <servlet-mapping>
53         <servlet-name>springMvc</servlet-name>
54         <url-pattern>*.do</url-pattern>
55     </servlet-mapping>
56     <!-- 配置Session超时时间,单位分钟 -->
57     <session-config>
58         <session-timeout>15</session-timeout>
59     </session-config>
60 
61 
62 </web-app>

四、 编写 控制器(controller) 与测试类

  

 1 package sy.controller;
 2 
 3 import java.util.List;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 
12 import sy.model.Goods;
13 import sy.service.GoodsService;
14 import sy.service.OrdersService;
15 import sy.service.UserService;
16 
17 @Controller
18 @RequestMapping("/index")
19 public class Index {
20 
21     private UserService userService;
22     private OrdersService ordersService; // 订单
23     private GoodsService goodsService; // 商品
24     private List<Goods> list; // 商品集合
25 
26     @RequestMapping("/show")
27     public String show(HttpServletRequest request) {
28         list = goodsService.selectAll();
29         Goods goods = goodsService.selectByPrimaryKey(1);
30         request.setAttribute("list", list);
31         request.setAttribute("goods", goods);
32         System.out.println(goods.getGname());
33         return "WEB-INF/user/show";
34     }
35     @RequestMapping(method = RequestMethod.GET ,value = "/show")
36     
37     
38     
39     
40     
41     
42     public UserService getUserService() {
43         return userService;
44     }
45 
46     @Autowired
47     public void setUserService(UserService userService) {
48         this.userService = userService;
49     }
50 
51     public OrdersService getOrdersService() {
52         return ordersService;
53     }
54 
55     @Autowired
56     public void setOrdersService(OrdersService ordersService) {
57         this.ordersService = ordersService;
58     }
59 
60     public GoodsService getGoodsService() {
61         return goodsService;
62     }
63 
64     @Autowired
65     public void setGoodsService(GoodsService goodsService) {
66         this.goodsService = goodsService;
67     }
68 
69 }

  编写 测试类 

 1 package sy.test;
 2 
 3 import java.util.List;
 4 
 5 import org.junit.runner.RunWith;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 import sy.model.Goods;
11 import sy.service.GoodsService;
12 import sy.service.UserService;
13 
14 
15 @RunWith(SpringJUnit4ClassRunner.class)
16 @ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})//加载配置文件
17 public class Test {
18 
19     private UserService userService;
20     private GoodsService goodsService;
21     
22     @org.junit.Test
23     public void select(){
24     //    Users users=userService.selectUserByid(1);
25     //    System.out.println(users.getUname());
26     //    Goods goods =goodsService.selectByPrimaryKey(1);
27     //    System.out.println(goods.getGname());
28         List list =goodsService.selectAll();
29         Goods goods=    (Goods) list.get(0);
30         System.out.println(goods.getGname());
31     }
32     
33     
34     
35     
36     
37     
38     
39     
40     
41     
42     
43     public GoodsService getGoodsService() {
44         return goodsService;
45     }
46     @Autowired
47     public void setGoodsService(GoodsService goodsService) {
48         this.goodsService = goodsService;
49     }
50     public UserService getUserService() {
51         
52         return userService;
53     }
54     @Autowired
55     public void setUserService(UserService userService) {
56         this.userService = userService;
57     }
58     
59 
60     
61     
62     
63 }

 

Mybatis + String mvc 整合

标签:

原文地址:http://www.cnblogs.com/shiyalong/p/4773935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!