搭建一个简单的MyBatis框架

开发环境:Windows 10 企业版 2016 长期服务版,eclipse Oxygen.2 package,MySQL Community Server 5.5.61,Navicat for MySQL 11.1.13 企业版,Apache Tomcat 8.0。

  • 在eclipse中创建Maven项目,项目原型为quickstart模式,同时建立如下结构;搭建一个简单的MyBatis框架_第1张图片

  • 导入相应的Maven依赖,主要有Junit 4.12,mybatis 3.2.8,mysql 5.1.6;(注:以上版本均经过测试,使用其他版本测试时可能会出现原因不能的BUG);

             	
    			junit
    			junit
    			4.12
    			test
    		
    		
    			org.mybatis
    			mybatis
    			3.2.8
    		
    		
    			mysql
    			mysql-connector-java
    			5.1.6
    		

     

  • 在MySQL数据库中建立相应的表;(注:建表时尽量使用SQL语句建表);

  • 回到eclipse中的项目,在pojo包下建立相应的实体类,类中的属性名和属性类型尽量与数据库表中的字段名和字段类型保持一致,并创建的对应的set/get方法;

    public class Dept {
    	private Integer id;
    	private Integer deptno;
    	private String address;
    
    	public Dept(Integer id, Integer deptno, String address) {
    		super();
    		this.id = id;
    		this.deptno = deptno;
    		this.address = address;
    	}
    
    	public Dept() {
    		super();
    	}
    
    	public Integer getId() {
    		return id;
    	}
    
    	public void setId(Integer id) {
    		this.id = id;
    	}
    
    	public Integer getDeptno() {
    		return deptno;
    	}
    
    	public void setDeptno(Integer deptno) {
    		this.deptno = deptno;
    	}
    
    	public String getAddress() {
    		return address;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    	@Override
    	public String toString() {
    		return "员工编号:" + id + ", 部门编号:" + deptno + ",家庭地址:" + address;
    	}
    }

     

  • 在dao包下建立一个接口,以及一个名字和接口名一致的xml文件,切记这个接口不能写实现类;

    public interface DeptMapper {
    	public List find();
    }
    
    
    	
    

     

  • 配置src/resource/java目录下的dataSource.properties,该文件主要存放连接数据库需要的相关信息;

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/xxxx
    username=root
    password=xxxx

     

  • 配置src/resource/java目录下的mybatis-config.xml文件,该文件主要实现数据源的读取以及映射操作;

    
    
    	
    		
    	
    	
    		
    			
    			
    				
    				
    				
    				
    			
    		
    	
    	
    		
    	
    

     

  • 在util包下创建实体类,实现sqlsessionFactory的创建以及sqlsession的开启和关闭;

    public class Configuration {
    	public InputStream is;
    	public SqlSessionFactory ssf;
    	public SqlSession ss;
    
    	public SqlSession getsession() {
    		try {
    			is = Resources.getResourceAsStream("mybatis-config.xml");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		ssf = new SqlSessionFactoryBuilder().build(is);
    		ss = ssf.openSession();
    		return ss;
    	}
    
    	public void close() {
    		ss.close();
    	}
    }

     

  • 在service包下建立一个接口以及该接口的实现类,最终操作均为操作该实现类;

    public interface DeptService {
    	public List find();
    }
    public class DeptServiceImpl implements DeptService {
    	public InputStream is = null;
    	public SqlSessionFactory ssf = null;
    	public SqlSession ss = null;
    
    	public List find() {
    
    		return new Configuration().getsession().getMapper(DeptMapper.class).find();
    
    	}
    
    }

     

  • 在src/test/java目录下创建测试类,测试最终效果;

    public class JunitTest {
    
    	@Before
    	public void setUp() throws Exception {
    	}
    
    	@After
    	public void tearDown() throws Exception {
    	}
    
    	@Test
    	public void test() {
    		List list;
    		list = new DeptServiceImpl().find();
    		list.forEach(System.out::println);
    	}
    
    }

     

  • 该测试方法执行结构如下:(注:推荐使用Log4j打印日志)

 

 

 

你可能感兴趣的:(Java,mybatis)