1、maven引入jar

<dependency>
<groupId>com.github.gonglb.tools</groupId>
<artifactId>autoupdatetable-mybatis-tkmapper</artifactId>
<version>0.0.2</version>
</dependency>

2、mybatis开启驼峰转换

<settings>
<!--需开启驼峰下划线自动转换 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>

  

3、SqlSessionFactory配置

xml方式

<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 扫描model包,使用别名,需要增加com.github.gonglb.tools.autoupdatetable.model-->
<property name="typeAliasesPackage" value="com.xxxx.xxxx.*.model,com.github.gonglb.tools.autoupdatetable.model" />
<!-- 扫描sql配置文件:mapper接口需要的xml文件 -->
<property name="mapperLocations">
<list>
<value>classpath*:mapper/*.xml</value><!--需要扫描包内mapper-->
<value>classpath*:mapper/*/*.xml</value>
</list>
</property>
<!-- mybatis配置 -->
<property name="configLocation" value="classpath:mybatis-settings.xml" />
</bean>

  

//注解方式

@Bean("sqlSessionFactoryBean")
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-setting.xml"));
     //扫描保内的mapper
Resource[] resources = resolver.getResources("classpath*:mapper/*Mapper.xml");
sqlSessionFactoryBean.setMapperLocations(resources);      //增加扫描工具的model,com.github.gonglb.tools.autoupdatetable.model
sqlSessionFactoryBean.setTypeHandlersPackage("com.xxx.xxx.*.model,com.github.gonglb.tools.autoupdatetable.model");
return sqlSessionFactoryBean.getObject();
}

4、MapperScannerConfigurer配置

xml方式
<!-- mapper接口扫描 -->
<bean class="com.github.gonglb.tools.autoupdatetable.entrance.AutoTableTKMapperScannerConfigurer">
  <property name="basePackage" value="com.xxxx.xxxx.*.mapper" />
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.special.InsertListMapper
</value>
</property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

    <!--关键配置-->
<!--需要更新表结构的包路径 -->
<property name="packs" value="com.xxx.xxx.test.model"/>
<!-- 可选参数 create(所有表删除重新创建)、update(更新表)、none(不做操作) -->
<property name="tableAuto" value="update"/>
</bean>

  

//注解方式
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer() throws Exception {
AutoTableTKMapperScannerConfigurer autoTableTKMapperScannerConfigurer = new AutoTableTKMapperScannerConfigurer();
autoTableTKMapperScannerConfigurer.setBasePackage("com.xxx.xxx.*.mapper");
autoTableTKMapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
Properties properties = new Properties();
properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.special.InsertListMapper");
autoTableTKMapperScannerConfigurer.setProperties(properties);

//关键配置
autoTableTKMapperScannerConfigurer.setPacks("com.xxx.xxx.test.model");
autoTableTKMapperScannerConfigurer.setTableAuto("update");
return autoTableTKMapperScannerConfigurer;
}

5、model配置方式

import org.apache.ibatis.type.JdbcType;

import com.github.gonglb.tools.autoupdatetable.common.ColumnType;
@Table(name = "t_test")
public class Test{ @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "test_id",nullable = false,unique = false,length = 11,columnDefinition = "主键")
@ColumnType(jdbcType = JdbcType.INTEGER)
private Integer id;

  //不指定name默认自动转换为驼峰下划线模式
@Column(name = "test_name",nullable = true,unique = false,length = 64,columnDefinition = "注释",table="默认值")
@ColumnType(jdbcType = JdbcType.VARCHAR)
private String name; }

附带自动生成java model 和sql http://47.98.124.10:8088/love-web//html/java/java-model-generate.html

4、注意事项
a、该项目根据 mybatis-enhance-actable-0.0.1 开源项目的思路改编而来
b、该项目会自动删除更新表结构,根据实际情况使用

c、建议单独建立一个模块只负责更新表结构

