MyBatis---自动创建表
该项目基于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---自动创建表的更多相关文章
- SpringBoot+Mybatis 自动创建数据表(适用mysql)
Mybatis用了快两年了,在我手上的发展史大概是这样的 第一个阶段 利用Mybatis-Generator自动生成实体类.DAO接口和Mapping映射文件.那时候觉得这个特别好用,大概的过程是这样 ...
- Hibernate连接mysql数据库并自动创建表
天才第一步,雀氏纸尿裤,Hibernate第一步,连接数据库. Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个 ...
- Hibernate自动创建表
只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 <property name="hibernate.hbm2ddl.auto">update& ...
- Hibernate根据实体类自动创建表
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码<propert ...
- MyBatis动态创建表
转载请注明出处:https://www.cnblogs.com/Joanna-Yan/p/9187538.html 项目中业务需求的不同,有时候我们需要动态操作数据表(如:动态建表.操作表字段等).常 ...
- mysql 按照月份自动创建表,以年和月为表明,动态生成。
需求:mysql5.5 数据库,想要根据月份自动创建表,每个月创建一张表,需要数据库自动创建,并根据当前年和月动态生成表名称. 解决办法:1 连接数据库工具为Navicat 2 首先创建存储过程, ...
- hibernate自动创建表报表不存在
在hibernate.cfg.xml配置了<property name="hibernate.hbm2ddl.auto">update</property> ...
- sql自动创建表并复制数据
---------------自动创建表并复制数据sql,需要自己设置主键----------- select * into 新表 from 旧表
- Hibernate 自动创建表bug问题解决
我在hibernate.cfg.xml配置文件中添加了自动创建表的的属性:(这样当数据库中没有此表是,hibernate就会自动帮我们创建一张表) <property name="hb ...
- SQL Server ->> 自动创建表并从文件加载数据
这个存储过程自动创建表并从文件加载数据. 有一点需要说明的是Excel 12.0驱动是兼容了Excel 97-2003和Excel 2007两者格式的Excel文件. CREATE PROCEDURE ...
随机推荐
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
这个例子介绍了如何在Service中使用Notification,相关的类为NotifyingController和NotifyingService. 在Service中使用Notification的 ...
- 入坑Ubuntu手记-系统安装和简单配置
对于开发者而言,Linux的环境帮助是非常大的.同样的,Linux对很多Windows下的软件,尤其是游戏不支持,这也是一个非常重要的生产力的因素.嗯…我可能就是为了控制自己少玩游戏,直接上一个Ubu ...
- spring-mabatis整合的配置文件
1.spring.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- hdu-1892 See you~---二维树状数组运用
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 题目大意: 题目大意:有很多方格,每个方格对应的坐标为(I,J),刚开始时每个格子里有1本书, ...
- 机器学习实战之Logistic回归
Logistic回归一.概述 1. Logistic Regression 1.1 线性回归 1.2 Sigmoid函数 1.3 逻辑回归 1.4 LR 与线性回归的区别 2. LR的损失函数 3. ...
- c#无限循环
for( ; ; ) 最快的 while(true) while(1) ?好像也是不过就是扫到的 public bool a= true; 中断一个循环while(a) a=f ...
- 100 numpy exercises
100 numpy exercises A joint effort of the numpy community The goal is both to offer a quick referenc ...
- Linux 关于动态链接库以及静态链接库的一些概念
库有动态与静态两种,动态通常用.so为后缀,静态用.a为后缀.例如:libhello.so libhello.a 为了在同一系统中使用不同版本的库,可以在库文件名后加上版本号为后缀,例如: libhe ...
- 多重网格方法(Multigridmethod)
原文链接 多重网格方法是解微分方程的方法.这个方法的好处是在利用迭代法收敛结果的时候速度特别快.并且,不管是否对称,是否线性都无所谓.它的值要思想是在粗糙结果和精细结果之间插值. 前面介绍了Gauss ...
- js控制select选中显示不同表单内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...