mybatis 中如何映射实体类和表名

有实体类Game,表tb_game,该如何映射。

如果有mybatis配置文件属性详细教程就更好了

    首先:最好把实体类里面的变量名称和表里面字段写成一致。

    然后:在SqlMapConfig.xml中
    <typeAliases>
    <typeAlias alias="game" type="实体类路径"/>
    </typeAliases>
    这就是把你的实体类写了个别名

    最后:在写查询添加的时候:
    <select id="gameDao" resultType="game">返回实体类对象
    select * from tb_game
    </select>
    这样查出的结果就对应上数据了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-20
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="game"> // namespace 可以随便定义
<typeAlias alias="game" type="../Game"/> // alias 可以随便定义 type 是你实体类Game的路径

<sql id="sql_list">
<![CDATA[
select * from tb_game // 这个地方是查询数据库,所以只能用表名,不能是实体类Game
]]>
</sql>

<select id="accountlist" resultClass="game"> // id 随便定义,resultClass是 你的实体类,具体是你 typeAlias 中定义的
<include refid="sql_list"/>
</select>
</sqlMap>
第2个回答  2011-12-29
首先:你最好把你的实体类里面的变量名称和你的表里面字段写成一致。
然后:在SqlMapConfig.xml中
<typeAliases>
<typeAlias alias="game" type="实体类路径"/>
</typeAliases>
这就是把你的实体类写了个别名
最后:在你写查询添加的时候:
<select id="gameDao" resultType="game">返回你的实体类对象
select * from tb_game
</select>
这样查出的结果就对应上你的数据了。本回答被提问者采纳
第3个回答  2012-01-01
import java.awt.*; import java.awt.event.*; import javax.swing.*; super("ATM柜员机"); Container container=getContentPane(); buttons=new
第4个回答  2019-12-13

使用代码生成器的方式。也有使用注解的方式,一看大体你就明白了。

@Test
   public void AutoCreate(){
String path="D://java_base/Errands";
      AutoGenerator mpg = new AutoGenerator();
      //MyBatis-Plus 全局策略配置
      GlobalConfig gc = new GlobalConfig();
      //生成文件的输出目录【默认 D 盘根目录】
      gc.setOutputDir(path+"/src/main/java");
      //是否覆盖已有文件
      gc.setFileOverride(false);
      //开启 ActiveRecord 模式
      gc.setActiveRecord(true);
      //是否在xml中添加二级缓存配置
      gc.setEnableCache(true);
      //开启 BaseResultMap
      gc.setBaseResultMap(true);
      //开启 baseColumnList
      gc.setBaseColumnList(true);
      // 开发人员
      gc.setAuthor("XXX");
      // 自定义文件命名,注意 %s 会自动填充表实体属性!
      gc.setMapperName("%sDao");
      gc.setXmlName("%sMapper");
      gc.setServiceName("%sService");
      gc.setServiceImplName("%sServiceImpl");
      gc.setControllerName("%sController");
      mpg.setGlobalConfig(gc);
      // 数据源配置
      DataSourceConfig dsc = new DataSourceConfig();
      dsc.setDbType(DbType.MYSQL);
      dsc.setDriverName("com.mysql.jdbc.Driver");
      dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
      dsc.setUsername("root");
      dsc.setPassword("root");
      mpg.setDataSource(dsc);
      // 策略配置
      StrategyConfig strategy = new StrategyConfig();
      strategy.setTablePrefix(new String[] { "t_m_", "t_p_", "t_s_", "t_r_", "t_c_"});// 此处可以修改为表的前缀
      strategy.setNaming(NamingStrategy.no_change);// 表名生成策略
      strategy.setInclude(new String[] {"te_test","userTest"}); // 需要生成的表
      // strategy.setExclude(new String[]{"test"}); // 排除生成的表
      // 公共父类
     // strategy.setSuperControllerClass("com.common.bean.pageObject");
      strategy.setSuperEntityClass("com.common.bean.pageObject");
      mpg.setStrategy(strategy);
      // 包配置
      PackageConfig pc = new PackageConfig();
      pc.setParent("com.errands");
//             pc.setModuleName("member");
      mpg.setPackageInfo(pc);
      
      String templatePath = "/templates/mapper.xml.ftl";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return path + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
      // 执行生成
      mpg.execute();
   }

相似回答