其他信息mvn clean deploy -P release

mybatis 自动更新表结构 ,兼容通用tkmapper的更多相关文章

  1. 让EntityFramwork自动更新表结构

    在项目开发中,难免会遇到数据库表结构变化的情况,手动去维护数据库是一件繁琐的事情.好在EntityFramwork为我们这些懒人提供了可供自动更新数据结构的机制,废话不多说,直接上代码: 首先创建一个 ...

  2. 利用powerDesigner15.1连接oracle数据库并自动生成表结构

    利用powerDesigner15.1连接oracle数据库并自动生成表结构 参考:http://blog.csdn.net/qq_24531461/article/details/76713802 ...

  3. hibernate.hbm2ddl.auto=update不能自动生成表结构

    在写上篇文章<spring整合springmvc和hibernate>的时候,曾遇到一个问题 INFO: Server startup in 8102 ms Hibernate: inse ...

  4. Spring4整合Hibernate5时不能自动生成表结构

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: Spring4整合Hibernate5时,不再使用hibernate.cfg.xml,将其内容整合到Spring配置文件中,启动后不能 ...

  5. Code First 下自动更新数据库结构(Automatic Migrations)

    示例 Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> ...

  6. 在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据)

    原文:在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所 ...

  7. mybatis根据数据库表结构自动生成实体类,dao,mapper

    首先, pom需要引入 <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifac ...

  8. Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构

    本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metada ...

  9. Hibernate 自动更新表出错 More than one table found in namespace

    报错:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table f ...

随机推荐

  1. Vim常见配置与命令

    本文引自http://www.acczy.net/?p=301,在自己这里放一个以后方便查看 1. 基本安装 安装Vim,Windows系统中的主目录(类似于Linux的Home)中建立vimfile ...

  2. java类型强转

    知乎: 首先基本数据类型不是对象,强转改的是值,分为有损和无损,有损会丢失数据细节. 然后对象,只有继承关系的类才能强转,改变的只是引用,而且向上转型是安全的,把你转为人类是安全的,你还是你,只是现在 ...

  3. 关于 log4j.additivity的说明

    log4j.additivity是 子Logger 是否继承 父Logger 的 输出源(appender) 的标志位.具体说,默认情况下 子Logger 会继承 父Logger 的appender, ...

  4. frameset框架集

    frame使用注意事项: 1.frame不能脱离frameset单独使用 2.frame不能放在body标签中,不然不起效果. 3.frame的高度只能由frameset来决定. frameset:是 ...

  5. HDU 1556.Color the ball-差分数组-备忘

    备忘. 差分数组: 区间更新查询有很多方法,线段树.树状数组等都可以.如果为离线查询,就可以考虑使用差分数组. 假设对于区间[l,r]的每个数都加1,我们用一个数组a来记录,a[l]+=1;a[r+1 ...

  6. Oracle like 里面的通配符 以及regexp_like

    关于like后面的条件,Oracle提供了四种匹配模式: 1,% :表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FR ...

  7. 【费用流】bzoj1520 [POI2006]Szk-Schools

    注意:建图的时候,一定要把旧标号相同的分开. #include<cstdio> #include<algorithm> #include<cstring> #inc ...

  8. 【分块】【常数优化】【Orz faebdc】洛谷 P1083 NOIP2012提高组 借教室

    分块90分. By AutSky_JadeK [重点在下面] #include<cstdio> #include<cmath> using namespace std; #de ...

  9. [python]关于字符串查找和re正则表达式的效率对比

    最近需要在python中做大日志文件中做正则匹配 开始直接在for in 中每行做re.findall,后来发现,性能不行,就在re前面做一个基本的字符串包含判断 (str in str),如果不包含 ...

  10. Delphi~通过程序窗体句柄获取程序路径

    http://www.cnblogs.com/Jesses/articles/1636323.html 引用PsAPI var  h:HWND;  pid: Cardinal;  pHandle: T ...