SSM中的Mybatis的操作



一:整合日志含有log4j和logback
logback是log4j的升级版他性能提升较大,有些甚至达到10倍以上,占的内存更小,slf4j能很好的整合它,还有很多数不胜数的优势
  1.下载jar包,依赖:

         log4j
         log4j
         1.2.16
      
 2.log4j.properties配置文件
     ### direct log messages to stdout ###
     log4j.appender.stdout=org.apache.log4j.ConsoleAppender
     log4j.appender.stdout.Target=System.out
     log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
     log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

     ### direct messages to file mylog.log ###
     log4j.appender.file=org.apache.log4j.FileAppender
     log4j.appender.file.File=d:/log.txt
     log4j.appender.file.layout=org.apache.log4j.PatternLayout
     log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

     ### set log levels - for more verbose logging change 'info' to 'debug' ###

     log4j.logger.cn.dawn=debug, stdout,file
     ###log4j.rootLogger=debug, stdout,file###

    


3.logback
  1.jar包(所需要的依赖)
    
      org.slf4j
      slf4j-api
      1.7.21
    


ch.qos.logback
logback-core
1.2.3


ch.qos.logback
logback-classic
1.2.3
test
  2.logback.xml配置文件
复制代码
  
  
      
   
   
      
      
          
            %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]%logger{56}.%method:%L -%msg%n  
          
      
  
    
      
        logs/OALog.log  
          
          
            logs/archive/OALog_all_%d{yyyy-MM-dd}.%i.log.zip  
              
              
              
                100MB  
              
          
          
          
            %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]%logger{56}.%method:%L -%msg%n  
          
      
   
    
    

       //要改成自己项目的dao层全类名
        
        
          
    
      
    
  
复制代码
 
   

二:SQL的操作

         1: sql片段解决  * sql的查询语句列名太多很麻烦解决的方案:

                                                

                                     bookId,bookName,bookAuthor,bookPrice

                         
                         

 

 

2:模糊查询

  dao层:public List likeSelect(String bookName);

  

  小配置中:

                 

     
        bookId,bookName,bookAuthor,bookPrice
    

    
    
    
    

  测试类中:

             

 @Test
    public void t6likeSelect(){

        String path = "mybatis-config.xml";

        try {
            InputStream e = Resources.getResourceAsStream(path);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(e);
            SqlSession session = factory.openSession();

            IBookDAO mapper = session.getMapper(IBookDAO.class); session.getMapper(dao层接口.class)反射的方式获取到了dao层这个接口的实现类,所以就可以使用强类型的方式,直接通过点的方式点出来供你选择。例如mapper.findAll()
            List list = mapper.likeSelect("的");
            for (Book item:list) {
                System.out.println(item.getBookName());
            }


            session.close();
        } catch (IOException var9) {
            var9.printStackTrace();
        }
    }

  三:resultMap

 

在接口中的方法声明如下:public List findAllBook();

在小配置中如下:


    
        
        
    
    
    

他默认的自动映射行为是PARTIAL,部分的,也就是开启的,他也可以手动设置,关闭自动映射行为

设置方法是在大配置文件中

 

 

它的意思是关闭NONE,这个不用关,所以我就是简单的操作后就注释掉了

测试类中

@Test
    public void t1SelectAll(){
        String path = "mybatis-config.xml";

        try {
            InputStream e = Resources.getResourceAsStream(path);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory factory = builder.build(e);
            SqlSession session = factory.openSession();
            IBookDAO mapper = session.getMapper(IBookDAO.class);
            List list = mapper.findAllBook();
            for (Book item:list) {
                System.out.println(item.getBookName());
            }

            session.close();
        } catch (IOException var9) {
            var9.printStackTrace();
        }
    }

四:多条件查询

实体类:

 

public class Book {
    private Integer bookID;
    private String bookName;
    private String bookAuthor;
    private Integer bookPrice;

 }

 

接口中的方法

俩种形式,一种采用map,一种采用直接参数Index(索引的方式)来实现的多条件查询

 

//根据多条件查询map版
    public List findtrueBookMap(Map map);
    //根据多条件查询index版
    public List findtrueBookIndex(String bookName,Integer bookPrice);

 

小配置中

 

复制代码

    
    
    
复制代码

 

测试类中

 

复制代码
    ///多条件查询Index版
    @Test
    public void t4selectmoreIndex(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List books = mapper.findtrueBookIndex("心",40);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }

    ///多条件查询map版
    @Test
    public void t3selectmoreMap(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        Map map=new HashMap();
        map.put("bookName","心");
        map.put("bookPrice",40);
        List books = mapper.findtrueBookMap(map);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }
复制代码

 五:智能标签分为 where ,if ,choose ,foreach的array方式 ,foreach的list方式 ,foreach的list自定义类型方式

实体类

复制代码
public class Book {
    private Integer bookID;
    private String bookName;
    private String bookAuthor;
    private Integer bookPrice;

    public Book() {
    }

    public Integer getBookID() {
        return this.bookID;
    }

    public void setBookID(Integer bookID) {
        this.bookID = bookID;
    }

    public String getBookName() {
        return this.bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookAuthor() {
        return this.bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public Integer getBookPrice() {
        return this.bookPrice;
    }

    public void setBookPrice(Integer bookPrice) {
        this.bookPrice = bookPrice;
    }
}
复制代码

接口中的方法

 

复制代码
//智能标签where if
    public List findtrueBookByIf(String bookName,Integer bookPrice);
    //智能标签where choose
    public List findtrueBookByChoose(Integer bookPrice);
    //智能标签where foreach array
    public List findtrueBookByForeachArray(int [] array);
    //智能标签where foreach list
    public List findtrueBookByForeachList(List list);
    //智能标签where foreach list
    public List findtrueBookByForeachListBook(List list);
复制代码

 

小配置中

 

复制代码

    
    
    
    
    
    
    
    
    
复制代码

 

测试类中

 

复制代码
///智能标签where + foreach list Book自定义list 进行多条件查询
    @Test
    public void t9selectZhiNengByForeachListBook(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List list=new ArrayList();
        Book b1=new Book();
        b1.setBookID(1);
        Book b2=new Book();
        b2.setBookID(2);
        list.add(b1);
        list.add(b2);
        List books = mapper.findtrueBookByForeachListBook(list);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }


    ///智能标签where + foreach list 进行多条件查询
    @Test
    public void t8selectZhiNengByForeachList(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List list=new ArrayList();
        list.add(1);
        list.add(3);
        List books = mapper.findtrueBookByForeachList(list);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }


    ///智能标签where + foreach array 进行多条件查询
    @Test
    public void t7selectZhiNengByForeachArray(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        int[] array={1,3};
        List books = mapper.findtrueBookByForeachArray(array);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }



    ///智能标签where + choose进行多条件查询
    @Test
    public void t6selectZhiNengByChoose(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List books = mapper.findtrueBookByChoose(500);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }


    ///智能标签where + if 进行多条件查询
    @Test
    public void t5selectZhiNengByIf(){
        SqlSession session= MyBatisUtils.getSession();

        IBookDAO mapper = session.getMapper(IBookDAO.class);
        List books = mapper.findtrueBookByIf("心",40);
        for (Book items:books) {
            System.out.println(items.getBookName());
        }

        session.close();

    }

六:

转载于:https://www.cnblogs.com/shenjunxiu/p/8477526.html

你可能感兴趣的:(SSM中的Mybatis的操作)