mybatis整合Mapper通用工具,实现单表基本操作

                 

木秀于林,风必摧之

文档资料:https://github.com/abel533/Mapper


需要导入的maven依赖文件如下:


			tk.mybatis
			mapper
			3.2.2
		
		
		
			javax.persistence
			persistence-api
			1.0
		

      * 第二个是用于model注解的使用。



mybatis.xml配置文件内容如下:



	
	
		
		
		
	
	
	
		
		
			
				mappers=tk.mybatis.mapper.common.Mapper
			
		
	


mybatis-config.xml文件如下:




	
		
		
		
	
	
	
		
	
	
	
		
			
		
	


IBaseDao:

package com.citic.gatz.base;

import tk.mybatis.mapper.common.BaseMapper;

/**
 * 
 * @author fengchao
 * Mapper中通用的方法:
 * 		继承了base组合接口中的4个组合接口,包含完整的CRUD方法
 *
 * @param 
 */
public interface IBaseMapper extends BaseMapper {

}

以UserTest为例来说明:

sql:

CREATE TABLE `user_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=78 DEFAULT CHARSET=utf8;

model:

package com.citic.gatz.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
/**
 * model层测试类
 * @author fengchao
 *
 */
@Table(name="user_test")
public class UserTest implements Serializable {
	private static final long serialVersionUID = 6626527450093471276L;
	@Id  
    @Column(name = "id") 
	private Integer id;
	
	private String username;
	
	private Integer age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
}

          *若有的属性不需要和数据库挂钩,在使用@Transient进行注解标示




dao:

package com.citic.gatz.dao;

import java.util.List;

import com.citic.gatz.base.IBaseMapper;
import com.citic.gatz.model.UserTest;



public interface UserTestMapper extends IBaseMapper {
	/**
	 * 根据username查找对应的信息
	 * @param username
	 * @return
	 */
	List selectByUsername(String username);
}

   *其中里面有一个特点的业务的方法selectByUsername,只时候需要自己在Mapper文件里面完善。


mapper:




	
		
		
		
	
  
  	
        id,username,age
  	
	
	
	

测试类如下:

package com.dao.test;

import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.citic.gatz.dao.UserTestMapper;
import com.citic.gatz.model.UserTest;
import com.github.pagehelper.PageHelper;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations ={"classpath:beans.xml"})
public class UserTestDaoTest {
	@Autowired
	private UserTestMapper userTestMapper;
	
	@Test
	public void insert() {
		for(int i = 0; i < 10; i++) {
			UserTest obj = new UserTest();
			obj.setUsername("feng" + i);
			obj.setAge(24 + i);
			int insert = userTestMapper.insert(obj);
			System.out.println(insert);
		}
	}
	
	@Test
	public void delete() {
		int key = userTestMapper.deleteByPrimaryKey(8);
		System.out.println(key);
	}
	
	@Test
	public void update() {
		UserTest userTest = userTestMapper.selectByPrimaryKey(7);
		userTest.setUsername("saaa");
		
		//根据主键更新实体全部字段,null值会被更新
		int key = userTestMapper.updateByPrimaryKey(userTest);
		System.out.println(key);
	}
	
	@Test
	public void find() {
		PageHelper.startPage(2, 3);  //第二页 三条数据
		List list = userTestMapper.selectAll();
		for (UserTest userTest : list) {
			System.out.println(userTest.getUsername());
		}
	}
	
	@Test
	public void findByUsername() {
		List list = userTestMapper.selectByUsername("feng9");
		for (UserTest userTest : list) {
			System.out.println(userTest.getUsername());
		}
	}
}

                 除了最后一个方法是自己写的,其余的都是通过Mapper插件完成。 完毕!详细API还是查找文档······

你可能感兴趣的:(mybatis)