Spring3MVC+Mybatis快速使用

1.项目结构:


2.代码:


1.User.xml   Article.java    User.javaI    UserOperation.java:参考mybatis快速使用
2.UserController.java:请求处理
package com.spring3.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.mybatis.bean.Article;
import com.mybatis.service.IUserOperation;

@Controller
@RequestMapping("/article")
public class UserController {

	@Autowired
	//类型注入
	IUserOperation userMapper;
	
	@RequestMapping("/list")
	public ModelAndView listall(HttpServletRequest requset,HttpServletResponse response){
		List<Article> articles = userMapper.getUserArticles(1);
		ModelAndView mav = new ModelAndView("list");
		mav.addObject("articles", articles);
		return mav;
	}
}

3.Configuration.xml:可以不再导入mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<typeAlias alias="User" type="com.mybatis.bean.User"/>
		<typeAlias type="com.mybatis.bean.Article"/>
	</typeAliases>
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"></transactionManager>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments>
	
<!-- 	<mappers>
		<mapper resource="com/mybatis/bean/User.xml"/>
	</mappers> -->
	
</configuration>
4.applicationContext.xml:配置数据源、sqlsessionfactory、事务、实例化接口类mapper bean
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
       <!-- 加载数据库配置文件 -->
        <!-- <context:property-placeholder location="classpath:/config/database.properties"/>  -->
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        	destroy-method="close" 
        	p:driverClassName="com.mysql.jdbc.Driver"
        	p:url="jdbc:mysql:///mybatis?characterEncoding=utf8"
        	p:username="root" p:password="root"
        	p:maxActive="20" p:maxIdle="5">
        </bean>
        <!-- 注册事务管理 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!--注册spring管理sessionfactory  -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        	<!-- 配置数据源 -->
        	<property name="dataSource" ref="dataSource"></property>
        	<!-- 指定factory配置文件 -->
        	<property name="configLocation" value="classpath:config/Configuration.xml"></property>
        	<!-- 导入所有mapper配置文件 -->
        	<property name="mapperLocations" value="classpath*:com/mybatis/bean/*.xml"></property>
        </bean>
        
        <!-- 通过扫描包,实例化所有的接口bean,这是一个bean集合  扫描实例化MapperScannerConfigurer
        	实例化调用MapperFactoryBean
        	注入是可使用@Autowired 类型装配-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        	<!-- 扫描包 使用分号或逗号 作为分隔符设置多于一个的包路径-->
        	<property name="basePackage" value="com.mybatis.service"></property>
        </bean>
        
</beans>

5.web.xml:配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Spring3Mybatis</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 加载spring配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:config/applicationContext.xml</param-value>
  </context-param>
  <!-- 启动监听加载spring容器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 注册servlet,加载dispatcher配置文件 -->
  <servlet>
  	<servlet-name>mvc-dispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  	<!-- 默认加载mvc-dispatcher-servlet.xml 的servlet配置 -->
  </servlet>
  <servlet-mapping>
  	<servlet-name>mvc-dispatcher</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

6.mvc-dispatcher-servlet.xml:驱动注解、一些全局配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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-3.0.xsd">
        <!-- 包扫描,spring解析驱动 -->
        <context:component-scan base-package="com.spring3.controller"></context:component-scan>
        <!-- mvc注解驱动 -->
        <mvc:annotation-driven/>
       
        <!-- 静态资源控制 -->
        <mvc:resources location="/WEB-INF/static" mapping="/static/**"/>
        <mvc:default-servlet-handler/>
       
        <!-- 注册请求视图处理器  前后缀配置-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<property name="prefix">
        		<value>/WEB-INF/pages/</value>
        	</property>
        	<property name="suffix">
        		<value>.jsp</value>
        	</property>
        </bean>
</beans>

7.测试:
<body>
	<c:forEach items="${articles }" var="item">
		${itme.id}--${item.title }--${item.content }<br/>
	</c:forEach>
</body>


3.总结:

1.configuration.xml文件中的数据库配置可以不用

2.其他配置和spring+mybatis一样,唯有spring配置文件在 实例化mapper时配置略有不同,不过都是通过MapperFactoryBean去实例化接口mapper。

@Autowired类型注入

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值