准备工作:

  下载整合所需的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. Redis缓存服务搭建及实现数据读写--转载

    来自 http://www.cnblogs.com/lc-chenlong/p/3218157.html 1.  下载安装Redis 下载地址:https://github.com/MSOpenTec ...

  2. jQuery常用的查找Dom元素方法

    废话不多说,先来个总结,然后下面是demo 一. 同级节点之间的检索(检索深度N=0) next()是在兄弟节点中,往后匹配; prev()是在兄弟节点中,往前匹配. 二. 父级/子级节点的检索(检索 ...

  3. 轻量级sqlite是增删改查

    --创建数据库 create database ios --使用数据库 use ios --创建数据表 create table student ( stuid int primary key aut ...

  4. Inside dependency property

    依赖属性的定义,分为3步(以PresentationFramework中的System.Windows.Controls.Button为例) 1.  声明依赖属性 public static read ...

  5. Memcache第一篇---基础教程

    Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. 它可以应 ...

  6. ue4访问php接口

    继上一篇介绍ue4打开web url窗口,这篇就来介绍下怎么访问php接口. 要做的两步: 1.c++自己写个接受请求的方法 f Post lhc-URL Request就是自定义的c++方法, /* ...

  7. 手机端 H5上传头像

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. 让程序同时只能运行一个C++ Builder实现(转)

    源:让程序同时只能运行一个 很多人都讨论过这个问题, 这里用Victor串口控件里面现成的共享内存功能来实现. 当程序运行第二次时只是激活第一次运行的窗口, 而不是再运行一个程序. 需要在主程序里实现 ...

  9. 字典NSDictionary的常见用法

    // 动态获取字典的第一个典 NSString *firstKey = responseObject.keyEnumerator.nextObject;

  10. PHP7新特性

    重写ZenVM,性能比PHP5.6提升300% 新特性: 1.变量类型(为PHP7.1的JIT特性做准备)function test(int $a, string $b, array $c) : in ...