软件编程
位置:首页>> 软件编程>> java编程>> Mybatis之typeAlias配置的3种方式小结

Mybatis之typeAlias配置的3种方式小结

作者:如果屈原会编程  发布时间:2023-11-26 16:42:14 

标签:Mybatis,typeAlias,配置

Mybatis typeAlias配置

1.定义别名

<typeAliases>
     <typeAlias alias="User" type="cn.lxc.vo.User" />
</typeAliases>

2.扫描包方式

<typeAliases>
     <package name="cn.lxc.vo" />
</typeAliases>

3.注解方式

package cn.lxc.vo;
import org.apache.ibatis.type.Alias;
@Alias("User")
public class User {
   private int id;
   private String name;
   private int age;

public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
}

springboot加载mybatis的typeAlias问题

springboot打成jar之后再linux上运行会报找不到 type alias 对应的实体类的问题,这是springboot扫包的问题。

工程上默认使用的是Mybatis的DefaultVFS进行扫描,但是在springboot的环境下,Mybatis的DefaultVFS这个扫包会出现问题,所以只能修改VFS,

为了清晰可见,直接贴代码

@Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        logger.info("load SpringBootVFS");
        //DefaultVFS在获取jar上存在问题,使用springboot只能修改
        VFS.addImplClass(SpringBootVFS.class);
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources1 = resolver.getResources("classpath*:/mybatis/*.xml");
        Resource[] resources2 = resolver.getResources("classpath*:/mysql/mapper/*.xml");
        Resource[] resources = (Resource[]) ArrayUtils.addAll(resources1,resources2);
        sqlSessionFactoryBean.setMapperLocations(resources);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.xxx.xx.entity");
        return sqlSessionFactoryBean.getObject();
    }

来源:https://www.cnblogs.com/lxcmyf/p/6444120.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com