MyBatis 逆向工程的使用

什么是逆向工程
mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po…)
配置文件generatorConfig.xml





	
		
			
			
		
		
		
		
		

		
		
			
		

		
		
			
			
			
			
            
           
		
        
		
			
			
		
		
		
			
			
		
		
		

执行生成程序

public class GeneratorSqlMapDemo01 {

public static void main(String[] args) throws Exception {
	List warnings = new ArrayList();
	boolean overwrite = true;
	File configFile = new File("src/generatorConfig.xml");
	ConfigurationParser cp = new ConfigurationParser(warnings);
	Configuration config = cp.parseConfiguration(configFile);
	DefaultShellCallback callback = new DefaultShellCallback(overwrite);
	MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
			callback, warnings);
	myBatisGenerator.generate(null);

}

}

根据数据库一张表完成逆向工程的例子
MyBatis 逆向工程的使用_第1张图片
逆向工程生成的student类

public class Student {
    private Integer id;

    private String name;

    private Integer age;

    private String sex;

    private String email;
    .............

对逆向工程生成的StudentMapper建立测试类进行测试

/**
 * 凡是带selective的方法的特点:遇到null字段忽略。
 * 
 * @author h
 *
 */
public class StudentMapperTest {

	private StudentMapper studentMapper;
	
	@Before
	public void setUp() throws Exception {
		SqlSession sqlSession=MyBatisUtils.getSqlSession();
		this.studentMapper=sqlSession.getMapper(StudentMapper.class);
		System.out.println("this.studentMapper="+this.studentMapper);
	}

	@Test
	public void testCountByExample() {
		StudentExample studentExample=new StudentExample();
		
		studentExample.setDistinct(true);
		studentExample.setOrderByClause("id desc");
		int count=this.studentMapper.countByExample(studentExample);
		System.out.println("count="+count);
	}

	@Test
	public void testDeleteByExample() {
		StudentExample example=new StudentExample();
		Criteria criteria=example.createCriteria();
		criteria.andNameEqualTo("小丽");
		int count=this.studentMapper.deleteByExample(example);
		System.out.println("count="+count);
	}

	@Test
	public void testDeleteByPrimaryKey() {
		int id=10;
		int count=this.studentMapper.deleteByPrimaryKey(id);
		System.out.println("count="+count);
	}

	@Test
	public void testInsert() {
		Student student=new Student();
		student.setName("小娥");
		int count=this.studentMapper.insert(student);
		System.out.println("count="+count);
	}

	@Test
	public void testInsertSelective() {
		Student student=new Student();
		student.setName("小静");
		int count=this.studentMapper.insertSelective(student);
		System.out.println("count="+count);
	}

	@Test
	public void testSelectByExample() {
		StudentExample example=new StudentExample();
		
		Criteria criteria=example.createCriteria();
		String condition="小";
		criteria.andNameLike("%"+condition+"%");
		criteria.andSexEqualTo("男");
		List list=this.studentMapper.selectByExample(example);
		System.out.println(list);
	}

	@Test
	public void testSelectByPrimaryKey() {
		int id=1;
		Student student=this.studentMapper.selectByPrimaryKey(id);
		System.out.println(student);
	}

	@Test
	public void testUpdateByExampleSelective() {
		int id=8;
		Student student=this.studentMapper.selectByPrimaryKey(id);
		if(student!=null){
			student.setName("丽儿");
			student.setAge(18);
			int count=this.studentMapper.updateByPrimaryKeySelective(student);
			System.out.println("count="+count);
		}else{
			System.out.println("学生不存在!");
		}
		
	}

	@Test
	public void testUpdateByExample() {
		int id=8;
		Student student=this.studentMapper.selectByPrimaryKey(id);
		if(student!=null){
			StudentExample studentExample=new StudentExample();
			Criteria criteria=studentExample.createCriteria();
			criteria.andNameEqualTo("丽儿");
			student.setName("妹儿");
			student.setAge(18);
			int count=this.studentMapper.updateByExample(student, studentExample);
			System.out.println("count="+count);
		}else{
			System.out.println("学生不存在!");
		}
	}

	@Test
	public void testUpdateByPrimaryKeySelective() {
		int id=8;
		Student student=this.studentMapper.selectByPrimaryKey(id);
		if(student!=null){
			student.setName("小美");
			int count=this.studentMapper.updateByPrimaryKeySelective(student);
			System.out.println("count="+count);
		}
		
	}

	@Test
	public void testUpdateByPrimaryKey() {
		int id=8;
		Student student=this.studentMapper.selectByPrimaryKey(id);
		if(student!=null){
			student.setName("妹儿");
			int count=this.studentMapper.updateByPrimaryKey(student);
			System.out.println("count="+count);
		}
	}

}

你可能感兴趣的:(框架)