zoukankan      html  css  js  c++  java
  • 作业1、2、3、4 2019/10/23

    使用注解模式:

    接口:

    package com.test;
    
    import com.pojo.Goods;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IgoodsMapper {
    
        /**
         * 需求: 通过商品名称 (模糊查询)、供应商(供应商id)、是否付款
         * 查询到 订单编码,商品名称,供应商名称,账单,金额,是否付款,创建时间
         */
        @Select("SELECT g.goodsCode,g.goodsName,p.provider_name,g.money, g.payState,g.timeStart FROM goods g,provider p WHERE g.goodsName LIKE concat('%',#{goods_Name},'%') AND p.provider_id = #{provider_id} AND g.payState = #{goods_payState} AND g.goods_id = p.goods_id")
        List<Goods> getGoodsList(Map<String,String> map);
    
    
    
    }
    IgoodsMapper

    测试类:

    package com.demo.test;
    
    import com.pojo.Demo;
    import com.pojo.Goods;
    import com.pojo.Grade;
    import com.test.DemoMapper;
    import com.test.IgoodsMapper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Test01 {
    
    
        @Test
        public void test03() throws IOException {
            //1、读取配置文件
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //2、创建SqlSessionFactory工厂
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(in);
            //3、创建 SqlSession对象
            SqlSession sqlSession = factory.openSession();
            //4、实现方法
            IgoodsMapper mapper = sqlSession.getMapper(IgoodsMapper.class);
            Map<String,String> map = new HashMap<String, String>();
            map.put("goods_Name","机");
            map.put("provider_id","10086");
            map.put("goods_payState","1");
            List<Goods> list = mapper.getGoodsList(map);
            System.out.println("---------------------------");
            for (Goods goods:list){
    
                System.out.println(goods.getGoodsCode()+" "+goods.getGoodsName()+" "+goods.getProvider_name()+
                        " "+goods.getMoney()+" "+
                        goods.getPayState()+" "+goods.getTimeStart());
            }
            System.out.println("------------------------");
            //5、释放资源
            sqlSession.close();
            in.close();
    
        }
    }
    test01.java

    ------------------------------------------------------------------------------------------------------------------------

    这里我以添加商品库存数量为例:

    goods_id为主键    goods_num为库存数量

    实现思路:

    1、为实体类添加 这两个属性

    接口代码:添加这些

        @Insert("UPDATE provider SET goods_num = goods_num+#{goods_num} WHERE goods_id = #{goods_id}")
        int addGoods_num(Map<String,Integer> map);

    测试类:

    package com.demo.test;
    
    import com.pojo.Demo;
    import com.pojo.Goods;
    import com.pojo.Grade;
    import com.test.DemoMapper;
    import com.test.IgoodsMapper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Test01 {
        public static void main(String[] args) throws IOException {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //2、创建SqlSessionFactory工厂
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(in);
            //3、创建 SqlSession对象
            SqlSession sqlSession = factory.openSession();
            //4、实现方法
            IgoodsMapper mapper = sqlSession.getMapper(IgoodsMapper.class);
    
            Map<String,Integer> map = new HashMap<String, Integer>();
            map.put("goods_id",1);
            map.put("goods_num",2);
            int i = sqlSession.insert("addGoods_num", map);
    
            if(i>0){
                sqlSession.commit();
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }
            //5、释放资源
            sqlSession.close();
            in.close();
    
        }
    
    }
    test01.java

    测试结果:

    添加前的的数据库数据

    :重点看 goods_ id = 1;

    goods_num是否添加 我们测试类写的2 

    变成8

    结果:

    ------------------------------------------------------------------------------------------------------------------------

    把provider里的供应商名称修改名称:

     把goods_id = 1的provider_name的小米修改成腾讯

    代码如下:

    接口添加代码:

     @Insert("UPDATE provider p SET p.provider_name = #{provider_name} WHERE goods_id = #{goods_id}")
        int update_provider_name(Map<String,String> map);

    测试类:

    package com.demo.test;
    
    import com.pojo.Demo;
    import com.pojo.Goods;
    import com.pojo.Grade;
    import com.test.DemoMapper;
    import com.test.IgoodsMapper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Test01 {
        public static void main(String[] args) throws IOException {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //2、创建SqlSessionFactory工厂
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(in);
            //3、创建 SqlSession对象
            SqlSession sqlSession = factory.openSession();
            //4、实现方法
            IgoodsMapper mapper = sqlSession.getMapper(IgoodsMapper.class);
    
            Map<String,String > map = new HashMap<String, String >();
            map.put("goods_id","1");
            map.put("provider_name","腾讯");
            int i = sqlSession.update("update_provider_name", map);
    
            if(i>0){
                sqlSession.commit();
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }
            //5、释放资源
            sqlSession.close();
            in.close();
    
        }
    
    }
    test01.java

    结果:

    --------------------------------------------------------------------------------------

    实现思路:

    使用goods_id主键

    效果:

     把goods_id =4;删除

    代码如下:

    IgoodsMapper接口

        @Delete("DELETE FROM provider WHERE goods_id = #{goods_id}")
        int delectBy_provider_id(Map<String ,Integer> map);

    测试类:

    test01
    package com.demo.test;
    
    import com.pojo.Demo;
    import com.pojo.Goods;
    import com.pojo.Grade;
    import com.test.DemoMapper;
    import com.test.IgoodsMapper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Test01 {
        public static void main(String[] args) throws IOException {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            //2、创建SqlSessionFactory工厂
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(in);
            //3、创建 SqlSession对象
            SqlSession sqlSession = factory.openSession();
            //4、实现方法
            IgoodsMapper mapper = sqlSession.getMapper(IgoodsMapper.class);
    
            Map<String,Integer > map = new HashMap<String, Integer>();
            map.put("goods_id",4);
            int i = sqlSession.delete("delectBy_provider_id", map);
    
            if(i>0){
                sqlSession.commit();
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
            //5、释放资源
            sqlSession.close();
            in.close();
    
        }
    
    }

  • 相关阅读:
    Selenium-Xpath使用方法
    HTML基础之js
    HTML基础—DOM操作
    CSS基础知识总结二
    CSS基础知识总结之css样式引用的三种方式
    BZOJ 2329/2209 [HNOI2011]括号修复 (splay)
    BZOJ 1576 [USACO]安全路经Travel (树剖+线段树)
    BZOJ 2402 陶陶的难题II (01分数规划+树剖+线段树+凸包+二分)
    BZOJ 4044 Virus synthesis (回文自动机+dp)
    BZOJ 2342 [SHOI2011]双倍回文 (回文自动机)
  • 原文地址:https://www.cnblogs.com/bichen-01/p/11724033.html
Copyright © 2011-2022 走看看