• mybits(2)增删改查


    CustomerMapper.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.kaibing.mapper.CustomerMapper">
        <!-- 根据ID查询 -->
         <select id="findCustomerById" parameterType="Integer" 
             resultType="com.kaibing.pojo.Customer">
         select * from t_customer where id = #{id}
         </select>
         
         <!-- 模糊查询 -->
         <select id="findCustomerByName" parameterType="String" 
             resultType="com.kaibing.pojo.Customer">
             select * from t_customer where username like '%${value}%'
         </select>
         
         <!-- 插入用户 -->
         <insert id="addCustomer" parameterType="com.kaibing.pojo.Customer">
             insert into t_customer(username,jobs,phone)
             values(#{username},#{jobs},#{phone})
         </insert>
         
         <!-- 更新 -->
         <update id="updateCustomer" parameterType="com.kaibing.pojo.Customer">
             update t_customer set 
             username=#{username},jobs=#{jobs},phone=#{phone}
             where id=#{id}
         </update>
         
         <!-- 删除 -->
         <delete id="deleteCustomer" parameterType="Integer">
             delete from t_customer where id=#{id}
         </delete>
    </mapper>

    MybitsTest.java

    package com.kaibing.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    
    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 com.kaibing.pojo.Customer;
    
    public class MybitsTest {
        
        public static void main(String[] args) {
            String resource = "mybits-config.xml";
    //        读取配置文件
            InputStream inputStream=null;
            try {
                inputStream = Resources.getResourceAsStream(resource);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
    //        构建配置文件工厂
            SqlSessionFactory sqlSessionFactory = 
                    new SqlSessionFactoryBuilder().build(inputStream);
    //        创建会话
            SqlSession sqlSession = sqlSessionFactory.openSession();
            
            
            
            
    //        查询ById
            Customer customer =  sqlSession.selectOne("com.kaibing.mapper.CustomerMapper"
                    + ".findCustomerById",1);
    //        输出结果
            System.out.println(customer.toString());
            
            
            
            
            
    //        查询like ByName
            List<Customer> customers =  sqlSession.selectList("com.kaibing.mapper.CustomerMapper"
                    + ".findCustomerByName","g");
    //        输出结果
            System.out.println("|||||||||||||||||||||||");
            for(Customer cust : customers) {
                System.out.println(cust);
            }
            
            
    //        Customer insertCustomer = new Customer();
    //        insertCustomer.setJobs("大傻");
    //        insertCustomer.setPhone("11111111");
    //        insertCustomer.setUsername("wanger");
    ////        插入
    //        Integer num =  sqlSession.insert("com.kaibing.mapper.CustomerMapper"
    //                + ".addCustomer",insertCustomer);
    ////        输出结果
    //        System.out.println("|||||||||||||||||||||||");
    //        System.out.println(num);
            
            Customer updateCustomer = new Customer();
            updateCustomer.setId(4);
            updateCustomer.setJobs("二傻");
            updateCustomer.setPhone("22222");
            updateCustomer.setUsername("wangsan");
    //        插入
            Integer update =  sqlSession.update("com.kaibing.mapper.CustomerMapper"
                    + ".updateCustomer",updateCustomer);
    //        输出结果
            System.out.println("|||||||||||||||||||||||--");
            System.out.println(update);
            
    //        插入
            Integer delete =  sqlSession.delete("com.kaibing.mapper.CustomerMapper"
                    + ".deleteCustomer",1);
    //        输出结果
            System.out.println("|||||||||||||||||||||||");
            System.out.println("delete"+delete);
            
    //        提交事务
            sqlSession.commit();
            
    //        关闭会话
            sqlSession.close();
        }
        
    
    }
  • 相关阅读:
    【ecshop】 完全清除版权信息
    【ecshop】使用sql 清除测试数据
    Java异常处理:给程序罩一层保险
    想清楚你究竟想成为什么样的人了吗?
    Java集合类的那点通俗的认知
    2019年的第一天,我给自己定了一份读书计划
    Java的内部类真的那么难以理解?
    29岁了还一事无成是人生的常态?
    Java接口的实例应用:致敬我的偶像——何塞·穆里尼奥
    程序员年底众生相
  • 原文地址:https://www.cnblogs.com/kaibing/p/9111024.html
Copyright © 2020-2023  润新知