Spring+springmvc+mybatis整合案例

今天在整合过程中发生了很多问题,根本原因在于整合jar不完整,导致的NotFoundClass报错,这个错误很经典,一旦jar有误就会发生,我在这里粘贴上我的项目源码,以便于提供大家参考。
源码下载地址:http://download.csdn.net/detail/u013821825/9546000
项目中我用的是spring3.1.2+mybatis3.0.5整合的

项目结构:
Spring+springmvc+mybatis整合案例_第1张图片

1、数据准备

create table
CREATE TABLE `emp` (
   `id` int(10) NOT NULL,
   `name` varchar(20) DEFAULT NULL,
   `age` int(3) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1

INSERT INTO `test`.`emp`(`id`,`name`,`age`) VALUES ( '1','xsx','24');
INSERT INTO `test`.`emp`(`id`,`name`,`age`) VALUES ( '2','mpt','23');

2、实体类

package org.xsx.entity;

public class Emp {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }


}

3、映射Mapper配置文件

  

<mapper namespace="org.xsx.dao.EmpDao">

<select id="findAll" resultType="org.xsx.entity.Emp">
    select id,name,age from emp
select>

mapper>

4、映射接口

package org.xsx.dao;

import java.util.List;

import org.xsx.annotation.MyBatisRepository;
import org.xsx.entity.Emp;

/**
 * DAO组件
 * @author xusha
 *
 */
@MyBatisRepository
public interface EmpDao {
    public List findAll();
}

5、业务控制类

package org.xsx.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xsx.dao.EmpDao;
import org.xsx.entity.Emp;

@Controller
@RequestMapping("emp")
public class EmpController {
    @Resource
    private EmpDao empDao;

    @RequestMapping("/findEmp.do")
    public String find(Model model){
        List list = empDao.findAll();
        model.addAttribute("emps", list);
        return "emp/emp_list";
    }
}

6、web.xml


<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">



    <servlet>
        <servlet-name>SpringMvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext.xmlparam-value>
        init-param>
    servlet>

    <servlet-mapping>
        <servlet-name>SpringMvcservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <async-supported>trueasync-supported> 
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>

    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>*.dourl-pattern>
    filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>
web-app>

7、spring位置文件


<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:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        
        <property name="password" value="root" />
        
        <property name="initialSize" value="1" />
        
        <property name="maxActive" value="500" />
        
        <property name="maxIdle" value="2" />
        
        <property name="minIdle" value="1" />
    bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:org/xsx/entity/*.xml" />
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.xsx.dao" />
        <property name="annotationClass" value="org.xsx.annotation.MyBatisRepository" />
    bean>

    
    <context:component-scan base-package="org.xsx" />
    
    <mvc:annotation-driven />
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    bean>


beans>

8、jsp文件

<%@page pageEncoding="utf-8" 
contentType="text/html;charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 
    prefix="c" %>
<html>
    <head>head>
    <body>
        <table width="60%" cellpadding="2" cellspacing="0">
            <tr>
                <td>编号td>
                <td>姓名td>
                <td>年龄td>
            tr>
            <c:forEach items="${emps}" var="e">
            <tr>
                <td>${e.id }td>
                <td>${e.name }td>
                <td>${e.age }td>
            tr>
            c:forEach>
        table>
    body>
html>


9、结果
Spring+springmvc+mybatis整合案例_第2张图片

你可能感兴趣的:(java)