培训班的东西,内容比较多
————————————————————————————————————————————————————————————————————————
————————————————————————————————————————————————————————————————————
所需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