该项目基于Maven实现

该项目实现了在项目启动时,对数据库表进行操作

源码下载

实现步骤:

1.向pom.xml文件添加maven依赖

<dependency>
  <groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
  <artifactId>mybatis-enhance-actable</artifactId>
  <version>1.0.1</version>
</dependency>

2.在项目资源文件夹中创建autoCreateTable.properties(数据库表操作配置)文件

#create:系统启动后,会将所有的表删除掉,然后根据entity类中配置的结构重新建表,该操作会破坏原有数据。
#update:系统启动后,会根据entity类中配置的结构对表字段进行增删改操作
#------增:添加数据库表;根据实体向数据库表中添加字段
#------删:根据实体删除数据库表中的字段;不能实现删除项目实体类而删除数据库表
#------改:修改数据库字段的名字、属性
#none:系统不做任何处理
mybatis.table.auto=update
#用来配置要扫描的用于创建表的对象的包名
mybatis.model.pack=com.sunchenbin.store.model
#用来区别数据库,预计会支持这四种数据库mysql/oracle/sqlserver/postgresql,但目前仅支持mysql
mybatis.database.type=mysql

3.修改applicationContext.xml文件

添加扫描底层注入代码

<context:component-scan base-package="com.gitee.sunchenbin.mybatis.actable.manager.*" />

引入数据库操作配置文件autoCreateTable.properties

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
    <list>
      <value>classpath*:config/autoCreateTable.properties</value>
    </list>
  </property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
  <property name="properties" ref="configProperties" />
</bean>

添加mybatis.actable的mapper文件包,用于对数据库表进行操作。在正常项目中,已有该配置,即只需添加value值即可

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mapperLocations">
    <array>
      <value>classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml</value>
    </array>
  </property>
</bean>

添加mybatis.actable的dao包。在正常项目中,已有该配置,即只需在value值的最后添加;com.gitee.sunchenbin.mybatis.actable.dao.*即可

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gitee.sunchenbin.mybatis.actable.dao.*" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

特别注意:

项目引入配置文件不能使用如下方式

<beans profile="production">
  <context:property-placeholder ignore-unresolvable="true" file-encoding="utf-8"
  location="classpath:config.properties,classpath:jdbc.properties" />
</beans>

只能使用如下代码代替

<context:property-placeholder location="classpath:config.properties,classpath*:config/jdbc.properties" />

MyBatis---自动创建表的更多相关文章

  1. SpringBoot+Mybatis 自动创建数据表(适用mysql)

    Mybatis用了快两年了,在我手上的发展史大概是这样的 第一个阶段 利用Mybatis-Generator自动生成实体类.DAO接口和Mapping映射文件.那时候觉得这个特别好用,大概的过程是这样 ...

  2. Hibernate连接mysql数据库并自动创建表

    天才第一步,雀氏纸尿裤,Hibernate第一步,连接数据库. Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个 ...

  3. Hibernate自动创建表

    只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 <property name="hibernate.hbm2ddl.auto">update& ...

  4. Hibernate根据实体类自动创建表

    Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码<propert ...

  5. MyBatis动态创建表

    转载请注明出处:https://www.cnblogs.com/Joanna-Yan/p/9187538.html 项目中业务需求的不同,有时候我们需要动态操作数据表(如:动态建表.操作表字段等).常 ...

  6. mysql 按照月份自动创建表,以年和月为表明,动态生成。

    需求:mysql5.5 数据库,想要根据月份自动创建表,每个月创建一张表,需要数据库自动创建,并根据当前年和月动态生成表名称. 解决办法:1 连接数据库工具为Navicat  2  首先创建存储过程, ...

  7. hibernate自动创建表报表不存在

    在hibernate.cfg.xml配置了<property name="hibernate.hbm2ddl.auto">update</property> ...

  8. sql自动创建表并复制数据

    ---------------自动创建表并复制数据sql,需要自己设置主键----------- select * into 新表 from 旧表

  9. Hibernate 自动创建表bug问题解决

    我在hibernate.cfg.xml配置文件中添加了自动创建表的的属性:(这样当数据库中没有此表是,hibernate就会自动帮我们创建一张表) <property name="hb ...

  10. SQL Server ->> 自动创建表并从文件加载数据

    这个存储过程自动创建表并从文件加载数据. 有一点需要说明的是Excel 12.0驱动是兼容了Excel 97-2003和Excel 2007两者格式的Excel文件. CREATE PROCEDURE ...

随机推荐

  1. git 获取领先落后的命令

    git --git-dir=/data/usr/local/gerrit-site/git/aixuexi-admin.git rev-list --left-right --count master ...

  2. SourceTree Win10 安装过程及配置

    SourceTree 是一款拥有可视化界面的项目版本控制软件,适用于git项目管理,同时它集成了 git flow 工作流程,对于不熟悉 git 命令的初学者来说,可以通过 SourceTree 快速 ...

  3. Apache Module mod_ssl

    http://httpd.apache.org/docs/current/mod/mod_ssl.html Description: Strong cryptography using the Sec ...

  4. Altium_Designer-PCB的覆铜步骤

    1.覆铜的意义     覆铜,就是将PCB上闲置的空间作为基准面,然后用固体铜填充,这些铜区又称为灌铜.敷铜的意义在于,减小地线阻抗,提高抗干扰能力:降低压降,提高电源效率:还有,与地线相连,减小环路 ...

  5. Installing xgboost and cmake, mingw64 and mingw

    Problem: installing the xgboost to get the python package for later importing

  6. SQL:获取语句执行时间

    项目中查看数据库查询语句执行时间,脚本如下: --清除缓存 CHECKPOINT; DBCC DROPCLEANBUFFERS; DBCC FREEPROCCACHE; DBCC FREESYSTEM ...

  7. MySQL一致性非锁定读

    一致性非锁定读(consistent nonlocking read)是指InnoDB存储引擎通过多版本控制(multi versionning)的方式来读取当前执行时间数据库中行的数据,如果读取的行 ...

  8. 最长公共单词,类似LCS,(POJ2250)

    题目链接:http://poj.org/problem?id=2250 解题报告: 1.状态转移方程: ; i<=len1; i++) { ; j<=len2; j++) { dp[i][ ...

  9. 将一个命令的输出保存到CSV文件

    执行段: 结果段: 补充:配合不同的命令可以使工作更加简单 使用Imort-Csv命令从文件中导入结构化数据

  10. MySQL 主从同步 Slave_IO_Running: No

    MariaDB [chen]> show slave status \G *************************** 1. row ************************* ...