如何使用MyBaits实现数据库的数据全部查询和数据条数总和统计?

一 核心配置文件

二实体类

三数据库属性配置文件

四dao层sql语句映射

五测试类(以及工具类)

如何使用MyBaits实现数据库的数据全部查询和数据条数总和统计?_第1张图片


准备工作:配置config文件格式和mapper文件格式(复制到新建的xml文件第一行)或者进行myeclips设置自动生成



导入jar包

如何使用MyBaits实现数据库的数据全部查询和数据条数总和统计?_第2张图片

UserMapper.java

package cn.kgc.dao;

import java.util.List;

import cn.kgc.pojo.User;

public interface UserMapper {

	//查询用户的数据
	public List getUserList();
	
	//count
	public int count();
	
	
}

UserMapper.xml





	
	    
	    
	
	
	
	
	
	

User.java

package cn.kgc.pojo;

import java.util.Date;

public class User {
	
	private Integer id;//
	private String userCode;//用户编码
	private String userName;//用户密码
	private String userPassword;//密码
	private Integer gender;
	private Date birthday;
	private String phone;
	private String address;
	private Integer userRole;
	private Integer createdBy;
	private Date creationDate;
	private Integer modifyBy;
	private Date modifyDate;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUserCode() {
		return userCode;
	}
	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Integer getUserRole() {
		return userRole;
	}
	public void setUserRole(Integer userRole) {
		this.userRole = userRole;
	}
	public Integer getCreatedBy() {
		return createdBy;
	}
	public void setCreatedBy(Integer createdBy) {
		this.createdBy = createdBy;
	}
	public Date getCreationDate() {
		return creationDate;
	}
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	public Integer getModifyBy() {
		return modifyBy;
	}
	public void setModifyBy(Integer modifyBy) {
		this.modifyBy = modifyBy;
	}
	public Date getModifyDate() {
		return modifyDate;
	}
	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}
	
	
	
}

MapperTest.java

package cn.kgc.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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 org.junit.Test;

import cn.kgc.dao.UserMapper;
import cn.kgc.pojo.User;
import cn.kgc.utils.MyBatisUtil;

public class MapperTest {
	
	//没有使用将公共的逻辑代码写入工具类,最初的方法
	@Test
	public void test() {
	    String resource="mybatis-config.xml";
	    int count=0;
	    SqlSession sqlSession=null;
	    try {
	    	//1.获取mybatis-config.xml的输入流
			InputStream is=Resources.getResourceAsStream(resource);
			//2.建立SqlSessionFactory对象,来完成对配置文件的读取
			SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
			//3.建立sqlSession对象
			sqlSession=factory.openSession();
			count=sqlSession.selectOne("cn.kgc.dao.UserMapper.count");
			System.out.println(count);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
	}

	
	//将公共的逻辑代码写入工具类,调用工具类中的静态方法。selectOne()方法获得数据条数
	@Test
	public void test2() {
	   
	    int count=0;
	    SqlSession sqlSession=null;
	    try {
			sqlSession=MyBatisUtil.createSqlSession();
			count=sqlSession.selectOne("cn.kgc.dao.UserMapper.count");
			System.out.println(count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			MyBatisUtil.closeSqlSession(sqlSession);
		}
	}
	
	//将公共的逻辑代码写入工具类,调用工具类中的静态方法。selectList()方法获得所有数据条信息
	@Test
	public void test3() {
	   
	  List userList=new ArrayList();
	    SqlSession sqlSession=null;
	    try {
	    	//1
			sqlSession=MyBatisUtil.createSqlSession();
			//2
			userList=sqlSession.selectList("cn.kgc.dao.UserMapper.getUserList");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//3
			MyBatisUtil.closeSqlSession(sqlSession);
		}
	    
	    for(User user :userList){
	    	System.out.println(user.getUserCode()+"----"+user.getUserName());
	    }
	    
	    
	  
	}
	  
	   //基于接口的方式 
	//将公共的逻辑代码写入工具类,调用工具类中的静态方法。调用接口中的抽象方法getUserList()获得所有数据条信息
	    @Test
		public void test4() {
		   
		  List userList=new ArrayList();
		    SqlSession sqlSession=null;
		    try {
				sqlSession=MyBatisUtil.createSqlSession();
				userList=sqlSession.getMapper(UserMapper.class).getUserList();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				MyBatisUtil.closeSqlSession(sqlSession);
			}
		    
		    for(User user :userList){
		    	System.out.println(user.getUserCode()+"----"+user.getUserName());
		    }
	    }
}

MyBatisUtil.java

package cn.kgc.utils;

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;

public class MyBatisUtil {
	private static SqlSessionFactory factory;
	//
	static{
		
		InputStream is;
		try {
			is = Resources.getResourceAsStream("mybatis-config.xml");
			//2.建立SqlSessionFactory对象,来完成对配置文件的读取
			 factory=new SqlSessionFactoryBuilder().build(is);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static SqlSession createSqlSession(){
		return factory.openSession(false);
	}
	
	public static void closeSqlSession(SqlSession sqlSession){
		if(sqlSession!=null){
			sqlSession.close();
		}
	}

}

database.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms
user=root
password=root

mybatis-config.xml





    
   
    
    	
		
		
		    	
    
    
    
    	
    	
    
    
    
    	
    	
    	    
    	    
    	    
    	         
    	         
    	         
    	            	    
    	    
    	
    
    	
    
    
       
    





你可能感兴趣的:(MyBatis)