SpringMVC+Mybatis架构下的增删查改

第一步新建一个Controller,以产品信息为例(ProductController)

package com.xcy.ctrl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/Product")
public class ProductController {
	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse res){
		ModelAndView mv = new ModelAndView();//mv初始化
		mv.setViewName("true");//设定访问页面为 true.jsp
		return mv;
	}
}

第二步建Service, 它只是一个接口,真正是通过ServiceImpl实现

**在public class productInfoServiceImpl implements ProductInfoService之前要有@Service进行注解

package com.xcy.service;

import java.util.List;

import com.xcy.bean.ProductInfo;


public interface ProductInfoService {
	public List<ProductInfo> getAllProduct();//获得所有产品信息列表

}
package com.xcy.service.impl;

import java.util.List;

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

import com.xcy.bean.ProductInfo;
import com.xcy.mapper.ProductInfoMapper;
import com.xcy.service.ProductInfoService;

@Service
public class ProductInfoServiceImpl implements ProductInfoService{
//	@Autowired
//	public ProductInfoMapper chanpin;
	@Override
	public List<ProductInfo> getAllProduct() {
		// TODO Auto-generated method stub
		//return chanpin.queryAll();
		System.out.println("getAllProduct");
		return null;
	}

}

第三步建一个Mapper,它也是一个接口,在 .xml文件中编辑相应的sql语句

package com.xcy.mapper;

import java.util.List;

import com.xcy.bean.ProductInfo;

public interface ProductInfoMapper {
	public List<ProductInfo> queryAll();

}
<?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.xcy.mapper.ProductInfoMapper">
    <select id="queryAll" parameterType="string" resultType="com.xcy.bean.ProductInfo">
        select * from Product
    </select>
</mapper>

之后要将Product.xml文件引入到mybatisConfig.xml文件中                                                          

<mapper resource="spring/mapping/Product.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>
    <settings>
        <setting name="jdbcTypeForNull" value="NULL"/>
    </settings>

    <plugins>
        <plugin interceptor="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
            <property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.OracleDialect"/>
        </plugin>
    </plugins>

    <mappers>
        <mapper resource="spring/mapping/Users.xml"/>
        <mapper resource="spring/mapping/Cost.xml"/>
        <mapper resource="spring/mapping/Student.xml"/>
	<mapper resource="spring/mapping/Category.xml"/>
	<mapper resource="spring/mapping/Product.xml"/>
    </mappers>
</configuration>

第四步 查询产品详细信息

package com.xcy.ctrl;

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.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xcy.bean.ProductInfo;
import com.xcy.service.ProductInfoService;

@Controller
@RequestMapping("/Product")
public class ProductController {
	
	@Autowired
	public ProductInfoService cp;
	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse res){
		ModelAndView mv = new ModelAndView();//mv初始化
		mv.setViewName("true");//设定访问页面为 true.jsp
		List<ProductInfo> list = cp.getAllProduct();//获得所有产品信息列表
			for(int i = 0; i< list.size() ;i++){
				ProductInfo c = list.get(i);
				System.out.println("Id:"+ c.getId() + 
						"  Name:" + c.getName() + " Spec:"+ c.getSpec()
						+ " Price:" + c.getPrice());
			}//输出每个产品的编号、名称、规格、价格
		
		return mv;
	}
}

第五步新增产品信息

(1)在Controller中增加一个@RequestMapping

@RequestMapping("/addProduct")
	public ModelAndView addProductInfo(HttpServletRequest req, HttpServletResponse res){
		String Id=req.getParameter("Id");//获得浏览器中设定的产品编号
		String  Name=req.getParameter("Name");//获得浏览器中设定的产品名称 
		String Spec = new String();
		int Price=0;
		ProductInfo c=new ProductInfo();
                c.setId(Id);//设置编号
                c.setName(Name);//设置名称
		c.setSpec(Spec);//设置规格
		c.setPrice(Price);//设置价格
		cp.addProduct(c);//新增产品
		ModelAndView mv = new ModelAndView();//初始化
		System.out.println("addProduct success");
		mv.setViewName("true");//设定访问见面为true.jsp
		return mv;//返回界面
}

