MyBatis -generator应用

其实Mybatis-generator应用起来相当简单,尤其是在配好之后用起来更是神速啊。
首先下载mybatis-generator.jar包
下面是maven下载方式
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>

放到工程的lib目录下,这里有必要说下目录结构,我的是一个play的项目有,而我的目录结构是这样的

其他的如果是ssh或者其他项目根据自己的需求来更改

接下来新建个配置文件,名字随便,这里命名为generator.xml,配置文件的官方地址也贴一下 http://mybatis.github.io/generator/configreference/xmlconfig.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <classPathEntry location="lib/mysql-connector-java-5.1.33.jar" />

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://***.***.***.***:3306/***"
        userId="***"
        password="***">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 实体定义地址 -->
    <javaModelGenerator targetPackage="domain" targetProject="app">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- sql的配置文件定义 -->
    <sqlMapGenerator targetPackage="rpst"  targetProject="app">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- DAO定义 XMLMAPPER是xml的配置方式,还有ANNOTabelMapper的是注解方式 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="dao.mapper"  targetProject="app">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    
    <table tableName="try_user" domainObjectName="User"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    </table>


  </context>
</generatorConfiguration>

恩恩相当简单的文件和说明,如果有不懂可以去官网看看,虽然是英文,但是很好懂

ok,然后可以在工程目录那里cmd,输入

java -jar lib/mybatis-generator-core-1.3.2.jar -configfile app/rpst/generatorConfig.xml -overwrite

然后他就会自动生成实体类、ORM配置文件、Mapper到domain,rpst,dao.mapper下了。

OK

似乎缺点什么,每次都输命令行不太方便啊,哈哈,那就写个bat咯

放到工程目录下generator.bat

@echo on
rem
cd /d %~dp0
call java -jar lib/mybatis-generator-core-1.3.2.jar -configfile app/rpst/generatorConfig.xml -overwrite

然后在eclipse双击就可以更新了,多好。

关于Generator生成的代码的质量我保留意见,但是还是挺方便的。

对于Mapper,我原来将他理解为DAO,但是后来发现这样做有点不合理,所以我将DAO再抽出来一层

形成这样的目录结构。

UserDAO.java依旧是接口

package dao;
import domain.User;
public interface UserDAO {
    int deleteByPrimaryKey(Integer uId);
    int insert(User record);
    int insertSelective(User record);
    User selectByPrimaryKey(Integer uId);
    int updateByPrimaryKeySelective(User record);
    int updateByPrimaryKey(User record);
}

然后实现类这样写

package dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import dao.MyBatis;
import dao.UserDAO;
import dao.mapper.UserMapper;
import domain.User;
/**
 * 
 * @author ZZB
 * QQ:562800930
 * TEL:15914921094
 */
public class UserDAOImpl extends MyBatis implements UserDAO{
        
    @Override
    public int deleteByPrimaryKey(Integer uId) {
        SqlSession session = getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        return userMapper.deleteByPrimaryKey(uId);
    }

    @Override
    public int insert(User record) {
        SqlSession session = getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        SqlSession session = getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer uId) {
        SqlSession session = getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        return userMapper.selectByPrimaryKey(uId);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        SqlSession session = getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        SqlSession session = getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        return userMapper.updateByPrimaryKey(record);
    }

}
这样如果我换成其他的ORM框架的时候就可以很灵活了,只要改实现类就可以了。
posted @ 2014-11-10 22:36  改着名儿玩  阅读(8262)  评论(1编辑  收藏  举报