zoukankan      html  css  js  c++  java
  • Mybatis练习之查询、增加、

    1、创建功能接口

     1 public interface ProviderMapper {
     2 
     3     /**
     4      * 查询供货商的所有信息
     5      * @return
     6      */
     7     public List<Provider> getProviderList();
     8 
     9     /**
    10      * 增加供货商信息
    11      * @param provider 供货商实体类
    12      * @return
    13      */
    14     public int addProvide(Provider provider);
    15 
    16     /**
    17      * 根据供货商联系人模糊查询供应商信息
    18      * @param proContact 模糊查询的供货商姓名
    19      * @return
    20      */
    21     public List<Provider>  findProvideByproName(String proContact);
    22 }
    接口

    2、将接口在Mapper.xml中配置,Mapper.xml文件直接映射到对应的接口,调用接口方法会自动去Mapper.xml文件中找到对应的标签

     1 <mapper namespace="cn.kgc.Dao.ProviderMapper">
     2     <select id="proList" resultType="Provider">
     3         select * from smbms_provider
     4     </select>
     5     <select id="proCount" resultType="int">
     6         select count(1) from smbms_provider
     7     </select>
     8     <select id="getProviderList" resultType="cn.kgc.entity.Provider">
     9          select * from smbms_provider
    10     </select>
    11     <insert id="addProvide" parameterType="Provider">
    12         INSERT INTO smbms_provider (id,proCode,proName,proDesc,proContact,
    13         proPhone,proAddress,proFax,createdBy,creationDate,modifyDate,modifyBy)  
    14               VALUES   (#{id},#{proCode},#{proName},#{proDesc},  
    15                           #{proContact},#{proPhone},#{proAddress},
    16                           #{proFax},#{createdBy},#{creationDate},#{modifyDate},
    17                           #{modifyBy})  
    18     </insert>
    19     <select id="findProvideByproName" resultType="Provider" parameterType="String">
    20         select * from smbms_provider where proContact like CONCAT('%',#{proName},'%')
    21     </select>
    22 </mapper>
    Mapper.xml

    3、将Mapper.xml配置在mybatis-config.xml中

    1 <mappers>
    2         <mapper resource="cn/kgc/Dao/FinancingProductMapper.xml"></mapper>
    3         <mapper resource="cn/kgc/Dao/ProviderMapper.xml"></mapper>
    4     </mappers>
    Mapper.xml在配置文件中的配置

    4、编写工具类

     1 public class myBatisUntil {
     2     private static SqlSession sqlSession;
     3     static {
     4         String path = "mybatis-config.xml";
     5         try {
     6             InputStream is= Resources.getResourceAsStream(path);
     7             SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
     8             sqlSession = sqlSessionFactory.openSession();
     9         } catch (IOException e) {
    10             e.printStackTrace();
    11         }
    12     }
    13     public static SqlSession getSqlSession(){
    14         return sqlSession;
    15     }
    16 }
    获取sqlSession的工具类

    5、测试类

     1 /**
     2      * 查询供货商的所有信息
     3      */
     4     @Test
     5     public void test02(){
     6         SqlSession sqlSession = myBatisUntil.getSqlSession();
     7         List<Provider> providerList = sqlSession.getMapper(ProviderMapper.class).getProviderList();
     8         //List<Provider> providerList = sqlSession.selectList("cn.kgc.Dao.ProviderMapper.proList");
     9         for (Provider provider : providerList) {
    10             System.out.println(provider.getId()+"---"+provider.getProCode()+"---"
    11                     +provider.getProName()+"---"+provider.getProDesc()+"---"
    12                     +provider.getProContact()+"---"+provider.getProPhone()+"---"
    13                     +provider.getProAddress()+"---"+provider.getProFax()+"---"
    14                     +provider.getModifyBy()+"---"+provider.getCreationDate()
    15                     +"---"+provider.getModifyDate()+"---"+provider.getCreatedBy());
    16         }
    17     }
    18 
    19 
    20 /**
    21      * 增加供货商信息
    22      */
    23     @Test
    24     public void test04(){
    25         SqlSession sqlSession = myBatisUntil.getSqlSession();
    26         
    27         Provider provider = new Provider();
    28         provider.setId(16);
    29         provider.setProCode("ZJ_GYS002");
    30         provider.setProName("乐摆日用品厂");
    31         provider.setProDesc("长期合作伙伴,主营产品:各种中、高档塑料杯,塑料乐扣水杯(密封杯)、保鲜杯(保鲜盒)、广告杯、礼品杯");
    32         provider.setProContact("王佳欣");
    33         provider.setProPhone("18060975170");
    34         provider.setProAddress("湖里区");
    35         provider.setProFax("0579-34452321");
    36         provider.setCreatedBy(1);
    37         provider.setCreationDate("2019-10-21 10:01:30");
    38         int count = sqlSession.getMapper(ProviderMapper.class).addProvide(provider);
    39         System.out.println("添加:"+count);
    40     }
    41 
    42 
    43 /**
    44      * 根据供货商联系人模糊查询供应商信息
    45      */
    46     @Test
    47     public void test05() {
    48         SqlSession sqlSession = myBatisUntil.getSqlSession();
    49         String proContact = "王";
    50         List<Provider> providerList = sqlSession.getMapper(ProviderMapper.class).findProvideByproName(proContact);
    51         //List<Provider> providerList = sqlSession.selectList("cn.kgc.Dao.ProviderMapper.proList");
    52         for (Provider provider : providerList) {
    53             System.out.println(provider.getId() + "---" + provider.getProCode() + "---"
    54                     + provider.getProName() + "---" + provider.getProDesc() + "---"
    55                     + provider.getProContact() + "---" + provider.getProPhone() + "---"
    56                     + provider.getProAddress() + "---" + provider.getProFax() + "---"
    57                     + provider.getModifyBy() + "---" + provider.getCreationDate()
    58                     + "---" + provider.getModifyDate() + "---" + provider.getCreatedBy());
    59         }
    60     }
    测试类

    测试结果:

    查询供应商信息:

  • 相关阅读:
    【转载】设计模式_单例模式(学习)
    【转载】设计模式_抽象工厂模式(学习)
    【转载】设计模式_工厂方法(学习)
    【转载】设计模式_六大原则(学习)
    【转载】设计模式_简单工厂模式(学习)
    java有用的启动参数
    linux freetds操作mssql
    ansible 控制windows
    nginx 版本介绍
    tar 压缩去除目录
  • 原文地址:https://www.cnblogs.com/Dean-0/p/11717681.html
Copyright © 2011-2022 走看看