Spring+Sping MVC + Mybatis三大框架整合步骤

1.web.xml

<!-- 配置前端控制器 -->
  <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:config/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
    <!-- 配置spring的监听器 -->
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 加载路径下的其他配置文件 -->
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:config/applicationContext-*.xml</param-value>
    </context-param>

2.springMVC.xml

<?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"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context        
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc.xsd  ">
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="cn.com.mvc" use-default-filters="false">
       <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 处理器解析器和适配器 -->
    <mvc:annotation-driven />
    <!-- 视图解析器 -->
    <!-- 动态返回jsp路径(前缀+名称+后缀): /jsp/index+.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 支持上传文件 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
		<!-- 100M -->
		<property name="maxUploadSize" value="104857600"></property>	
		<property name="defaultEncoding" value="utf-8"></property>   
	</bean>
    
</beans>

3.applicationContext-mybatis.xml(spring和mybatis整合)

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     <!-- 开启注解扫描 -->
     <context:annotation-config />
     <context:component-scan base-package="cn.com.how" >
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>
     <!-- 加载数据库配置文件 -->
     <context:property-placeholder file-encoding="utf-8" location="db.properties"/>
     <!-- 连接数据库的bean -->
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="${db.driver}" />
         <property name="url" value="${db.url}"/>
         <property name="username" value="${db.uname}"/>
         <property name="password" value="${db.pwd}"/>
    </bean>
    <!-- 会话工厂 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--属性1:    添加别名 -->
        <property name="typeAliasesPackage" value="cn.com.how.pojo" />
    <!--属性2:注入数据库连接对象 -->
        <property name="dataSource" ref="dataSource"/>
    <!--属性3:pojo类的xml的配置文件 -->
        <property name="mapperLocations" value="classpath:cn/com/how/mapper/*.xml"/>
    </bean>
 
    <!-- 扫描mapper类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.com.how.mapper"/>
    </bean>
     
</beans>

4.db.properties:

#mysql
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test
db.uname=root
db.pwd=admin

5.项目架构:(有时将mapper.java和mapper.xml分开)

6.controller:

@Controller
@RequestMapping("/ssm")
public class UserController {	
	@Resource
	UserService userService;
	@RequestMapping("/login")
	public String login(User user,HttpServletRequest request) {
		int count = userService.login(user);
		if(count>0) {
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			return "list";
		}else {
			return "redirect:/login.jsp";
		}
	}

}

7.service

@Service
public class UserServiceImpl implements UserService {

	@Resource
	UserMapper userMapper;
	@Override
	public int login(User user) {
		
		return userMapper.login(user);
	}

}

8.mapper.java和mapper.xml一 一对应

mapper.xml中:

<mapper namespace="cn.com.how.mapper.UserMapper" >

总结:三大框架的整合,web.xml还是加载前端控制器,映射springMVC的配置文件,然后加载spring的监听器 匹配加载spring及其他配置文件。spring的配置文件中整合mybatis的sqlsession,扫描相应的mapper类。然后controller注入service对象,动态代理方式注入,可以直接通过接口去创建实现类的对象。service实现类注入mapper.java接口,mybatis会自动创建实现类去执行xml文件中的sql语句。最后返回给controller,controller返回给前台交互。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值