mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下)

继续 mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)

五、使用监听器启动Spring容器

1、修改pom.xml文件,添加Spring-web

2、修改web.xml,配置启动Spring容器

3、新建BookServer

4、新建BookServlet

5、修改ApplicationContext.xml

6、测试

继续!!!

五、使用监听器启动Spring容器

1、修改pom.xml,添加Spring-web包(注:上一篇中的pom.xml已经添加)

<!--添加spring-web包 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

2、修改web.xml文件(src/main/webapp/WEB-INF/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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <description>Spring容器启动监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:ApplicationContext.xml</param-value>
  </context-param>
</web-app>

3、新建一个BookService

package com.lmei.service;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.lmei.dao.BookDaoI;
import com.lmei.entity.Book;

@Service
public class BookService {
    @Resource
    BookDaoI bookDao;
    public List<Book> getAllBook() {
        return bookDao.getAllBook();
    }
}

@Service切记勿漏写,如果没写,运行时会报错“org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lmei.service.BookService] is defined”

4、新建BookServlet

package com.lmei.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.lmei.service.BookService;

@WebServlet("/BookServlet.do")
public class BookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    BookService bookService;

    @Override
    public void init() throws ServletException {
      //在当前上下文中获得Spring容器
      WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
      //从容器中获得bean
      bookService = ctx.getBean(BookService.class);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        writer.print(bookService.getAllBook().size());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

serialVersionUID作用:序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。

两种生成方式:

一个是默认的1L,比如:private static final long serialVersionUID = 1L;

一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如:private static final long serialVersionUID = xxxxL;

5、修改ApplicationContext.xml,新增配置

    <!--自动扫描映射接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定sql会话工厂,在上面配置过的 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 指定基础包,即自动扫描com.lmei.dao这个包下的所有接口类 -->
        <property name="basePackage" value="com.lmei.dao"></property>
    </bean>

    <!--自动扫描组件 -->
    <context:component-scan base-package="com.lmei">
        <context:exclude-filter type="aspectj" expression="com.lmei.dao.*"/>
    </context:component-scan>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

完整的ApplicationContext.xml

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

    <!-- 引入db.properties属性文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!--定义一个jdbc数据源,创建一个驱动管理数据源的bean -->
    <bean id="jdbcDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>    

    <!--创建一个sql会话工厂bean,指定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定数据源 -->
        <property name="dataSource" ref="jdbcDataSource" />
        <!--类型别名包,默认引入com.lmei.entity下的所有类 -->
        <property name="typeAliasesPackage" value="com.lmei.entity"></property>
        <!--指定sql映射xml文件的路径 -->
        <property name="mapperLocations"
            value="classpath:com/lmei/mapper/*Mapper.xml"></property>
    </bean>

    <!--自动扫描映射接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定sql会话工厂,在上面配置过的 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 指定基础包,即自动扫描com.lmei.dao这个包下的所有接口类 -->
        <property name="basePackage" value="com.lmei.dao"></property>
    </bean>

    <!--自动扫描组件 -->
    <context:component-scan base-package="com.lmei">
        <context:exclude-filter type="aspectj" expression="com.lmei.dao.*"/>
    </context:component-scan>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>

6、测试

项目右键->Run AS->Run On Server->Tomcat Server at localhost(运行前确认是否已配置Apache Tomcat)

运行成功后,在浏览器输入:http://localhost:8888/spring_mybatis_1/BookServlet.do

返回 4,表示有4条符合条件的结果。

时间: 2024-08-02 02:51:40

mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下)的相关文章

mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文件复制到src/main/webapp下,删掉WebContent 5.修改Deployment Assembly 6.测试 二.mybatis访问mysql 1.数据库准备 2.修改pom.xml 3.创建实体类 4.创建访问接口 5.添加映射文件 6.添加MyBatisCfg.xml配置文件,注

OpenStack 学习笔记(六):OpenStack neutron服务搭建

--先决条件 1.)创建数据库 MariaDB [(none)]> CREATE DATABASE neutron; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'localhost' IDENTIFIED BY 'neutron'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)

springmvc学习笔记(一) -- 从零搭建,基础入门

1.新建maven项目 参考mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)第一部分,修改配置 2.修改pom.xml 文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://mav

springmvc+mybatis学习笔记(汇总)

springmvc+mybatis学习笔记(汇总) 标签 : springmvc mybaits springmvcmybatis学习笔记汇总 目录 联系作者 笔记分为两大部分:mybatis和springmvc mybatis springmvc 笔记内容主要是mybatis和springmvc的一些基本概念和使用方法,涉及概念介绍.环境搭建.编程细节.运行调试等方面. 这套笔记整体偏入门和应用,适合快速上手,对底层实现和机理并未做过多分析.我后续会研读spring源码,并把学习的收获写成博客

mybatis学习笔记(10)-一对一查询

mybatis学习笔记(10)-一对一查询 mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实现一对一查询小结 本文使用两种方式(resultType和resultMap)实现一对一查询,查询订单信息,关联查询创建订单的用户信息 resultType实现 sql语句 确定查询的主表:订单表 确定查询的关联表:用户表 关联查询使用内连接?还是外连接? 因为orders表中有一个外键(user_id),通过外键关联查询

mybatis学习笔记(14)-spring和mybatis整合

mybatis学习笔记(14)-spring和mybatis整合 mybatis学习笔记14-spring和mybatis整合 整合思路 整合环境 sqlSessionFactory 原始dao开发和spring整合后 mapper代理开发 遇到的问题 本文主要将如何将spring和mybatis整合,只是作简单的示例,没有使用Maven构建.并展示mybatis与spring整合后如何进行原始dao开发和mapper代理开发. 整合思路 需要spring通过单例方式管理SqlSessionFa

MyBatis MapperScannerConfigurer配置――MyBatis学习笔记之八

MyBatis MapperScannerConfigurer配置——MyBatis学习笔记之八 2012-09-02 20:01:42 标签:Spring MyBatis MapperScannerConfigurer bean默认命名 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://legend2011.blog.51cto.com/3018495/980150 在上一篇博文的示例中,我们在beans.xml中配置了stu

mybatis学习笔记(1)

之前做项目的时候,DAO层写了一些spring jdbc,用起来的确不是很方便,今天特意去学习了新的框架:mybatis.把之前用spring-jdbc写的内容换成了mybatis框架搭建的内容. 首先你要到mybatis的官网去下mybatis的jar包:mybatis-3.2.7.jar.由于我是在spring的基础上去搭建mybatis所以还要去弄一个mybatis-spring-1.2.2.jar, 这个连接的包好像在spring官方是找不到的,需要自己去网上找. 进入正题.首先在src

Mybatis学习笔记(二) 之实现数据库的增删改查

开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载.首先建立一个名字为 MyBaits 的 dynamic web project 1. 可以创建maven项目,依赖的包mybatis-3.2.0-SNAPSHOT.jar,mysql-connector-java-5.1.22-bin.jar <!-- mybatis包 --> <depe