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. #!bin/sh是啥

    第一句的#!是对脚本的解释器程序路径,脚本的内容是由解释器解释的,我们可以用各种各样的解释器来写对应的脚本,比如说/bin/csh脚本,/bin/perl脚本,/bin/awk脚本,/bin/sed脚 ...

  2. cobbler自动重装

    如果物理机上想更换操作系统 yum -y install http://mirrors.163.com/centos/7/extras/x86_64/Packages/epel-release-7-9 ...

  3. Ubuntu 14.04 安装JDK 8

    1.安装JDK,参考 1.下载 JDK 8 从http://www.oracle.com/technetwork/java/javasebusiness/downloads/选择下载JDK的最新版本 ...

  4. 集训day15 t1 poj3728

    [问题描述] 有一颗n个节点的树 每个节点上都有许多奸商在卖东西,第i个奸商的理想价格为vi,即他会以vi的价格购买或卖出一件东西 有m个人希望从树上的某个点走到另一个点,问你在只进行一次买卖(每次仅 ...

  5. Codeforces #430 Div2 D

    #430 Div2 D 题意 给出一些数,每次操作先将所有数异或一个值,再求这些数中没有出现过的最小的非负整数. 分析 对于更新操作,对于 \(x\) 所有为 \(1\) 的位给相应层添加一个标记,当 ...

  6. 洛谷——P1495 曹冲养猪

    题目描述 自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲满不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数量,于是曹冲想狠狠耍曹操一把.举个例子,假如有 ...

  7. 微信小程序开发教程(八)视图层——.wxml详解

    框架的视图层由WXMKL(WeiXin Markup language)与WXSS(WeiXin Style Sheet)编写,由组件进行展示. 对于微信小程序而言,视图层就是所有.wxml文件与.w ...

  8. Spring中BeanFactory和ApplicationContext的区别

    1. BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责bean的生命周期. 2. ApplicationContext除了提供上述BeanF ...

  9. [LOJ6437]PKUSC

    旋转多边形是没有前途的,我们考虑旋转敌人,那么答案就是所有人的可行区间长度之和除以$2\pi$ 首先对每个敌人找到那些旋转后会落到多边形上的角度,实际上就是圆和一些线段求交,解方程即可,注意判一下落在 ...

  10. IO流--FileReader&&FileWriter

    (一)FileReader (1)第一种读取方式 package com.songyan.fileReader; import java.io.FileNotFoundException; impor ...