SpringMVC+Mybatis学习

简单Web项目搭建:

一.流程

1. 导包

  n个springMVC;

  2个mybatis<其中一个是mybatis-spring>;

  3个jackson包;

2. xml配置

  web.xml和applicationContext.xml

3. 建包,建接口,建类

4. 建jsp

二:具体分说

1. XML配置:

1.1 web.xml配置:

  字符编码器和SpringMVC控制器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    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">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <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>
  </filter>

  <filter-mapping>
      <filter-name>CharacterEncodingFilter</filter-name>
      <url-pattern>*.do</url-pattern>
  </filter-mapping>

  <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
      </init-param>
  </servlet>

  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>

  a.字符编号器 :filter

 1   <filter>
 2       <filter-name>CharacterEncodingFilter</filter-name>
 3       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 4       <init-param>
 5           <param-name>encoding</param-name>
 6           <param-value>utf-8</param-value>
 7       </init-param>
 8   </filter>
 9   <filter-mapping>
10       <filter-name>CharacterEncodingFilter</filter-name>
11       <url-pattern>/*</url-pattern>
12   </filter-mapping>

  b.SpringMVC控制器:Servlet

 1   <servlet>
 2       <servlet-name>DispatcherServlet</servlet-name>
 3       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4       <init-param>
 5           <param-name>contextConfigLocation</param-name>
 6           <param-value>classpath:applicationContext.xml</param-value>
 7       </init-param>
 8       <load-on-startup>1</load-on-startup>
 9   </servlet>
10   <servlet-mapping>
11       <servlet-name>DispatcherServlet</servlet-name>
12       <url-pattern>/</url-pattern>
13   </servlet-mapping>

1.2 applicationContext.xml配置:

  注解扫描、mvc驱动、数据源、sqlSessionFactory、dao层帮助类、事务管理、事务驱动(没有先后顺序)

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.1.xsd

    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.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

    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    ">

    <context:component-scan base-package="cn.bdqn.ssm"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="user" value="tb28"></property>
        <property name="password" value="accp"></property>
    </bean>

    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="cn.web.ssm" ></property>
    </bean>

    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
        <property name="basePackage" value="cn.web.ssm.dao"></property>
    </bean>

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

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

</beans>

  a. 注解扫描:

1 <context:component-scan base-package="cn.bdqn.ssm">
2 </context:component-scan>

  b. mvc驱动:

<mvc:annotation-driven></mvc:annotation-driven>

  c. 数据源:看导入的jdbc及数据库的包:本次是 c3p0,ojdbc(Oracle数据库)

1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
2     <property name="user" value="betterLearning"></property>
3     <property name="password" value="gzh"></property>
4     <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
5     <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
6 </bean>

  d. sqlSessionFactory:要从数据源中获取

1 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
2     <property name="dataSource" ref="dataSource"></property>
3     <property name="typeAliasesPackage" value="cn.web.ssm"></property>
4 </bean>

  e. dao层帮助类:扫描xxxDao.xml,xxxDao.xml作为实现类放入容器中

1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
2     <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
3     <property name="basePackage" value="cn.web.ssm.dao"></property>
4 </bean>

  f. 事务管理:也要从数据源中获取

1 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
2     <property name="dataSource" ref="dataSource"></property>
3 </bean>

  g. 事务驱动:

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

三、建包、建接口、建类(展示部分)

流程:entity--dao--service(包括serviceImpl)--controller

  a. entity包:Ticket.java

package cn.web.ssm.entity;

public class Ticket {
    private Integer id;
    private String company;
    private Double price;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Ticket [id=" + id + ", company=" + company + ", price=" + price
                + "]";
    }
}

  b. dao包:TicketDao.xml和TicketDao.java;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" >
<mapper namespace="cn.web.ssm.dao.TicketDao">
  <select id="getTicketsById" resultType="Ticket">
    select t.id id,t.sell_company company,t.ticket_price price
    from flight_tickets t where t.flight_id=#{id}
  </select>
</mapper>
package cn.bdqn.ssm.dao;
import java.util.List;
import cn.web.ssm.entity.Ticket;

public interface TicketDao {
    List<Ticket> getTicketsById(Integer id);
}

  c. service包:TicketService.java和TicketServiceImpl.java

package cn.web.ssm.service;
import java.util.List;
import cn.web.ssm.entity.Ticket;

public interface TicketService {
    List<Ticket> getTicketsById(Integer id);
}
package cn.web.ssm.service;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.web.ssm.dao.TicketDao;
import cn.web.ssm.entity.Ticket;

@Service
@Transactional
public class TicketServiceImpl implements TicketService {
    @Autowired
    private TicketDao ticketDao;

    public void setTicketDao(TicketDao ticketDao) {
        this.ticketDao = ticketDao;
    }
    public List<Ticket> getTicketsById(Integer id) {
        return ticketDao.getTicketsById(id);
    }
}

  d.controller包:TicketController.java;

package cn.web.ssm.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.web.ssm.entity.Ticket;
import cn.web.ssm.service.TicketService;

@Controller
public class TicketController {
    @Autowired
    private TicketService ticketService;

    public void setTicketService(TicketService ticketService) {
        this.ticketService = ticketService;
    }

    @RequestMapping("showTicket")
    @ResponseBody
    public List<Ticket> showTicket(Integer id) {

        List<Ticket> tickets = ticketService.getTicketsById(id);
        for (Ticket ticket : tickets) {
            System.out.println(ticket);
        }
        return tickets;
    }
}
时间: 2024-12-15 15:25:46

SpringMVC+Mybatis学习的相关文章

springmvc+mybatis学习笔记(汇总)

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

Spring+SpringMVC+MyBatis深入学习及搭建(三)——MyBatis全局配置文件解析

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6874672.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发 MyBatis的全局配置文件SqlMapConfig.xml,配置内容和顺序如下: properties(属性) setting(全局配置参数) typeAliases(类名别名) typeHandlers(类名处理器) objectFactory(对

Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——SpringMVC注解开发(基础篇) 本文主要内容: (1)SpringMVC校验 (2)数据回显 (3)异常处理器 (4)图片上传 (5)Json数据交互 (6)支持RESTful 1.SpringMVC校验 1.1校验理解 项目中,通常使用较多的是前端的校验,比如页面中js校验.对于安全要求较高的

Spring+SpringMVC+MyBatis深入学习及搭建(十一)——SpringMVC架构

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6985816.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十)--MyBatis逆向工程 1.什么SpringMVC Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一个模块.SpringMVC和Spring无需通过中间整合层进行整合. SpringMVC是一个基于mvc的web框架. 2.mvc在b/s系统下的应用 (1)用户发

Spring+SpringMVC+MyBatis深入学习及搭建(十五)——SpringMVC注解开发(基础篇)

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7065294.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十四)--SpringMVC和MyBatis整合 1.商品修改功能开发 1.1需求 操作流程: (1)进入商品查询列表页面: (2)点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询),  要修改的商品从数据库查询,根据商品id(主键)查询商品信息: (3)在商品修改页面,修改商品信息,修改后,

Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射 mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sq

Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发(转发同上)

前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)--MyBatis的基础知识.MybatisFirst中存在大量重复的代码.这次简化下代码: 原地址:http://www.cnblogs.com/shanheyongmu/p/7121016.html 使用MyBatis开发Dao,通常有两种方法,即原始Dao开发方法和Mapper接口开发方法. 1.SqlSession使用范围 1.1 SqlsessionFactoryBuilder 通过SqlSessionFac

Spring+SpringMVC+MyBatis深入学习及搭建(十)——MyBatis逆向工程

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6973266.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(九)--MyBatis和Spring整合 使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件. 1.什么是逆向工程 mybatis需要程序员自己编写sql语句,mybatis官方提供逆向工程可以针对单表自动生成mybatis执行所需要

Spring+SpringMVC+MyBatis深入学习及搭建(十四)——SpringMVC和MyBatis整合

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7010363.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十三)--SpringMVC入门程序(二) 1.需求 使用springmvc和mybatis完成商品列表查询. 2.整合思路 springmvc+mybatis的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mappe