一、概述
Mybatis官方提供逆向工厂,可以针对单表自动生成mybatis执行所需要的代码(如mapper.java、mapper.xml、pojo)。这里有很多方法可选:
1
这里我们建议使用第四种方式或者MyEclipse插件方式,最好是前者,因为不受开发工具影响。使用前者时需要下载一个工具包mybatis-generator-core-1.3.2.jar。
二、使用工具逆向生成相关代码
首先我们建立一个java工程(mybatis-generator),然后导入mybatis的相关jar包和上面提到的工具包。然后编写用于生成相关类的配置文件:
generatorConfig.xml
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
connectionURL="jdbc:mysql://localhost:3305/mybatis"
userId="root"
password="walp1314">
targetProject=".\src">
targetProject=".\src">
type="XMLMAPPER" targetProject=".\src">
编写一个工具类生成相关代码:
GeneratorSqlmap.java
package cn.itcast.ssm.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorSqlmap {
public void generator() throws Exception {
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("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);
}
public static void main(String[] args) {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
说明:这个类的是直接从官网上找到的,这里不比关心其实现原理。运行此类我们就可以发现在工程中生成了相关的类。
2
注意:一般我们都是单独建一个工程,专门用来逆向生成相关代码,然后将这些代码拷贝到项目工程中,这样比较安全,避免出现同名文件的覆盖问题。这里将代码拷贝到了工程spring-mybatis02。
三、使用生成的代码
测试ItemsMapper.java中的方法:
package cn.itcast.ssm.mapper;
import cn.itcast.ssm.pojo.Items;
import cn.itcast.ssm.pojo.ItemsExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ItemsMapper {
int countByExample(ItemsExample example);
int deleteByExample(ItemsExample example);
int deleteByPrimaryKey(Integer id);
int insert(Items record);
int insertSelective(Items record);
List selectByExampleWithBLOBs(ItemsExample example);
List selectByExample(ItemsExample example);
Items selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
int updateByPrimaryKeySelective(Items record);
int updateByPrimaryKeyWithBLOBs(Items record);
int updateByPrimaryKey(Items record);
}
测试方法ItemsMapperTest.java
package cn.itcast.ssm.mapper;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.ssm.pojo.Items;
import cn.itcast.ssm.pojo.ItemsExample;
public class ItemsMapperTest {
private ApplicationContext applicationContext;
private ItemsMapper itemsMapper;//这里我们使用的是spring的自动扫描后注入的
@Before
public void setUp()throws Exception{
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
}
//根据主键删除
@Test
public void testDeleteByPrimaryKey() {
}
@Test
public void testInsert() {
//构造Items对象
Items items = new Items();
items.setName("手机");
items.setPrice(1999f);
itemsMapper.insert(items);
}
//自定义条件查询
@Test
public void testSelectByExample() {
ItemsExample itemsExample = new ItemsExample();
//通过criteria构造查询条件
ItemsExample.Criteria criteria = itemsExample.createCriteria();
criteria.andNameEqualTo("台式机");//这就是我们添加的查询条件
//可能返回多天记录
List list = itemsMapper.selectByExample(itemsExample);
for(Items tmp : list){
System.out.println(tmp.getName());
}
}
//根据主键查询
@Test
public void testSelectByPrimaryKey() {
Items items = itemsMapper.selectByPrimaryKey(1);
System.out.println(items.getName());
}
//更新
@Test
public void testUpdateByPrimaryKey() {
//对所有字段进行更新,需要先查询出来再更新
Items items = itemsMapper.selectByPrimaryKey(1);
items.setName("水杯");
itemsMapper.updateByPrimaryKey(items);
//如果传入字段不为空才更新,一般用在批量更新中,不需要先查询再更新
//itemsMapper.updateByPrimaryKeySelective(items);
}
}
说明:这里我们是对相关方法的一个简要测试。其他内容还需要再研究。