ssm整合

本文档详细介绍了SSM(Spring、SpringMVC、MyBatis)框架的整合过程,包括所需的jar包、实体类、基础类、扩展类、Mapper接口及XML配置、Service接口及实现类、前端控制器的配置。内容涵盖从数据库连接到前端展示的所有步骤,适合初学者和开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

培训班的东西,内容比较多

————————————————————————————————————————————————————————————————————————


————————————————————————————————————————————————————————————————————

所需jar包 :主要就是mybatis的包 和springmvc的包 以及它们的整合包

——————————————————————————————————————————————————————————————————————

实体类:com.myself.ssm.po

基础类:Items

package com.myself.ssm.po;

import java.util.Date;

public class Items {
    private  Integer id;
    private  String name;
    private  float price;
    private String pic;
    private Date createtime;
    private String detail;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getPic() {
        return pic;
    }
    public void setPic(String pic) {
        this.pic = pic;
    }
    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;
    }
    public void setCreatetime(String string) {
        // TODO Auto-generated method stub
        
    }
}

——————————————————————————————————————

扩展类:ItemsCustom

package com.myself.ssm.po;


/*商品信息的扩展类
 *
 */
public class ItemsCustom extends Items{
   
    //添加商品扩展的属性
    
}

——————————————————————————————————————

包装类:ItemsQueryVo

package com.myself.ssm.po;

/**
 *
 * @author Administrator
 *
 */

public class ItemsQueryVo {
 
      //商品信息
    private Items items;
    //为了系统的可扩展性,对原始生成po进行扩展
    private ItemsCustom  itemsCustom;
    public Items getItems() {
        return items;
    }
    public void setItems(Items items) {
        this.items = items;
    }
    public ItemsCustom getItemsCustom() {
        return itemsCustom;
    }
    public void setItemsCustom(ItemsCustom itemsCustom) {
        this.itemsCustom = itemsCustom;
    }
}

——————————————————————————————————————————————————————————————————

对应mapper包:com.myself.ssm.mapper

ItemsMapper的mapper:

package com.myself.ssm.mapper;

import java.util.List;

import com.myself.ssm.po.Items;
import com.myself.ssm.po.ItemsCustom;
import com.myself.ssm.po.ItemsQueryVo;

public interface ItemsMapper {
    //商品查询列表
    public List<Items>  findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
    
    public ItemsCustom findItemsById(Integer id) throws Exception;
    
    //更新商品信息
    public void updateItems(ItemsCustom itemsCustom)throws Exception;
}
————————————————————————————————

ItemsMapper的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.myself.ssm.mapper.ItemsMapper">

    <resultMap id="BaseResultMap" type="Items" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="price" property="price" jdbcType="VARCHAR" />
        <result column="pic" property="pic" jdbcType="VARCHAR" />
        <result column="createtime" property="createtime" jdbcType="DATE" />
        <result column="detail" property="detail" jdbcType="VARCHAR" />
    </resultMap>
    
    <sql id="Base_Column_List" >
        name, price, detail
    </sql>
 
    <select id="findItemsList" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select
        <include refid="Base_Column_List" />
        from student
        where id = #{id,jdbcType=INTEGER}
    </select>
    
    <select id="findItemsById" parameterType="Integer" resultType="itemsCustom">
        SELECT * FROM ITEMS WHERE id= #{Items.id}
    </select>
    
    <update id="updateItems" parameterType="itemsCustom">
        update items set name=#{name},price=#{price},pic=#{pic},createtime=#{createtime},detail=#{detail} where id=#{id}
    </update>
</mapper>

——————————————————————————————

扩展类的mapper:ItemsMapperCustom

package com.myself.ssm.mapper;

import java.util.List;

import com.myself.ssm.po.ItemsCustom;
import com.myself.ssm.po.ItemsQueryVo;

public interface ItemsMapperCustom {
   
    //商品查询列表
    public List<ItemsCustom>  findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
———————————————————————————————

扩展类的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.myself.ssm.mapper.ItemsMapperCustom">

    <!-- 定义商品查询的sql片段,就是商品查询条件
    建议以单标查询为条件
     -->
    <sql id="querry_items_where">
     <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
     <!-- 商品查询条件通过ItemsQueryVo包装对象中itemsCustom属性传递 -->
        <if test="itemsCustom!=null">
            <if test="itemsCustom.name!=null  and itemsCustom.name!=''">
               and name LIKE '%${itemsCustom.name}%'
            </if>  
            <if test="itemsCustom.id!=null  ">
               and id=#{itemsCustom.id}
            </if>       
        </if>
    </sql>
      <!-- 商品列表查询 -->
      <!--parameterType传入包装对象
      resultType建议使用扩展对象
       -->
     <select id="findItemsList" parameterType="com.myself.ssm.po.ItemsQueryVo"
     resultType="com.myself.ssm.po.ItemsCustom">
          SELECT * FROM items
          <where>
              <include refid="querry_items_where"></include>
          </where>
     </se

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值