简单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"?>
<beansxmlns="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.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvchttp://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;}@Overridepublic 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 pricefrom 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 {@Autowiredprivate 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 {@Autowiredprivate TicketService ticketService;public void setTicketService(TicketService ticketService) {this.ticketService = ticketService;}@RequestMapping("showTicket")@ResponseBodypublic List<Ticket> showTicket(Integer id) {List<Ticket> tickets = ticketService.getTicketsById(id);for (Ticket ticket : tickets) {System.out.println(ticket);}       return tickets;}
}

转载于:https://www.cnblogs.com/gzhcsu/p/6753338.html

SpringMVC+Mybatis学习相关推荐

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

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6973266.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(九)--My ...

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

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6985816.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十)--My ...

  3. Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7098753.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十六)--S ...

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

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7010363.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十三)--S ...

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

    前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)--MyBatis的基础知识.MybatisFirst中存在大量重复的代码.这次简化下代码: 使用MyBatis开发Dao ...

  6. [Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP

    上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(三):Spring实现JDBC 下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(五):SpringA ...

  7. 事务中mybatis通过id查不到但是通过其他条件可以查到_40打卡 MyBatis 学习

    第57次(mybatis) 学习主题:mybatis 学习目标: 1 掌握框架的概念 2 掌握mybatis环境搭建 对应视频: http://www.itbaizhan.cn/course/id/8 ...

  8. idea springmvc_手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路

    手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis 本文中的图片用了个人服务器存储,网速较慢,各位老司机耐心等待. 文章开始之前,小编准备先来一波福利youhuo ...

  9. (五)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven构建根项目

    上一篇我们介绍<springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven模块规划>,从今天开始,我们将对代码的每一个构建做详细的记录,能够帮助大家 ...

最新文章

  1. SpringMVC注解@initbinder解决类型转换问题
  2. c语言return 11,二级C语言教程章节测试11.对函数的进一步讨论
  3. sql 删除用户失败
  4. 实锤!沙特新规,出货箱单必须显示条形码,发票必须盖章!
  5. dw按钮图片滚动js_轮播图--swiper插件/原生js/jQuery
  6. 体验 PHP under .NET Core
  7. 谈谈HashMap线程不安全的体现
  8. LeetCode 45. 跳跃游戏 II(贪心/BFS,难)
  9. 中关村在线 测试软件,主观测试软件:ZOL Monitor LCD 简介
  10. 从月薪 1000 到 2W+,文科生如何逆袭成为大厂程序员?
  11. Android自定义样式
  12. python做3d相册_简单3D翻页相册制作教程(示例代码)
  13. Huginn实现自动通过slack推送豆瓣高分电影
  14. php数据存储mysql_php mysqli 存储数据库
  15. 二叉树的中序遍历-递归和非递归算法
  16. 常用URL分享,实用地址
  17. C语言abs和labs函数详解和示例
  18. java中e.printStackTrace()不要使用,请使用logger记录
  19. 多玩英雄联盟插件盒子v3.8.0
  20. 为什么你打工10年还是穷

热门文章

  1. c 写c语言代码编辑器,最好用的c/cpp代码编辑器是vim,没有之一
  2. 如果让我重做一次研究生--王泛森院士
  3. 【软件设计师】2020-08-07
  4. POJ - 1321 棋盘问题
  5. 8051中断系统介绍
  6. 光模块、连接器、光纤的常用知识
  7. web压力测试之siege
  8. React with Webpack -1: 介绍Helloworld
  9. 信用逾期3年是不是一定会坐牢?
  10. 《命犯桃花》离斯蒂芬·金还很遥远