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類型注入

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章