准备工作:

  下载整合所需的jar包 点击此处下载

  使用MyBatis Generator生成dao接口、映射文件和实体类 如何生成

搭建过程:

  先来看一下项目的 目录结构

  

  1.配置dispatcherServlet-servlet.xml,将此文件放于WEB-INF下

  

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

    <!-- 自动扫描Controller包 -->
    <context:component-scan base-package="com.ssm.controller"/>

    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />

    <!-- 视图解释配置 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
        <property name="suffix" value="" />
    </bean>

</beans>

  2.配置web.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <display-name></display-name>

    <!-- 欢迎页面 -->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <!-- spring mvc的配置 -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 解决访问静态资源问题 -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>

    <!-- 解决中文乱码 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 加载Spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

  3.配置applicationContext.xml,并 将自动生成的dao实现类交给Spring管理

 

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.1.xsd
        http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <!-- 定义dbcp数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <!-- 指定JDBC驱动类 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver">
        </property>
        <!-- 提供连接数据库的URL地址 -->
        <property name="url" value="jdbc:mysql://localhost:3306/easybuy">
        </property>
        <!-- 提供连接数据库的用户名和密码 -->
        <property name="username" value="root"></property>
        <property name="password" value="pengxiongpengdi"></property>
    </bean>

    <!-- 定义SessionFactory Bean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="mapperLocations" value="classpath:com/buy/dao/*.xml"></property>
    </bean>

    <!-- 此处 将自动生成的dao实现类交给Spring管理 -->
    <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.buy.dao.UserEntityMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

    <!--开启注解 将com.jbit包中所有使用了注解的类扫描,然后创建成对象 -->
    <context:component-scan base-package="com.buy" />
</beans>

  4.将service实现类交给Spring管理

@Service("userService")
public class UserServiceImpl implements IUserService {

    @Autowired  // 此处使用ByType自动装配 dao实现类
    private UserEntityMapper userDao;

    @Override
    public UserEntity login(UserEntity user) {
        return userDao.getByNameAndPwd(user);
    }

    @Override
    @Transactional  // 使用该注解实现事务管理
    public void delById(String id) {
        userDao.delByid(id);
    }
}

  5.创建Controller

@Controller@RequestMapping(value="/emp")
public class EmpController {

    @Autowired  // 使用ByType装配private IEmpService empService;

    @RequestMapping(value="/queryEmps")
    public String queryEmps(HttpServletRequest request){
        List<Emp> list = empService.queryAll();
        request.getSession().setAttribute("list",list );
        return "index.jsp";
    }

  6.现在就可以使用 http://localhost:8080/ProjectName/emp/queryEmps访问到上述Controller

SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql的更多相关文章

  1. SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)

    1.开发环境搭建以及创建Maven Web项目 参看之前的博文[确保maven web项目不报错]:http://www.cnblogs.com/cainiaomahua/p/6306476.html ...

  2. SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(Spring+SpringMVC+MyBatis) ]

    SSM_BookSystem SSM框架基础 SSM_BookSystem ---> Hello CRUD 说明:本项目目前包含基础的CRUD 日期:2017-05-01 22:25:37 作者 ...

  3. SSM框架整合(Spring + SpringMVC + MyBatis)

    搭建环境 使用Spring(业务层)整合其他的框架SpringMVC(表现层)和MyBatis(持久层) Spring框架 创建数据库表 CREATE DATABASE ssm; USE ssm; C ...

  4. Maven+SSM框架搭建【spring+springmvc+mybatis】

    本案例用到:ssm[spring+springmvc+mybatis]框架 数据库:mysql (推荐使用mysql 或者 sqlserver  .oracle太大,一般大型项目才会用到) 开发工具: ...

  5. SSM框架的配置Spring+Springmvc +Mybatis

    ssm框架是由spring mvc +spring+mybatis组成 快速阅读 通过spring的配置文件spring.xml,在servlet中指定spring mvc的配置文件spring-mv ...

  6. SSM后台管理系统(Spring SpringMVC Mybatis Mysql EasyUI)

    非常简单的一个后台管理系统,功能不多,框架也不复杂, 源码下载(附数据库)-ssm后台管理系统框架(Spring mvc + mybatis + mysql + easyui ) 实例图片

  7. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...

  8. SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例

    简介: SSM框架是Spring,SpringMVC 和Mybatis框架的整合,是标准的MVC模式,将整个系统划分为表现层,controller层,service层,dao层四层. Spring实现 ...

  9. 04_SSM框架整合(Spring+SpringMVC+MyBatis)

    [SSM的系统架构] [整合概述] 第一步: MyBatis和Spring整合,通过Spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在Spring中进行注册. 第二 ...

随机推荐

  1. Sping--AOP--Annotation

    Aspectj 概念: 1. joinpoint:切入点, 比如@Before, @After, @Around 2. Pointcut:切入点集合, 比如 @Pointcut("execu ...

  2. 用php自动发邮件的简单实现

    如何自动发送邮件? php自带mail方法,不过只能在linux下直接使用,windows下要配置smtp服务器,有点麻烦. 可以用一些现成的类来实现,比如很有名的phpmailer,功能很强大,代码 ...

  3. gtk

    GTK官网:www.gtk.org sudo apt-get install build-essential 安装GTK开发套件: sudo apt-get install libgtk2.0-dev ...

  4. 跳舞链 Dancing Links

    作为搜索里面的一个大头,终于刷了一部分题目了,跳舞链一般都有现成的模板来套...... 至于跳舞链的学习的话,我觉得http://www.cnblogs.com/grenet/p/3163550.ht ...

  5. FZU 2091 播放器

    简单模拟题,开个栈维护一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<st ...

  6. 微信小程序之----接口调用方式

    最近开发了一个微信小程序版的任务管理系统,在向Java后台发送接口时遇到了一些问题,在这里做一个简单的总结. 官方接口 官方给出的接口叫做wx.request,请求方式比较简单,下面是官网给出的请求实 ...

  7. Centos 修改时间地区及NTP同步北京时间

    在我们使用CentOS系统的时候,也许时区经常会出现问题,有时候改完之后还是会出错,下面我们就来学习一种方法来改变这个状况.如果没有安装,而你使用的是 CentOS系统 那使用命令 yum insta ...

  8. Makefile — 基础

    参考: 跟我一起写 Makefile GNU make <GNU+Make项目管理(第三版)> 1.Makefile用途 使用GNU Make工具来管理程序是每个Linux工程师必须掌握的 ...

  9. ps--记录几个方法步骤

    1.图片文字去掉 1.1 矩形工具-->吸管-->alt+delete 1.2 钢笔工具-->Ctrl+回车(变换选区)-->吸管-->alt+delete 2.图层锁不 ...

  10. Object修改链表

    以前学习过链表的时候由于类型的接收不同,每次要重写链表 下面修改可用链表 class Link{ private class Node{ private Object data ; private N ...