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();
- ? ? }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。