您的位置:首页 > 其它

Mybatis + String mvc 整合

2015-08-31 20:40 429 查看
一、整理所需要的架包

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

  


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

编写service 的实现类

三、编写 Spring 配置文件

  Spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 引入属性文件
<context:property-placeholder location="DataSourceConfig.properties"/>
-->
<!-- 自动扫描Service(自动注入) -->
<context:component-scan base-package="sy.service*"/>

</beans>


  Spring --Mvc 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 自动扫描Controller包下的所有的类,使其认为是Spring mvc 的控制器 -->
<context:component-scan base-package="sy.controller" />

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<!-- 启动Spring mvc的注解功能,完成请求和注解POJO的映射
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingjacksonHttpMessageConverter" />
</list>
</property>
</bean>
-->
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp">
</bean>

<!-- 文件上传相关 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value>        <!-- 上传文件大小限制为31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>

</beans>


  Spring -Mybaits 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byType">

<!-- 引入资源文件
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location">
<value>
classpath:DataSourceConfig.properties
</value>
</property>
</bean>
-->
<!-- 配置数据源 -->
<bean id="dateSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/paynow"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<!-- my batis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dateSource"/>
<property name="mapperLocations" value="classpath:sy/mapper/*.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="sy.dao"/>
<property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
</bean>

<!-- 事务管管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource" />
</bean>

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* sy.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>

</beans>


  WEB.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>在线订购</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 加载Spring上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- UTF-8 过滤器 -->
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- spring mvc servlet -->
<servlet>
<description>Spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>Spring mvc配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置Session超时时间,单位分钟 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>

</web-app>


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

  

package sy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import sy.model.Goods;
import sy.service.GoodsService;
import sy.service.OrdersService;
import sy.service.UserService;

@Controller
@RequestMapping("/index")
public class Index {

private UserService userService;
private OrdersService ordersService; // 订单
private GoodsService goodsService; // 商品
private List<Goods> list; // 商品集合

@RequestMapping("/show")
public String show(HttpServletRequest request) {
list = goodsService.selectAll();
Goods goods = goodsService.selectByPrimaryKey(1);
request.setAttribute("list", list);
request.setAttribute("goods", goods);
System.out.println(goods.getGname());
return "WEB-INF/user/show";
}
@RequestMapping(method = RequestMethod.GET ,value = "/show")

public UserService getUserService() {
return userService;
}

@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}

public OrdersService getOrdersService() {
return ordersService;
}

@Autowired
public void setOrdersService(OrdersService ordersService) {
this.ordersService = ordersService;
}

public GoodsService getGoodsService() {
return goodsService;
}

@Autowired
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}

}


  编写 测试类

package sy.test;

import java.util.List;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import sy.model.Goods;
import sy.service.GoodsService;
import sy.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"})//加载配置文件
public class Test {

private UserService userService;
private GoodsService goodsService;

@org.junit.Test
public void select(){
//    Users users=userService.selectUserByid(1);
//    System.out.println(users.getUname());
//    Goods goods =goodsService.selectByPrimaryKey(1);
//    System.out.println(goods.getGname());
List list =goodsService.selectAll();
Goods goods=    (Goods) list.get(0);
System.out.println(goods.getGname());
}

public GoodsService getGoodsService() {
return goodsService;
}
@Autowired
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
public UserService getUserService() {

return userService;
}
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: