1 .pom.xml引入spring和mybatis的jar包

2.spring的mvc.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

<!-- 使用Annotation自动注册Bean,只扫描@Controller -->
<context:component-scan base-package="qianliyan.controller" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
<!-- 视图解析器
解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>

3. 配一下spring和mybatis整合文件applicationContext-dao.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:iluxday.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<!-- 连接池最大数量 -->
<property name="maxActive" value="30"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="5"></property>

</bean>
<!--spring与mybatis整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<!--
<property name="mapperLocations" value="classpath:com/ssm/mapping/*.xml"></property>
-->
<!-- 全局配置 -->
<!-- 若不保留mybatis配置文件用上面那条替换这一条 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,需要扫描多个包中间用逗号隔开 -->
<property name="basePackage" value="qianliyan.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<context:component-scan base-package="qianliyan.service.impl"/>
</beans>

4再配置mybatis的配置文件sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<!-- 注册userMapper.xml文件 -->
<mapper resource="qianliyan/mapping/ProductSaleTopMapper.xml"/>

</mappers>
</configuration>

5.web.xml文件中配置spring和springmvc的容器

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!-- Context ConfigLocation -->
<!-- Spring容器和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext-*.xml</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 防止Spring内存溢出监听器 -->
<!-- MVC servlet -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

6. 创建pojo类

package qianliyan.pojo;

public class ProductSaleTopModel {
private Integer productItemID;
private String ordersDate;

private Integer quanlity;

public Integer getProductItemID() {
return productItemID;
}

public void setProductItemID(Integer productItemID) {
this.productItemID = productItemID;
}

public String getOrdersDate() {
return ordersDate;
}

public void setOrdersDate(String ordersDate) {
this.ordersDate = ordersDate;
}

public Integer getQuanlity() {
return quanlity;
}

public void setQuanlity(Integer quanlity) {
this.quanlity = quanlity;
}
}

7。创建mapper.xml和Dao接口

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="qianliyan.dao.ProductSaleTopDao" >

<resultMap id="productSaleTopMap" type="qianliyan.pojo.ProductSaleTopModel">

<result column="ProductItemID" property="productItemID" jdbcType="INTEGER"/>
<result column="OrdersDate" property="ordersDate" jdbcType="VARCHAR"/>
<result column="Quantity" property="quanlity" jdbcType="INTEGER"/>
</resultMap>
<!-- 查询所有 -->
<select id="getAll" resultMap="productSaleTopMap">
select * from SaleProductTOP
</select>
</mapper>

package qianliyan.dao;
import java.util.List;

import qianliyan.pojo.*;

public interface ProductSaleTopDao {

public List<ProductSaleTopModel> getAll();
}

8。创建UserService 和UserServiceImp

package qianliyan.service;

import java.util.List;

import qianliyan.pojo.ProductSaleTopModel;

public interface ProductSaleTopService {
public List<ProductSaleTopModel> getAll();
}

package qianliyan.service.impl;

import java.util.List;

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

import qianliyan.dao.ProductSaleTopDao;
import qianliyan.pojo.ProductSaleTopModel;
import qianliyan.service.ProductSaleTopService;

@Service("productSaleTopService")
public class ProductSaleTopServiceImpl implements ProductSaleTopService{

@Autowired
private ProductSaleTopDao productSaleTopDao;
@Override
public List<ProductSaleTopModel> getAll() {
// TODO Auto-generated method stub
return productSaleTopDao.getAll();
}

}

9。创建控制层Controller.java

package qianliyan.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import qianliyan.pojo.ProductSaleTopModel;
import qianliyan.service.ProductSaleTopService;

@Controller
public class ProductSaleTop {
@Autowired
private ProductSaleTopService productSaleTopService;
@RequestMapping(value="/demo")
public String index(HttpServletRequest request, HttpServletResponse response, Model model){
System.out.println("Hello world!");

List<ProductSaleTopModel> list= productSaleTopService.getAll();
for(ProductSaleTopModel product : list)
{
System.out.println(product.getProductItemID()+":"+product.getQuanlity());
}
return "ProductSale";
}
}

10 在WEB-INF/jsps/下创建ProductSale.jsp

转载于:https://www.cnblogs.com/lovefendi/p/7526828.html

