MyBatis学习笔记——SSM整合(Spring4,SpringMVC4,MyBatis3)

构建环境

导入Jar包

pom.xml

<dependencies>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>4.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>4.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>4.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.0version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>4.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.0.0.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.apache.taglibsgroupId>
            <artifactId>taglibs-standard-implartifactId>
            <version>1.2.1version>
        dependency>
        <dependency>
            <groupId>org.apache.taglibsgroupId>
            <artifactId>taglibs-standard-specartifactId>
            <version>1.2.1version>
        dependency>
        <dependency>
            <groupId>net.sourceforge.cglibgroupId>
            <artifactId>com.springsource.net.sf.cglibartifactId>
            <version>2.2.0version>
        dependency>


        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.38version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.2version>
        dependency>

        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.6version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-apiartifactId>
                    <groupId>org.slf4jgroupId>
                exclusion>
            exclusions>
        dependency>

        
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.5.2version>
        dependency>

    dependencies>

构建mybatis目录环境

MyBatis学习笔记——SSM整合(Spring4,SpringMVC4,MyBatis3)_第1张图片

Spring相关配置文件

在/WEB-INF下的web.xml中配置Spring和SpringMVC的配置文件加载路径。


<web-app version="2.4" 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">

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>


    
    
    <servlet>
        <servlet-name>springservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>

    
    <servlet-mapping>
        <servlet-name>springservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

配置SpringMVC

SpringMVC的配置文件采用默认位置,即在WEB-INF/下,与web.xml同级目录,创建spring-servlet.xml,内容如下:


<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        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.0.xsd">

    
    
    <context:component-scan
        base-package="com.shen.mybatis" use-default-filters="false">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    context:component-scan>

    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/">property>
        <property name="suffix" value=".jsp">property>
    bean>

    
    <mvc:default-servlet-handler />
    
    <mvc:annotation-driven>mvc:annotation-driven>
beans>

配置Spring(重点)

在src/main/conf包(类路径)下,创建applicationContext.xml,和web.xml中配置的一致即可。


<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:tx="http://www.springframework.org/schema/tx"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        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.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    
    <context:component-scan
        base-package="com.shen.mybatis">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    context:component-scan>

    
    
    <context:property-placeholder
        location="classpath:dbconfig.properties" />

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}">property>
        <property name="driverClass" value="${jdbc.driver}">property>
        <property name="user" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
    bean>

    
    <bean id="dataSourceTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>

    
    <tx:annotation-driven
        transaction-manager="dataSourceTransactionManager" />

    
    
    
    
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource">property>
        
        <property name="configLocation" value="classpath:mybatis-config.xml">property>
        
        <property name="mapperLocations"
            value="classpath:mybatis/mapper/*.xml">property>
    bean>

    
    
    <mybatis-spring:scan base-package="com.shen.mybatis.dao" />
    
    
    
    
beans>

编写Mapper接口以及Mapper XML

com.shen.mybatis.dao.EmployeeMapper:

public interface EmployeeMapper {

    public Employee getEmpById(Integer id);

    public List getEmps();
}

src/main/conf/mybatis/mapper/EmployeeMapper.xml:



<mapper namespace="com.shen.mybatis.dao.EmployeeMapper">
    
    <select id="getEmpById" resultType="com.shen.mybatis.bean.Employee">
        select * from tbl_employee where
        id = #{id}
    select>

    
    <select id="getEmps" resultType="com.shen.mybatis.bean.Employee">
        select * from tbl_employee
    select>
mapper>

编写Controller以及Service

com.shen.mybatis.service.EmployeeService:

@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public List getEmps() {
        return employeeMapper.getEmps();
    }
}

EmployeeMapper使用Spring的自动注入功能,不用再使用sqlSession.getMapper来实例化。

com.shen.mybatis.controller.EmployeeController:

@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping(value = "/getemps")
    public String getAllEmps(Map map) {
        List emps = employeeService.getEmps();
        map.put("emps", emps);
        return "list";
    }
}

(1)使用@Controller注解,标识该类是一个Controller。
(2)EmployeeService属性使用@Autowired,自动注入。

前端页面

src/main/webapp/index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页title>
head>
<body>
    <a href="getemps">查询所有员工a>
body>
html>

src/main/webapp/WEB-INF/pages/list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工列表title>
head>
<body>
    <table>
        <tr>
            <td>编号td>
            <td>名字td>
            <td>邮箱td>
            <td>性别td>
        tr>
        <c:forEach items="${emps}" var="emp">
            <tr>
                <td>${emp.id}td>
                <td>${emp.lastName}td>
                <td>${emp.email}td>
                <td>${emp.gender}td>
            tr>
        c:forEach>
    table>

body>
html>

测试

基于Tomcat7.0,直接测试网页即可。

你可能感兴趣的:(Mybatis)