SpringMVC+Mybatis架构下的增删查改
来源:互联网 发布:北京美工培训 便宜 编辑:程序博客网 时间:2024/05/16 23:38
第一步新建一个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.jspreturn 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;@Servicepublic class ProductInfoServiceImpl implements ProductInfoService{//@Autowired//public ProductInfoMapper chanpin;@Overridepublic 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 {@Autowiredpublic ProductInfoService cp;@RequestMapping("/index")public ModelAndView index(HttpServletRequest req, HttpServletResponse res){ModelAndView mv = new ModelAndView();//mv初始化mv.setViewName("true");//设定访问页面为 true.jspList<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.jspreturn mv;//返回界面}
(2)在Service接口中新增一个方法
public int addProduct(ProductInfo c);
(3)在ServiceImpl中新增一个方法
@Overridepublic int addProduct(ProductInfo c) {// TODO Auto-generated method stubSystem.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.jspreturn mv;}
(2)在Service接口中新增一个方法
public void deleteProduct(String Name);//按产品名称删除产品
(3)在ServiceImpl中新增一个方法
@Overridepublic void deleteProduct(String Name) {// TODO Auto-generated method stubchanpin.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);//更新产品cModelAndView mv = new ModelAndView();//初始化System.out.println("==========");mv.setViewName("true");//设定访问页面为 true.jspreturn mv;//返回界面}
(2)在Service接口中新增一个方法
public void updateProduct(ProductInfo c);//更新产品c
(3)在ServiceImpl中新增一个方法
@Overridepublic void updateProduct(ProductInfo c) {// TODO Auto-generated method stubchanpin.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>
- SpringMVC+Mybatis架构下的增删查改
- SpringMVC+Mybatis架构下的增删查改,以家庭记账信息为例
- SpringMVC+Mybatis整合的增删改查
- SpringMVC+Mybatis整合的增删改查
- mybatis-springmvc+mybatis实现增删改查
- Springmvc+Spring+Mybatis+Maven简单的增删改查
- springmvc+mybatis+maven+junit实现网页的增删查改
- springmvc-mybatis的增删改查以及主键返回
- spring+springmvc+myBatis+jquery+ajax+jackson的增删改查
- spring+springmvc+myBatis+jquery+ajax+json的增删改查
- springmvc mybatis 增删改查示例
- mybatis的增删查改
- mybatis的增删查改
- Mybatis的增删改查
- mybatis的 增删改查
- mybatis的增删改查
- Mybatis 增删改查
- mybatis增删改查
- 群聊这么复杂,该怎么玩转?
- YCSB基础知识及HBase性能测试
- MySQL Schema设计(四)一个MySQL里的JQuery:common_schema
- Sublime Text3下配置SublimeLinter进行PHP代码检查
- [Leetcode] Invert Binary Tree
- SpringMVC+Mybatis架构下的增删查改
- linux远程挂载磁盘
- 第十一周项目1-验证算法(中序线索化二叉树)
- XMind 8 新版本,新的窗口视图工具
- PING ip 工具类
- 应用程序接口集成权威指南[电子书]
- Swiift3.0 UITextFiled键盘返回事件,点击其他区域,键盘收起,停止编辑;UITableViewCell取消选中行,以及系统默认的线的长短调整
- Android游戏开发属性
- 使用js进行string和json之间转换的方法