MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。

一、建立表结构

CREATE TABLE `user` (
  `id` varchar(50) NOT NULL,
  `username` varchar(18) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(18) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `name` varchar(18) DEFAULT NULL,
  `sex` varchar(2) DEFAULT NULL,
  `birthday` varchar(50) DEFAULT NULL,
  `address` varchar(500) DEFAULT NULL,
  `tel` varchar(18) DEFAULT NULL,
  `qq` varchar(18) DEFAULT NULL,
  `image` varchar(50) DEFAULT NULL,
  `sfjh` varchar(1) DEFAULT NULL,
  `sfzx` varchar(1) DEFAULT NULL,
  `sfhf` varchar(1) DEFAULT NULL,
  `sfpl` varchar(1) DEFAULT NULL,
  `sffx` varchar(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;

二、下载mybatis-generator-core

进入:http://code.google.com/p/mybatis/

选择Downloads,再选择MyBatis Generator Tool下载即可。

三、生成配置文件

新建一个空的XML配置文件,名称可以随便取,这里以generatorConfig.xml为名。最好将这个文件放在下载后的lib目录中,如图:


其中mysql的驱动可以随便放在非中文路径的地方,这里为了方便就放在lib目录下。

自动生成最重要的就是配置文件的书写,现在就开始介绍generatorConfig.xml这个文件的具体内容:





	
	
		
			
			
			
		
		
		
		
		
			
		
		
		
			
			
		
		
		
			
		
		
		
			
		
		
		

1、其中需要注意的有数据库驱动、数据库URL、用户名、密码、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最后需要生成的表名和对应的类名。


四、运行

需要通过CMD命令行方式来运行,首先可以先准备一个运行的脚本,这里使用的脚本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

需要注意的是:mybatis-generator-core-1.3.2.jar为下载的对应版本的jar,generatorConfig.xml 为配置文件名,如果不为这个可以在这里进行修改。

启动cmd进入到“F:\soft\mybatis-generator-core-1.3.2\lib”这个目录下,如图:


生成成功后进到src目录下,可以看到已经生成了对应的model、dao、mapping,如图:


下面可以看看生成后的UserMapper.xml




  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  
  
    id, username, password, email, name, sex, birthday, address, tel, qq, image, sfjh, 
    sfzx, sfhf, sfpl, sffx
  
  
  
    delete from user
    where id = #{id,jdbcType=VARCHAR}
  
  
    insert into user (id, username, password, 
      email, name, sex, birthday, 
      address, tel, qq, image, 
      sfjh, sfzx, sfhf, sfpl, 
      sffx)
    values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{email,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR}, 
      #{address,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR}, 
      #{sfjh,jdbcType=VARCHAR}, #{sfzx,jdbcType=VARCHAR}, #{sfhf,jdbcType=VARCHAR}, #{sfpl,jdbcType=VARCHAR}, 
      #{sffx,jdbcType=VARCHAR})
  
  
    insert into user
    
      
        id,
      
      
        username,
      
      
        password,
      
      
        email,
      
      
        name,
      
      
        sex,
      
      
        birthday,
      
      
        address,
      
      
        tel,
      
      
        qq,
      
      
        image,
      
      
        sfjh,
      
      
        sfzx,
      
      
        sfhf,
      
      
        sfpl,
      
      
        sffx,
      
    
    
      
        #{id,jdbcType=VARCHAR},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{password,jdbcType=VARCHAR},
      
      
        #{email,jdbcType=VARCHAR},
      
      
        #{name,jdbcType=VARCHAR},
      
      
        #{sex,jdbcType=VARCHAR},
      
      
        #{birthday,jdbcType=VARCHAR},
      
      
        #{address,jdbcType=VARCHAR},
      
      
        #{tel,jdbcType=VARCHAR},
      
      
        #{qq,jdbcType=VARCHAR},
      
      
        #{image,jdbcType=VARCHAR},
      
      
        #{sfjh,jdbcType=VARCHAR},
      
      
        #{sfzx,jdbcType=VARCHAR},
      
      
        #{sfhf,jdbcType=VARCHAR},
      
      
        #{sfpl,jdbcType=VARCHAR},
      
      
        #{sffx,jdbcType=VARCHAR},
      
    
  
  
    update user
    
      
        username = #{username,jdbcType=VARCHAR},
      
      
        password = #{password,jdbcType=VARCHAR},
      
      
        email = #{email,jdbcType=VARCHAR},
      
      
        name = #{name,jdbcType=VARCHAR},
      
      
        sex = #{sex,jdbcType=VARCHAR},
      
      
        birthday = #{birthday,jdbcType=VARCHAR},
      
      
        address = #{address,jdbcType=VARCHAR},
      
      
        tel = #{tel,jdbcType=VARCHAR},
      
      
        qq = #{qq,jdbcType=VARCHAR},
      
      
        image = #{image,jdbcType=VARCHAR},
      
      
        sfjh = #{sfjh,jdbcType=VARCHAR},
      
      
        sfzx = #{sfzx,jdbcType=VARCHAR},
      
      
        sfhf = #{sfhf,jdbcType=VARCHAR},
      
      
        sfpl = #{sfpl,jdbcType=VARCHAR},
      
      
        sffx = #{sffx,jdbcType=VARCHAR},
      
    
    where id = #{id,jdbcType=VARCHAR}
  
  
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      name = #{name,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=VARCHAR},
      birthday = #{birthday,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      tel = #{tel,jdbcType=VARCHAR},
      qq = #{qq,jdbcType=VARCHAR},
      image = #{image,jdbcType=VARCHAR},
      sfjh = #{sfjh,jdbcType=VARCHAR},
      sfzx = #{sfzx,jdbcType=VARCHAR},
      sfhf = #{sfhf,jdbcType=VARCHAR},
      sfpl = #{sfpl,jdbcType=VARCHAR},
      sffx = #{sffx,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  

接下来就可以将这三个目录拷贝到对应项目的目录中,如果需要新增自己的方法可以修改dao类。

更多文章见:http://www.16boke.com/article/detail/108


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