天天看點

Mybatis generator逆向工程Mybatis generator逆向工程

Mybatis generator逆向工程

生成檔案

項目結構

Mybatis generator逆向工程Mybatis generator逆向工程

所需jar包

Mybatis generator逆向工程Mybatis generator逆向工程

建立generatorConfig.xml

檔案中使用相對路經找不到生成檔案,我這使用的是絕對路經

<?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>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--資料庫連接配接的資訊:驅動類、連接配接位址、使用者名、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
                        password="123456">
        </jdbcConnection>

        <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true時把JDBC DECIMAL
            和 NUMERIC 類型解析為java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO類的位置 -->
        <javaModelGenerator targetPackage="com.xieli.liu.pojo"
                            targetProject="F:/JAVA23班第三組項目管理/JAVA23班第三組項目管理/newproject/newproject/src">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
            <!-- 從資料庫傳回的值被清理前後的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射檔案生成的位置 -->
        <sqlMapGenerator targetPackage="com.xieli.liu.mapper"
                         targetProject="F:/JAVA23班第三組項目管理/JAVA23班第三組項目管理/newproject/newproject/src">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.xieli.liu.mapper" targetProject="F:/JAVA23班第三組項目管理/JAVA23班第三組項目管理/newproject/newproject/src">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定資料庫表 -->
        <table tableName="user_info"></table>
    </context>
</generatorConfiguration>

           

生成類

package com.xieli.liu;


import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019-11-25.
 */
public class creact {
    // 執行main方法以生成代碼
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("F:/JAVA23班第三組項目管理/JAVA23班第三組項目管理/newproject/newproject/src/config/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 generator逆向工程Mybatis generator逆向工程

項目中使用生成檔案

将dao層和pojo層檔案拷貝到項目中

Mybatis generator逆向工程Mybatis generator逆向工程

測試類

package com.xieli.liu.test;

import com.xieli.liu.mapper.UserInfoMapper;
import com.xieli.liu.pojo.UserInfo;
import com.xieli.liu.pojo.UserInfoExample;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Created by Administrator on 2019-11-27
 */
public class test {
    private ApplicationContext applicationContext;
    UserInfoMapper userInfoMapper;
    @Before
    public void setup(){
        applicationContext=new ClassPathXmlApplicationContext("classpath:config/application-config.xml");
        userInfoMapper=(UserInfoMapper)applicationContext.getBean("userInfoMapper");
    }
    //根據主鍵查詢
    @Test
    public void selectByPrimaryKey(){
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(4);
        System.out.println(userInfo.getUserName());
    }
    //自定義條件查詢
    @Test
    public void selectByExample(){
        UserInfoExample userInfoExample = new UserInfoExample();
        //建構查詢條件
        UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
        //使用者名=maomao
        criteria.andUserNameEqualTo("maomao");
        List<UserInfo> userInfo =userInfoMapper.selectByExample(userInfoExample);
        System.out.println(userInfo.get(0).getNickName());
    }
    //添加資料
    @Test
    public void insert(){
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setUserName("小蘭");
        userInfo1.setUserPwd("66666666666");
        userInfoMapper.insert(userInfo1);
    }
    //添加資料
    @Test
    public void updateByPrimaryKey(){
        //更新所有字段,需要先查詢出來
        //修改ID為4的使用者名為小綠
     /*   UserInfo userInfo = userInfoMapper.selectByPrimaryKey(4);
        userInfo.setUserName("小綠");
        userInfoMapper.updateByPrimaryKey(userInfo);*/

        //更新傳入對象屬性不為null的字段,适用批量更新,不需要先查詢
        //修改ID為41的使用者名和密碼
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setUserId(41);
        userInfo1.setUserName("小白");
        userInfo1.setUserPwd("66666666666");
        userInfoMapper.updateByPrimaryKeySelective(userInfo1);
    }
}
           

繼續閱讀