经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Mybatis之typeAlias配置的3种方式小结
来源:jb51  时间:2022/1/17 16:41:06  对本文有异议

Mybatis typeAlias配置

1.定义别名

  1. <typeAliases>
  2. ? ? ?<typeAlias alias="User" type="cn.lxc.vo.User" />
  3. </typeAliases>

2.扫描包方式

  1. <typeAliases>
  2. ? ? ?<package name="cn.lxc.vo" />
  3. </typeAliases>

3.注解方式

  1. package cn.lxc.vo;
  2. import org.apache.ibatis.type.Alias;
  3. @Alias("User")
  4. public class User {
  5. private int id;
  6. private String name;
  7. private int age;
  8. public int getId() {
  9. return id;
  10. }
  11. public void setId(int id) {
  12. this.id = id;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public int getAge() {
  21. return age;
  22. }
  23. public void setAge(int age) {
  24. this.age = age;
  25. }
  26. }

springboot加载mybatis的typeAlias问题

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

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

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

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号