mybatis基本的增删改查实现

阅读更多

准备基本jar包

mybatis-3.0.5.jar,

mysql-connector-java-xxx.jar

 

表对象User,包含id,name,age,getset、toString方法。

 

mybatis配置文件


	

	

	
	
	
	    
		
	
	
	
	
		
			
			
				
				
				
				
			
		
	
	
		
	
   

 数据库配置文件db.properties

mysql_driver = com.mysql.jdbc.Driver
mysql_url = jdbc:mysql://127.0.0.1:3306/zrk_test
mysql_username = root
mysql_password =1234

access_driver = sun.jdbc.odbc.JdbcOdbcDriver
access_url = jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:/db.mdb
access_username = db
access_password =acms_123

 

 

User.xml mybatis的操作映射文件xml


  


  
 
 
 
  insert into t_zrk_user (id,name,age) values(#{id},#{name},#{age})
 
 
 
  insert into t_zrk_user (id,name,age) values
  
   (#{item.id},#{item.name},#{item.age})
  
 

在此处着重说明下批量更新操作。

       使用access做批量插入时,系统直接报错,不支持此批量操作,换到mysql时,就正常了。

 

UserMapper映射接口

package study.renk.dao;

import java.util.List;

import study.renk.pojo.User;


public interface UserMapper {
	User selectUser(String id);
	List selectByCondition(User user);
	void insertUser(User user);
	void insertBatchUser(List list);
}

 

 

 调用Main.java

package study.renk.domain;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 study.renk.dao.UserMapper;
import study.renk.pojo.User;

public class Main {
	private static final Log logger = LogFactory.getLog(Main.class);
	private UserMapper um;
	
	public UserMapper getUm() {
		return um;
	}

	public void setUm(UserMapper um) {
		this.um = um;
	}

	public static void main(String[] args) throws IOException {
		
		// 获取配置文件的输入流
		InputStream is = Resources.getResourceAsStream("mybatis.xml");
		// 获取我们的SqlSessionFactory(相当于Hibernate的SessionFactory);
		//SqlSessionFactoryBuilder有点类似于Hibernate的Configuration。
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
		// 获取一个Session
		SqlSession ss = ssf.openSession();
		UserMapper um = ss.getMapper(UserMapper.class);
//		User user = um.selectUser("2013073101");
//		System.out.println(user);
//		User condition = new User();
//		condition.setName("哈哈");
		long b_t = System.currentTimeMillis();
		List list = new ArrayList();
		for(int i=0;i<10000;i++){
			User user = new User();
			user.setId(1308010000+i+"");
			user.setName("Name"+(10000+i));
//			if(i == 3)
//				user.setAge(15+i+"aa");
//			else
				user.setAge(15+i+"");
			list.add(user);
			//10000条记录插入耗时:13819ms
//			um.insertUser(user);
		}
		//10000条记录插入耗时:2344ms
		um.insertBatchUser(list);
		System.out.println("插入用时:====>"+ (System.currentTimeMillis()-b_t)+"ms");
		try{
//下面的ss.select 此类,需要把user.xml中namespace修改为User
//			t.commit();
//			User user = (User)ss.selectOne("User.selectUser","2013072901");
//			System.out.println(user);
//			Map map = ss.selectMap("User.selectByCondition",condition,"id");
//			System.out.println(map.get("2013072901"));
//			List list = ss.selectList("User.selectByCondition",condition);
//			for(int i=0;i 
  

 

 

 

 

 

你可能感兴趣的:(Mybatis,helloworld)