您的位置:首页 > 博客中心 > 数据库 >

数据库逆向框架代码生成工具:MyBatis Generator的使用

时间:2022-03-10 17:49

<build>         <finalName>mybatis_generator</finalName>         <plugins>             <plugin>                 <groupId>org.mybatis.generator</groupId>                 <artifactId>mybatis-generator-maven-plugin</artifactId>                 <version>1.3.0</version>             </plugin>         </plugins> </build>

<?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>     <!--数据库驱动jar -->     <classPathEntry location="E:\mysql-connector-java-5.1.7-bin.jar" />     <context id="DB2Tables" targetRuntime="Ibatis2Java5">         <!--去除注释 (true好像不起作用) -->         <commentGenerator>             <property name="suppressAllComments" value="true" />         </commentGenerator>         <!--数据库连接 -->         <jdbcConnection driverClass="com.mysql.jdbc.Driver"                         connectionURL="jdbc:mysql://localhost:3306/news"                         userId="root"                         password="">         </jdbcConnection>         <!--默认false            Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC.          -->         <javaTypeResolver >             <property name="forceBigDecimals" value="false" />         </javaTypeResolver>         <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建  使用Maven生成在target目录下,会自动创建) -->         <javaModelGenerator targetPackage="com.qianyan.model" targetProject="MAVEN">             <property name="enableSubPackages" value="false" />             <property name="trimStrings" value="true" />         </javaModelGenerator>         <!--生成SQLMAP文件 -->         <sqlMapGenerator targetPackage="com.qianyan.persistence.ibatis"  targetProject="MAVEN">             <property name="enableSubPackages" value="false" />         </sqlMapGenerator>         <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现  context id="DB2Tables" 修改targetRuntime="MyBatis3"  -->         <javaClientGenerator type="SPRING" targetPackage="com.qianyan.persistence.dao"  targetProject="MAVEN">             <property name="enableSubPackages" value="false" />         </javaClientGenerator>         <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等-->         <table tableName="USER" domainObjectName="User" >         </table>     </context> </generatorConfiguration>

 public int deleteUserApplyInfo(long user_id,long team_id){         StudyTeamUserApplyInfoExample ue = new StudyTeamUserApplyInfoExample();         ue.createCriteria()                 .andUserIdEqualTo(new BigDecimal(user_id))                 .andTeamIdEqualTo(new BigDecimal(team_id));         return studyTeamUserApplyInfoDAO.deleteByExample(ue);     }

public int updateStudyTeamInfo(StudyTeamInfo st){         StudyTeamInfoExample ste = new StudyTeamInfoExample();         ste.createCriteria().andTeamIdEqualTo(st.getTeamId());         return studyTeamInfoDAO.updateByExampleSelective(st,ste);     }

public List<StudyTeamInfo> getStudyTeamInfoByName(String team_name){         StudyTeamInfoExample se = new StudyTeamInfoExample();         se.createCriteria().andTeamNameLike("%"+team_name+"%").andEnableEqualTo((short)1);         se.setOrderByClause("team_score desc");         List<StudyTeamInfo> ls = studyTeamInfoDAO.selectByExample(se);         if(ls!=null&&ls.size()>0){             return ls;         }         return null;     }

public StudyTeamLevel getStudyTeamLevel(long score){
        StudyTeamLevelExample le = new StudyTeamLevelExample();
        le.createCriteria().andNeedScoreLessThanOrEqualTo(score).andUpScoreGreaterThan(score);
        List<StudyTeamLevel> ls = studyTeamLevelDAO.selectByExample(le);
        if(ls!=null&&ls.size()>0){
            return ls.get(0);
        }
        return null;
    }


public void saveUserStudyTeamApplyInfo(StudyTeamUserApplyInfo uai){         long ref = studyTeamInfoDAO.getSeqAllRef();         uai.setRef(new BigDecimal(ref));         studyTeamUserApplyInfoDAO.insertSelective(uai);     }

<!-- oracle 分页头 -->     <sql id="oracle_Pagination_Head" >         <dynamic >             <isNotEmpty property="first" >                 <isNotEmpty property="last" >                     <![CDATA[select * from ( select row_.*, rownum rownum_ from ( ]]>                 </isNotEmpty>             </isNotEmpty>         </dynamic>     </sql>     <!-- oracle 分页尾 -->     <sql id="oracle_Pagination_Tail" >         <dynamic >             <isNotEmpty property="first" >                 <isNotEmpty property="last" >                     <![CDATA[) row_ where rownum <= #last# ) where rownum_ > #first#]]>                 </isNotEmpty>             </isNotEmpty>         </dynamic>     </sql> <select id="findAllStudyTeamMessagePage" parameterClass="java.util.Map"    resultMap="ResultMapWithUserName">         <!-- 增加oracle分页头部 -->         <include refid="oracle_Pagination_Head" />         select a.*,b.user_name,b.photo_name, rownum rn,sysdate         from user_info b,study_team_user_message a         where  b.user_id=a.user_id         and type=0         <isNotEqual prepend="and" property="team_id" compareValue="0">             a.team_id=#team_id#         </isNotEqual>         order by a.ref desc         <include refid="oracle_Pagination_Tail" />     </select>

<!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等-->         <table tableName="article" domainObjectName="Article" >             <columnOverride column="content" jdbcType="VARCHAR" />         </table>

<context id="DB2Tables" targetRuntime="Ibatis2Java5">
http://mybatis.github.io/generator/configreference/javaClientGenerator.html如果使用 type=XMLMAPPER (xml直接实现dao)

需要targetRuntime is MyBatis3 而且不向下兼容


数据库逆向框架代码生成工具:MyBatis Generator的使用,布布扣,bubuko.com

热门排行

今日推荐

热门手游