MyBatis简单搭建

1、导入相应的jar包

2、导入数据库配置文件  jdbc.properties,直接写在MyBatis配置文件里也行,但是我不建议那么做。


username=root
password=root
url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=30
maxIdle=10
minIdle=5
maxWait=3000
#removeAbandoned=true
#removeAbandonedTimeout=180

#user=lihh
#password=lihh2013
#url=jdbc:oracle:thin:@192.168.0.26:1521:tarena
#driver=oracle.jdbc.OracleDriver

#user=sd1305
#password=sd1305
#url=jdbc:oracle:thin:@192.168.0.26:1521:tarena
#driver=oracle.jdbc.OracleDriver

#user=openlab
#password=open123
#url=jdbc:oracle:thin:@192.168.0.23:1521:tarena10g
#driver=oracle.jdbc.OracleDriver

3、导入MyBatis配置文件  SqlMapConfig.xml

  
  

	
	
    
        
            
            
                  
                  
                  
                  
            
        
    
    
    	
    

4、为了测试我们再建两个类

  • 实体类  Dept
public class Dept implements Serializable{
	private int deptno;
	private String dname;
	private String loc;
	public Dept() {}
	public Dept(int deptno, String dname, String loc) {
		super();
		this.deptno = deptno;
		this.dname = dname;
		this.loc = loc;
	}
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	@Override
	public String toString() {
		return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
	}
}
  • dao类  DeptDao
import java.util.List;
import com.oracle.bean.Dept;

public interface DeptDao {
	public void addDept(Dept dept);
	public void deleteByDeptno(int deptno);
	public void updateDept(Dept dept);
	public Dept findByDeptno(int deptno);
	public List findAll();
}

5、把DeptDao配置到Mybatis映射文件中  DeptMapper.xml




	
		insert into t_dept values(#{deptno},#{dname},#{loc})
	
	
		delete from t_dept where deptno = #{deptno}
	
	
		update t_dept set dname = #{dname}, loc = #{loc} where deptno = #{deptno}
	
	
	



namespace       ——Dao接口位置
insert 	        ——插入
delete	        ——删除
update 	        ——更新
select 	        ——查询
id	        ——接口中方法名
parameterType	——传入参数类型
resultMap	——结果集

6、上述步骤完成后就可以测试了。

import java.io.IOException;
import java.io.Reader;
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 com.oracle.bean.Dept;

public class TestSqlSession {
	@Test
	public void testSqlSession() throws IOException {
		// 读取配置文件
		Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
		// 创建SqlSession
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory factory = builder.build(reader);
		SqlSession session = factory.openSession();
		System.out.println(session);
		// 关闭SqlSession
		session.close();
	}
	@Test
	public void testAddDept() throws IOException {
		// 读取配置文件
		Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
		// 创建SqlSession
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory factory = builder.build(reader);
		SqlSession session = factory.openSession();
		
		Dept dept = new Dept(7, "后勤部", "意大利");
		session.insert("addDept", dept);
		session.commit();
		
		// 关闭SqlSession
		session.close();
	}
	@Test
	public void testDeleteDept() throws IOException {
		// 读取配置文件
		Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
		// 创建SqlSession
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory factory = builder.build(reader);
		SqlSession session = factory.openSession();
		
		session.delete("deleteByDeptno", 7);
		session.commit();
		
		// 关闭SqlSession
		session.close();
	}
	@Test
	public void testUpdateDept() throws IOException {
		// 读取配置文件
		Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
		// 创建SqlSession
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory factory = builder.build(reader);
		SqlSession session = factory.openSession();
		Dept dept = new Dept(6, "安保部", "索马里");
		session.update("updateDept", dept);
		session.commit();
		
		// 关闭SqlSession
		session.close();
	}
	@Test
	public void testSelectOne() throws IOException {
		// 读取配置文件
		Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
		// 创建SqlSession
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory factory = builder.build(reader);
		SqlSession session = factory.openSession();
		Dept dept = session.selectOne("findByDeptno", 6);
		System.out.println(dept);
		
		// 关闭SqlSession
		session.close();
	}
	@Test
	public void testSelectAll() throws IOException {
		// 读取配置文件
		Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
		// 创建SqlSession
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		SqlSessionFactory factory = builder.build(reader);
		SqlSession session = factory.openSession();
		List list = session.selectList("findAll");
		System.out.println(list);
		
		// 关闭SqlSession
		session.close();
	}
}
 
  

 

你可能感兴趣的:(随笔)