(2)在Service接口中新增一个方法

public int addProduct(ProductInfo c);

(3)在ServiceImpl中新增一个方法

@Override
public int addProduct(ProductInfo c) {
// TODO Auto-generated method stub
System.out.println("addProduct");
chanpin.addProduct(c);
System.out.println("addProduct success");
return 0;
}

(4)在Mapper接口中新增一个方法

publicint addProduct(ProductInfoc);

(5)在.xml文件中中新增一个sql语句

<insert id="addProduct" parameterType="com.xcy.bean.ProductInfo">

<![CDATA[

        INSERT INTO product(Id,Name,Spec,Price)

                VALUES (#{Id}, #{Name}, #{Spec}, #{Price})

    ]]>

</insert>

第六步删除一条产品信息

(1)在Controller中增加一个@RequestMapping

@RequestMapping("/delete")
	
public ModelAndView deleteProductInfor(HttpServletRequest req, HttpServletResponse res){
		String Name = req.getParameter("Name");//获得浏览器中设定的产品名称
		cp.deleteProduct(Name);//删除浏览器中设定的产品名称的产品
		ModelAndView mv = new ModelAndView();
		System.out.println("==========");
		mv.setViewName("true");//设定访问见面为true.jsp
		return mv;
	}

(2)在Service接口中新增一个方法

public void deleteProduct(String Name);//按产品名称删除产品

(3)在ServiceImpl中新增一个方法

@Override
public void deleteProduct(String Name) {
		// TODO Auto-generated method stub
		chanpin.deleteByName(Name);
	}

(4)在Mapper接口中新增一个方法

public void deleteByName(String Name);

(5)在.xml文件中中新增一个sql语句

<delete id="deleteByName" parameterType="string" >
        delete from product where Name = #{Name}
</delete>

第七步更新一条产品信息

(1)在Controller中增加一个@RequestMapping

@RequestMapping("/Update")	
public ModelAndView updateProductInfor(HttpServletRequest req, HttpServletResponse res){
		String Id=req.getParameter("Id");//获得浏览器中设定的产品编号
		String Name = req.getParameter("Name");//获得浏览器中设定的产品名称
		String Spec = new String();
		int Price=0;
		ProductInfo c=new ProductInfo();
		c.setId(Id);//设置编号
		c.setName(Name);//设置名称
		c.setSpec(Spec);//设置规格
		c.setPrice(Price);//设置价格
		cp.updateProduct(c);//更新产品c
		ModelAndView mv = new ModelAndView();//初始化
		System.out.println("==========");
		mv.setViewName("true");//设定访问页面为 true.jsp
		return mv;//返回界面
	}

(2)在Service接口中新增一个方法

public void updateProduct(ProductInfo c);//更新产品c

(3)在ServiceImpl中新增一个方法

@Override
public void updateProduct(ProductInfo c) {
		// TODO Auto-generated method stub
		chanpin.update(c);
	}

(4)在Mapper接口中新增一个方法

public void update(ProductInfo c);

(5)在.xml文件中中新增一个sql语句

<!-- 更新一条记录 -->  
 <update id="update" parameterType="com.xcy.bean.ProductInfo">  
        update product
       	<trim prefix="set" suffixOverrides=",">
            <if test="Name != null">
                Name = #{Name},
            </if>
            <if test="Spec != null">
                Spec=#{Spec},
            </if>
            <if test="Price != null">
                Price = #{Price},
            </if>
        </trim>
        <trim prefix="where" prefixOverrides="and">
            <if test="Id != null">
                and Id = #{Id}
            </if>
        </trim>     
    </update>  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值