springMVC和mybatis的框架搭建相关推荐

  1. myeclipse下使用maven搭建SSM(spring、springmvc、mybatis)框架

    转自:http://blog.csdn.net/u012767369/article/details/70209400 MyEclipse配置Maven 1.在本地创建一个文件夹MavenReposi ...

  2. Spring+Mybatis+SpringMVC+Maven+MySql(SSM框架)搭建实例

    这篇文章我们来实现使用maven构建工具来搭建Spring+Mybatis+SpringMVC+MySql的框架搭建实例.工程下载 使用maven当然得配置有关环境了,不会配置的请看我前几篇文章,都有 ...

  3. 01-Intellij IDEA搭建SSM(SpringMVC+Spring+Mybatis+Maven)框架(上)

    1.环境搭建 (1)JDK 1.8下载安装 https://www.oracle.com/cn/java/technologies/javase-jdk8-downloads.html (2)Apac ...

  4. 01-Intellij IDEA搭建SSM(SpringMVC+Spring+Mybatis+Maven)框架(下)

    SSM入门实战(登录功能的实现) (1)数据库文件--->user.sql /*Navicat Premium Data TransferSource Server : MySQLSource ...

  5. SpringMVC和mybatis的框架

    1.首先以一个项目做例子,该项目用到的框架即为SpringMVC+mybatis,项目环境为MyEclipse+sqlserver+tomcat6,项目的地址(项目+数据库备份文件)大家可以上我的百度 ...

  6. SMM - Spring,SpringMVC,MyBatis 三大框架整合

    开发环境 真实项目开发最流行的开发结构体系是SMM整合 web层用 -> SpringMVC service层 -> Spring dao层用 ->MyBatis 项目结构 项目源码 ...

  7. idea springmvc_SSM三大框架使用Maven快速搭建整合(SpringMVC+Spring+Mybatis)

    本文介绍使用SpringMVC+Spring+MyBatis三大框架使用Maven快速搭建一个demo,实现数据从数据库中查询返回到页面进行展示的过程. 技术选型:SpringMVC+Spring+M ...

  8. spring+websocket综合(springMVC+spring+MyBatis这是SSM框架和websocket集成技术)

    java-websocket该建筑是easy.儿童无用的框架可以在这里下载主线和个人教学好java-websocket计划: Apach Tomcat 8.0.3+MyEclipse+maven+JD ...

  9. SSM Chapter 12 SpringMVC扩展和SSM框架整合

    SSM Chapter 12 SpringMVC扩展和SSM框架整合 笔记 本章目标: 掌握JSON对象的处理 理解数据转换和格式化 了解本地化 掌握Spring MVC+Spring+MyBatis ...

最新文章

  1. 上三角矩阵的特征值分解
  2. RestHighLevelClient 批量插入 elasticsearch 7.9 版本警告
  3. c++ 管理员身份_CATIA的管理员模式和多版本环境变量设置
  4. Python 内置模块之 asyncio(异步iO)
  5. 【hibernate merge】session1.merge(T entity)方法的含义和update方法的区别
  6. 用@Scheduled完成定时任务
  7. 十六进制转二进制原理
  8. HEVC与AVC的区别与联系(十二)
  9. 电池SOC仿真系列-基于GA-BP神经网络的电池SOC估算方法
  10. JS调用Arcgis实现地图中心点画圆
  11. OBS_Classic经典版框架
  12. PAT --- 1072.开学寄语 (20 分)
  13. mac vscode latex 中文乱码 怎么解决
  14. 百度地图集成(一):百度地图简单实现
  15. html版心宽度怎么设置,Word中版心尺寸是什么意思?怎样设置版心尺寸?
  16. C#基于NAudio的声音识别(一)——录制与切割
  17. CyberGhost使用、下载、注册【完整教程】
  18. CSAPP Lab3 实验记录 ---- Attack Lab(Ctarget)
  19. 信号间隔是什么意思_地铁信号里的行车闭塞是啥意思?
  20. html字体颜色代码属性,CSS文本字体颜色(CSS color)

热门文章

  1. 第4届华为编程大赛决赛试题解答(棋盘覆盖)
  2. s一般怎么称呼自己的m_“老公、老婆、亲爱的”?快来围观明星怎么称呼自己的另一半!...
  3. [C语言错误]expected declaration or statement at end of input)
  4. js禁止鼠标滑轮_js 禁止鼠标滑轮滚动的事件
  5. get请求获得传递参数
  6. 绘制奥林匹克五环旗。
  7. [向前一小步]Android简单实现指纹识别登录
  8. mac电脑怎么彻底关闭系统更新提示?
  9. 网易云接口手机号验证码验证登录
  10. 魅族20和魅族20pro的区别 魅族20和20pro参数对比