mybatis---------统计总数

 

1.新建数据库,

2.新建lib,导入库,添加mybatis配置文件mybatis-config.xml




    
      
    
    
        
            
            
            
            
            
            
            
        
    
    
        
          
    

3.新建实体类Category,

 配置映射Category.xml,

 新建接口CategoryMapper

package com.how2java.pojo;

/**
 * @author lenovo
 */
public class Category {
   private int id;
   private String name;
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
}



   
       
           insert into mybatis.category_ ( name ) values (#{name})
       
       
       
           delete from mybatis.category_ where id= #{id}
       
       
       

       
           update mybatis.category_ set name=#{name} where id=#{id}
       
             
   

package com.how2java.mapper;
 
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.how2java.pojo.Category;
 
public interface CategoryMapper {
 
    @Insert(" insert into category_ ( name ) values (#{name}) ")  
    public int add(Category category);  
       
    @Delete(" delete from category_ where id= #{id} ")  
    public void delete(int id);  
       
    @Select("select * from category_ where id= #{id} ")  
    public Category get(int id);  
     
    @Update("update category_ set name=#{name} where id=#{id} ")  
    public int update(Category category);   
       
    @Select(" select * from category_ ")  
    public List list();
//    统计总数 就是一条普通的查询SQL语句
    @Select(" select count(*) from category_ ")  
    public int count();  

}

4.创建测试类:

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.how2java.pojo.Category;
/**
 * 添加100数
 * @author lenovo
 */
public class MybatisTest1 {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        List cs = session.selectList("listCategory");
        for (Category c : cs) {
            session.delete("deleteCategory", c);
        }
        for (int i = 0; i < 100; i++) {
            Category c = new Category();
            c.setName("category name " + i);
            session.insert("addCategory", c);
        }
        List cs2 = session.selectList("listCategory");
        for (Category c : cs2) {
            System.out.println(c.getName());
        }

        session.commit();
        session.close();

    }
}
import java.io.IOException;
import java.io.InputStream;

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.how2java.mapper.CategoryMapper;
  
/**
 * @author lenovo
 */
public class MybatisTest2 {
  
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        CategoryMapper mapper = session.getMapper(CategoryMapper.class);
 
        System.out.println(mapper.count());
        
        session.commit();
        session.close();
    }

}

 

MybatisTest2运行结果:输出100的原因是在MybatisTest1中删除了Categroy内容并添加了100个数据

100

 

你可能感兴趣的:(mybatis入门,查询)