ssm整合

功能:在页面上显示数据库内的商品信息

项目目录结构:

数据库表Items

entity----Item.java

package com.it.ssm.entity;

import java.io.Serializable;
import java.util.Date;

import org.springframework.stereotype.Component;

@Component
public class Item implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    // 商品id
    private int id;
    // 商品名称
    private String name;
    // 商品价格
    private double price;
    // 商品创建时间
    private Date createtime;
    // 商品描述
    private String detail;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public Date getCreatetime() {
        return createtime;
    }
    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }
    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail;
    }
    @Override
    public String toString() {
        return "Item [id=" + id + ", name=" + name + ", price=" + price
                + ", createtime=" + createtime + ", detail=" + detail + "]";
    }

}

Dao---ItemDao

package com.it.ssm.dao;

import java.util.List;

import com.it.ssm.entity.Item;

public interface ItemDaor {

    public List<Item> selectByExample();

}

ItemDao的映射文件 ItemMapper.xml

<?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="com.it.ssm.dao.ItemMapper">
    <sql id="Base_Column_List">
        id,name,price,createtime,detail
    </sql>
    <select id="selectByExample" resultType="com.it.ssm.entity.Item">
        select
        <include refid="Base_Column_List"/>
        from items
    </select>
</mapper>

service---ItemService

package com.it.ssm.service;

import java.util.List;

import com.it.ssm.entity.Item;

public interface ItemService {
    //查询商品
    public List<Item> selectItemList();
}

serviceImpl---ItemServiceImpl

package com.it.ssm.service;

import java.util.List;

import javax.annotation.Resource;

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

import com.it.ssm.entity.Item;
import com.it.ssm.mapper.ItemMapper;

/**
 * 查询商品信息
 * @author Administrator
 *
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ItemMapper itemMapper;
   //查询商品
    public List<Item> selectItemList(){
        return itemMapper.selectByExample();
    }
}

db.perproties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=root
jdbc.password=0821

spring配置文件----applicationContext.xm

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 数据库连接池 -->
    <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="10"/>
        <property name="maxIdle" value="5"/>
    </bean>
    <!-- 配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置mybatis核心文件 -->
        <property name="mapperLocations" value="*/mapper/*.xml"/>
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 配置MapperScannerConfigurer  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.it.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>

springmvc配置文件---springmvc.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: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.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 配置controller扫描包 -->
    <context:component-scan base-package="com.it"/>

    <!-- 注解驱动 -->
    <mvc:annotation-driven/>

    <!-- 注册视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

ItemList.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
    <td>商品名称</td>
    <td>商品价格</td>
    <td>生产日期</td>
    <td>商品描述</td>
    <td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
    <td>${item.name }</td>
    <td>${item.price }</td>
    <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
    <td>${item.detail }</td>

    <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置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>
  <!-- 配置springmvc前端控制器 -->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

controller---ItemController.java

package com.it.ssm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

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

import com.it.ssm.entity.Item;
import com.it.ssm.service.ItemService;

@Controller
public class ItemController {

    @Resource
    private ItemService itemService;

    @RequestMapping("/itemList.action")
    public String queryItemList(HttpServletRequest request){
        List<Item> list = itemService.selectItemList();
        request.setAttribute("itemList", list);
        return "itemList";
    }
}

实现的效果

时间: 2024-08-12 15:57:27

ssm整合的相关文章

SSM整合框架实现ajax校验

SSM整合框架实现ajax校验 刚学习了ssm框架,ajax校验成功,分享下 1.导入jar包 2.配置spring-servlet.xml 1 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 2 <property name="messageConverters"> 3 <list> 4 &l

ssm 整合(方案二 maven)

通过maven来整合ssm方便很多,至少不用去找jar包 具体架构如下: 1.配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache

【转】ssm整合

http://m.blog.csdn.net/article/details?id=44455235 SSM框架--详细整合教程(Spring+SpringMVC+MyBatis) 发表于2015/3/19 11:44:55  576280人阅读 分类: Spring MVC 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的过程,这次刚刚好基于

SSM整合配置

SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis) 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些.以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下.这次,先说说三大框架整合过程.个人认为使用框架并不是很难

SSM整合步骤

SSM- CRUD SSM : SpringMVC+Spring+Mybatis Create (新建) +Retrieve (查询) +Update(更新)+Delete(删除) 功能点 1:分页 2:数据校验:JQuery前端校验+JSR后端校验. 3:ajax 4:Rest风格URL:使用HTTP协议请求方式的动词,来表示对对资源的操作:GET(查询).POST(新增).PUT(修改).DELETE(删除). 技术点 1:基础框架-SSM (SpringMVC+Spring+MyBatis

ssm整合时出现 org.springframework.beans.factory.BeanCreationException :Error creating bean with name ‘XXX’ 异常的原因及解决方法

ssm整合时出现 org.springframework.beans.factory.BeanCreationException :Error creating bean with name 'XXX' 异常的原因及解决方法(只是可能出现下列几种,不包含全部) 此异常为:注入 bean 失败异常,也就是找不到注入的bean. 可能有以下几种原因: 1.bean未注解或者注解错误 2.项目整合的时候jar包冲突 3.'XXX'的配置有错误 解决:1,3仔细检查就是,网上大部分的人应该是2这种错误,

SSM整合学习笔记

SSM整合核心: 1.持久层: org.mybatis.spring.mapper.MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring <!-- 使用mapper批量扫描器扫描mapper接口 规则:mapper.xml和mapper.java在一个目录 且同名即可 扫描出来mapper,自动让spring容器注册,bean的id就是mapper类名(首字母小写) --> <bean class="org.mybatis.sp

ssm 整合中js,css 文件无法引入

问题:ssm 整合中第三方 js,css 文件无法引入 检查:ssm 整合配置完好 无拦截器拦截 spring mvc  静态资源已配置 编译时可以直接跳转到js  css 问题发现 js  css 文件放在WEB-INF 下,导致无法引入 解决 js  css 文件放在webapp 下,可以引入 原文地址:https://www.cnblogs.com/jsbk/p/9461374.html

重磅回归-SSM整合进阶项目实战之个人博客系统

历经一个多月的重新设计,需求分析以及前后端开发,终于有了一定的输出:我自己实现的spring4+springmvc+mybatis3整合的进阶项目实战-个人博客系统 已然完成了,系统采用mvc三层模式进行整体的开发,涉及到技术一下子很难全部列出,其中不得不提的有:整合shiro实现登录安全认证,整合lucene实现全文信息检索,基于Spring的事件驱动模型实现业务服务模块之间的异步解耦(在RabbitMQ视频教程中我也会重提这个技术点!),爬虫框架Jsoup解析html文本中的图片,整合ued

ssm整合简单配置

最近由于系统重装,之前已经写好了的框架都被我删的一干二净,于是自己动手重新搭了个简答的ssm 运行环境 (java1.8,Tomcat8.5,maven3.5,MySQL6.0) pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http