MyBatis框架(增删改)

来源:互联网 发布:sql developer导入dmp 编辑:程序博客网 时间:2024/05/17 03:08
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);  //模糊查询}

小配置:



大配置:

   

  测试: