MyBatis框架(增删改)

MyBatis中如何去集成log4j
     步骤1:在pom.xml中添加一个log4j依赖
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    步骤2:在Resource目录中添加一个名称为log4j.property文件


   util:工具类层
 public class MyBatisUtil {
    //Mybatis
    static String path="Mybatis-config.xml";


    static SqlSessionFactory factory;
    static{
        try {
            InputStream is = Resources.getResourceAsStream(path);
        factory= new SqlSessionFactoryBuilder().build(is);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSession(){
        return factory.openSession();
    }
}


底层数据表中的列和实体类中的属性,只要有一个不匹配,MyBatis框架就不能自动的帮我们装配对象了。装配出来的是null




-------SQL片段:主要解决多个列重复书写的问题
<sql id="columns">
   stuid,stuname
</sql>


select <include refid="columns"></include>


---------多条件查询----------------------------------
select * from studentinfo where stuname like '%' #{stuName}  '%' and stuAge>#{stuAge}
-------》
 select * from studentinfo where stuname like '%' ? '%' and stuAge>? 
 
  select * from studentinfo where stuname like concat('%',#{stuName},'%') and stuAge>#{stuAge}
  ------>
 select * from studentinfo where stuname like concat('%',?,'%') and stuAge>? 
 
 select * from studentinfo where stuname like '%${stuName}%' and stuAge>#{stuAge}
 ------》
 select * from studentinfo where stuname like '%星星%' and stuAge>? 


StudentInfoDao接口:

public interface StudentINfoDao {
    public List<StudentINfo> shut();  //查询所有
    public   StudentINfo  stud(int  id);//根据id查询
    public  int  add(StudentINfo  stu);  //增加操作
    public  int  update(StudentINfo  stu); //修改操作
    public  int  delete(int stuid);  //删除操作
    public   List<StudentINfo> selectlink(StudentINfo info);  //模糊查询
}

小配置:



大配置:

   

  测试:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值