mybatis 自动更新表结构 ,兼容通用tkmapper
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的更多相关文章
- 让EntityFramwork自动更新表结构
在项目开发中,难免会遇到数据库表结构变化的情况,手动去维护数据库是一件繁琐的事情.好在EntityFramwork为我们这些懒人提供了可供自动更新数据结构的机制,废话不多说,直接上代码: 首先创建一个 ...
- 利用powerDesigner15.1连接oracle数据库并自动生成表结构
利用powerDesigner15.1连接oracle数据库并自动生成表结构 参考:http://blog.csdn.net/qq_24531461/article/details/76713802 ...
- hibernate.hbm2ddl.auto=update不能自动生成表结构
在写上篇文章<spring整合springmvc和hibernate>的时候,曾遇到一个问题 INFO: Server startup in 8102 ms Hibernate: inse ...
- Spring4整合Hibernate5时不能自动生成表结构
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: Spring4整合Hibernate5时,不再使用hibernate.cfg.xml,将其内容整合到Spring配置文件中,启动后不能 ...
- Code First 下自动更新数据库结构(Automatic Migrations)
示例 Web.config <?xml version="1.0" encoding="utf-8"?> <configuration> ...
- 在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据)
原文:在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所 ...
- mybatis根据数据库表结构自动生成实体类,dao,mapper
首先, pom需要引入 <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifac ...
- Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构
本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metada ...
- Hibernate 自动更新表出错 More than one table found in namespace
报错:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table f ...
随机推荐
- 树莓派3b基于UbuntuMate下载中文输入法
输入命令:sudo apt-get install fcitx-sunpinyin,就可以下载中文输入法包了. 命令格式:sudo apt-get install package是下载安装包.apt- ...
- POJ 1776 Task Sequences(竞赛图构造哈密顿通路)
链接:http://poj.org/problem?id=1776 本文链接:http://www.cnblogs.com/Ash-ly/p/5458635.html 题意: 有一个机器要完成一个作业 ...
- Unity防破解 —— 重新编译mono
Unity4.x版本导出android包时,只能选择mono,无法使用il2cpp,这就造成了我们的程序集很容易被修改--很多朋友在发布项目时觉得即使代码暴露出去也没什么关系,只有项目火了才有 ...
- 超级素数(sprime) (BFS)
问题 G: 超级素数(sprime) 时间限制: 1 Sec 内存限制: 64 MB提交: 47 解决: 11[提交][状态][讨论版] 题目描述 超级素数是指一个素数,每去掉后面一个数字,总能保 ...
- ASP.NET Core 2.2 基础知识(八) 主机 (未完待续)
主机负责应用程序启动和生存期管理.共有两个主机 API : 1.Web 主机 : 适用于托管 Web 应用,基于 IWebHostBuilder ; 2.通用主机 : 适用于托管非 Web 应用. 基 ...
- oracle tablespace usage status
select a.tablespace_name, a.bytes / 1024 / 1024 "Sum MB", (a.bytes - b.bytes) / 1024 / 102 ...
- Android开机过程
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha Android开机过程 BootLoder引导,然后加载Linux内核. 0号进程ini ...
- 【点分治】【哈希表】bzoj2599 [IOI2011]Race
给nlog2n随便过的跪了,不得已弄了个哈希表伪装成nlogn(当然随便卡,好孩子不要学)…… 不过为啥哈希表的大小开小点就RE啊……?必须得超过数据范围一大截才行……谜 #include<cs ...
- Scala实战高手****第1课:大数据时代的“黄金”语言Scala
共计28课,每节课程在1个小时左右. 每天至少2个课程.预计在11.30号完成. ——————————————————
- Oracle Linux 7.3下载与 dtrace安装
https://docs.oracle.com/cd/E52668_01/E86280/html/section_edm_dvp_hz.html http://www.oracle.com/techn ...