Mybatis(二)--Mapper代理开发

一、Mapper代理

传统开发方式跟数据连接使用dao接口,dao接口的实现类存在大量重复语句,且存在硬编码,如Mybatis(一)中的类似语句

sqlSession.delete("cn.cf.domain.User.deleteUserById", 3);

传入的变量因为sqlSession使用泛型,无法判断是否正确。因此对程序的开发会有些不利。

mapper代理

因此使用mapper代理改进。以下列子将使用mapper代理

二、mapper代理实例

2.1 环境搭建

Mybatis(二)--Mapper代理开发_第1张图片
2.2 实体类

package cn.cf.domain;

/**
 * Product的实体类
 * 
 * @author cf
 *
 */
public class Product {
	private int p_id;
	private String p_name;
	private Double p_price;
	private String p_category;

	public int getP_id() {
		return p_id;
	}

	public void setP_id(int p_id) {
		this.p_id = p_id;
	}

	public String getP_name() {
		return p_name;
	}

	public void setP_name(String p_name) {
		this.p_name = p_name;
	}

	public Double getP_price() {
		return p_price;
	}

	public void setP_price(Double p_price) {
		this.p_price = p_price;
	}

	public String getP_category() {
		return p_category;
	}

	public void setP_category(String p_category) {
		this.p_category = p_category;
	}

	@Override
	public String toString() {
		return "Product [p_id=" + p_id + ", p_name=" + p_name + ", p_price=" + p_price + ", p_category=" + p_category
				+ "]";
	}

}

2.3 mapper包的接口和配置

ProductMapper.java

package cn.cf.domain;

/**
 * Product的实体类
 * 
 * @author cf
 *
 */
public class Product {
	private int p_id;
	private String p_name;
	private Double p_price;
	private String p_category;

	public int getP_id() {
		return p_id;
	}

	public void setP_id(int p_id) {
		this.p_id = p_id;
	}

	public String getP_name() {
		return p_name;
	}

	public void setP_name(String p_name) {
		this.p_name = p_name;
	}

	public Double getP_price() {
		return p_price;
	}

	public void setP_price(Double p_price) {
		this.p_price = p_price;
	}

	public String getP_category() {
		return p_category;
	}

	public void setP_category(String p_category) {
		this.p_category = p_category;
	}

	@Override
	public String toString() {
		return "Product [p_id=" + p_id + ", p_name=" + p_name + ", p_price=" + p_price + ", p_category=" + p_category
				+ "]";
	}

}

productMapper.xml








	
	
		select p_id, p_name, p_price, p_category from products
	

	
	
	
	
	
	
	
	
		update products 
		set p_name = #{p_name} , p_price = #{p_price}, p_category = #{p_category}
		where p_id = #{p_id} 
	
	

2.4 sqlMapConfig.xml





	
	
		
	
	
 	
	
		
			
			
			
			
				
				
				
				
			
		
	
	
	
	
		
	
	

2.5 测试

TestDemo.java

package cn.cf.test;

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 org.junit.Before;
import org.junit.Test;

import cn.cf.domain.Product;
import cn.cf.mapper.ProductMapper;

public class TestDemo {
	private SqlSessionFactory sqlSessionFactory;
	
	@Before
	public void Before() throws Exception {
		String resource = "sqlMapConfig.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	
	//测试根据id查找
	@Test
	public void testfindById() {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
		Product product = productMapper.findById(1);
		System.out.println(product);
	}
	
	//测试查找全部
	@Test
	public void testfindAllProduct() {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
		List list = productMapper.findAllProduct();
		for (Product product : list) {
			System.out.println(product);
		}
	}
	
	//测试根据id修改
	@Test
	public void testupdateById() {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
		Product product = productMapper.findById(1);
		System.out.println(product);
		product.setP_name("红心火龙果");
		product.setP_price(30.5);
		System.out.println(product);
		productMapper.updateById(product);
		//修改提交事务
		sqlSession.commit();
	}
	
	
}	

以上都测试ok,不贴结果了。对数据库进行修改,记得要有提交事务的操作。

三、总结

使用mapper代理,代理对象是通过sqlSession的getMapper方法获取的。在上例中,dao层接口名相当于改为了ProductMapper,并有配置文件UserMapper.xml添加数据库操作语句。

因为相当于使用接口中的方法执行操作,传入参数和返回值的参数也有了设置。

你可能感兴趣的:(数据库)