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
