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. serialVersionUID的作用(zz)

    http://www.cnblogs.com/guanghuiqq/archive/2012/07/18/2597036.html 简单来说,Java的序列化机制是通过在运行时判断类的serialVe ...

  2. Java Retry implement

    There are many cases in which you may wish to retry an operation a certain number of times. Examples ...

  3. Codeforces #442 Div2 F

    #442 Div2 F 题意 给出一些包含两种类型(a, b)问题的问题册,每本问题册有一些题目,每次查询某一区间,问有多少子区间中 a 问题的数量等于 b 问题的数量加 \(k\) . 分析 令包含 ...

  4. Eclipse 教程 | 菜鸟教程

    http://www.runoob.com/eclipse/eclipse-charset.html

  5. httpd2.4出现AH00025: configuration error

    log文件 安装的是包含ssl的版本,需要加载 LoadModule authz_core_module modules/mod_authz_core.so

  6. 【博弈论】【SG函数】poj2311 Cutting Game

    由于异或运算满足结合律,我们把当前状态的SG函数定义为 它所能切割成的所有纸片对的两两异或和之外的最小非负整数. #include<cstdio> #include<set> ...

  7. 【莫队算法】【权值分块】bzoj3339 Rmq Problem

    如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #def ...

  8. Exercise02_09

    import javax.swing.JOptionPane; public class Acceleration { public static void main(String[] args){ ...

  9. jeeplus中两个项目redis冲突问题

    修改端口号[两个项目使用不同的database]

  10. NSNotificationCenter监听TextField文字变化

    注册 1: NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", na ...