电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

springboot+mybatis通過(guò)實(shí)體類(lèi)自動(dòng)生成數(shù)據(jù)庫(kù)表的方法

瀏覽:48日期:2023-05-06 18:22:31

前言

本章介紹使用mybatis結(jié)合mysql數(shù)據(jù)庫(kù)自動(dòng)根據(jù)實(shí)體類(lèi)生成相關(guān)的數(shù)據(jù)庫(kù)表。

首先引入相關(guān)的pom包我這里使用的是springboot2.1.8.RELEASE的版本

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.0</version></dependency><dependency><groupId>com.gitee.sunchenbin.mybatis.actable</groupId><artifactId>mybatis-enhance-actable</artifactId><version>1.0.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!--以下兩個(gè)類(lèi)需要加入,否則報(bào)錯(cuò)無(wú)法注入--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier><exclusions><exclusion><artifactId>commons-logging</artifactId><groupId>commons-logging</groupId></exclusion></exclusions></dependency>

添加數(shù)據(jù)庫(kù)配置文件application.propertiesapplication.properties這里是單獨(dú)配置mybatis自動(dòng)建表的相關(guān)信息。

mybatis.table.auto=updatemybatis.model.pack=com.xxx.xxx.entity//實(shí)體類(lèi)的路徑mybatis.database.type=mysql

mybatis.table.auto=

create:每次加載hibernate會(huì)自動(dòng)創(chuàng)建表,以后啟動(dòng)會(huì)覆蓋之前的表,所以這個(gè)值基本不用,嚴(yán)重會(huì)導(dǎo)致的數(shù)據(jù)的丟失。

create-drop :每次加載hibernate時(shí)根據(jù)model類(lèi)生成表,但是sessionFactory一關(guān)閉,表就自動(dòng)刪除,下一次啟動(dòng)會(huì)重新創(chuàng)建。

update:加載hibernate時(shí)根據(jù)實(shí)體類(lèi)model創(chuàng)建數(shù)據(jù)庫(kù)表,這是表名的依據(jù)是@Entity注解的值或者@Table注解的值,sessionFactory關(guān)閉表不會(huì)刪除,且下一次啟動(dòng)會(huì)根據(jù)實(shí)體。

model:更新結(jié)構(gòu)或者有新的實(shí)體類(lèi)會(huì)創(chuàng)建新的表。

validate:?jiǎn)?dòng)時(shí)驗(yàn)證表的結(jié)構(gòu),不會(huì)創(chuàng)建表 none:?jiǎn)?dòng)時(shí)不做任何操作

mybatis.model.pack=com.xxx.xxx.entity//你實(shí)體類(lèi)的路徑

個(gè)人項(xiàng)目配置文件,非統(tǒng)一,根據(jù)項(xiàng)目需求配置

springboot+mybatis通過(guò)實(shí)體類(lèi)自動(dòng)生成數(shù)據(jù)庫(kù)表的方法

進(jìn)行生成數(shù)據(jù)庫(kù)表相關(guān)配置

TestConfig配置文件

import com.alibaba.druid.pool.DruidDataSource;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.beans.factory.annotation.Value;import org.springframework.beans.factory.config.PropertiesFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;@Configuration@ComponentScan(basePackages = {'com.gitee.sunchenbin.mybatis.actable.manager.*'})//固定的包public class TestConfig {//連接數(shù)據(jù)庫(kù)配置文件的地址,具體查閱配置文件的結(jié)構(gòu) @Value('${spring.datasource.druid.driver-class-name}') private String driver;//連接數(shù)據(jù)庫(kù)配置文件的地址,具體查閱配置文件的結(jié)構(gòu) @Value('${spring.datasource.druid.url}') private String url;//連接數(shù)據(jù)庫(kù)配置文件的地址,具體查閱配置文件的結(jié)構(gòu) @Value('${spring.datasource.druid.username}') private String username;//連接數(shù)據(jù)庫(kù)配置文件的地址,具體查閱配置文件的結(jié)構(gòu) @Value('${spring.datasource.druid.password}') private String password; @Bean public PropertiesFactoryBean configProperties() throws Exception{ PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); propertiesFactoryBean.setLocations(resolver.getResources('classpath*:application.properties'));//classpath*:application.properties是mybatis的生成表配置文件 return propertiesFactoryBean; } @Bean public DruidDataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setMaxActive(30); dataSource.setInitialSize(10); dataSource.setValidationQuery('SELECT 1'); dataSource.setTestOnBorrow(true); return dataSource; } @Bean public DataSourceTransactionManager dataSourceTransactionManager() { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource()); return dataSourceTransactionManager; } @Bean public SqlSessionFactoryBean sqlSessionFactory() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources('classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml')); sqlSessionFactoryBean.setTypeAliasesPackage('com.xxx.xxx.entity.*'); //上述classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml固定的包路徑 //com.xxx.xxx.entity.*替換成你的實(shí)體類(lèi)地址 return sqlSessionFactoryBean; }}

MyBatisMapperScannerConfig配置文件

import org.mybatis.spring.mapper.MapperScannerConfigurer;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@AutoConfigureAfter(TestConfig.class)//上面第一點(diǎn)配置文件類(lèi)public class MyBatisMapperScannerConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{ MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage('com.xxx.xxx.mapper.*;com.gitee.sunchenbin.mybatis.actable.dao.*'); mapperScannerConfigurer.setSqlSessionFactoryBeanName('sqlSessionFactory'); //com.xxx.xxx.mapper.*替換成你的mapper地址 //com.gitee.sunchenbin.mybatis.actable.dao.*固定的包 return mapperScannerConfigurer; }}

新建實(shí)體進(jìn)行測(cè)試

注:@Table(name = “”)及@Column(name = “id”)注解使用,實(shí)體類(lèi)繼承BaseModel。

import com.gitee.sunchenbin.mybatis.actable.annotation.Column;import com.gitee.sunchenbin.mybatis.actable.annotation.Table;import com.gitee.sunchenbin.mybatis.actable.command.BaseModel;import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;@Table(name = 'em_t')//新建表數(shù)據(jù)庫(kù)表名public class EmpAttr extends BaseModel{ private static final long serialVersionUID = 5199244153134426433L; @Column(name = 'id',type = MySqlTypeConstant.INT,length = 11,isKey = true,isAutoIncrement = true) private String id; @Column(name='ename',type= MySqlTypeConstant.VARCHAR) private String ename; @Column(name='sal',type= MySqlTypeConstant.VARCHAR) private String sal; @Column(name='job',type= MySqlTypeConstant.VARCHAR) private String job; //...省略get,set方法}

運(yùn)行項(xiàng)目

會(huì)控制臺(tái)會(huì)顯示說(shuō)新建表完成2020-07-08 11:02:13.895 INFO 48536 — [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 開(kāi)始創(chuàng)建表:em_t2020-07-08 11:02:13.983 INFO 48536 — [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 完成創(chuàng)建表:em_t

. ____ _ __ _ _ / / ___’_ __ _ _(_)_ __ __ _ ( ( )___ | ’_ | ’_| | ’_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ’ |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE)2020-07-08 11:02:11.264 INFO 48536 --- [ main] com.qiaoyuantest.www.WwwApplication : Starting WwwApplication on DD-HP with PID 48536 (E:mysoftkaifasoftkaifa_codeideamyiperf_springboottargetclasses started by DD in E:mysoftkaifasoftkaifa_codeideamyiperf_springboot)2020-07-08 11:02:11.266 INFO 48536 --- [ main] com.qiaoyuantest.www.WwwApplication : The following profiles are active: prod2020-07-08 11:02:12.207 INFO 48536 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!2020-07-08 11:02:12.208 INFO 48536 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.2020-07-08 11:02:12.228 INFO 48536 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 10ms. Found 0 repository interfaces.2020-07-08 11:02:12.301 INFO 48536 --- [ main] o.s.c.a.ConfigurationClassPostProcessor : Cannot enhance @Configuration bean definition ’myBatisMapperScannerConfig’ since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as ’static’.2020-07-08 11:02:12.522 INFO 48536 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean ’org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration’ of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$54b62352] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2020-07-08 11:02:12.613 INFO 48536 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean ’redisConfiguration’ of type [com.qiaoyuantest.www.config.RedisConfiguration$$EnhancerBySpringCGLIB$$9518fca7] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2020-07-08 11:02:12.651 ERROR 48536 --- [ main] o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.7] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]2020-07-08 11:02:12.808 ERROR 48536 --- [ main] o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.7] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]2020-07-08 11:02:12.927 INFO 48536 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8910 (http)2020-07-08 11:02:12.937 ERROR 48536 --- [ main] o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.7] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]2020-07-08 11:02:12.937 INFO 48536 --- [ main] o.a.coyote.http11.Http11NioProtocol : Initializing ProtocolHandler ['http-nio-8910']2020-07-08 11:02:12.944 INFO 48536 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2020-07-08 11:02:12.944 INFO 48536 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]2020-07-08 11:02:13.035 INFO 48536 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2020-07-08 11:02:13.036 INFO 48536 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1732 ms2020-07-08 11:02:13.623 INFO 48536 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ’applicationTaskExecutor’2020-07-08 11:02:13.676 INFO 48536 --- [ main] c.g.s.m.a.m.handler.StartUpHandlerImpl : databaseType=mysql,開(kāi)始執(zhí)行mysql的處理方法Loading class `com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.2020-07-08 11:02:13.829 INFO 48536 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} initedfile類(lèi)型的掃描2020-07-08 11:02:13.895 INFO 48536 --- [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 開(kāi)始創(chuàng)建表:em_t2020-07-08 11:02:13.983 INFO 48536 --- [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 完成創(chuàng)建表:em_t2020-07-08 11:02:14.002 INFO 48536 --- [ main] c.q.www.config.RedisConfiguration : 自定義RedisCacheManager加載完成2020-07-08 11:02:14.826 INFO 48536 --- [ main] o.a.coyote.http11.Http11NioProtocol : Starting ProtocolHandler ['http-nio-8910']2020-07-08 11:02:14.849 INFO 48536 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8910 (http) with context path ’’2020-07-08 11:02:14.851 INFO 48536 --- [ main] com.qiaoyuantest.www.WwwApplication : Started WwwApplication in 4.162 seconds (JVM running for 4.863)

springboot+mybatis通過(guò)實(shí)體類(lèi)自動(dòng)生成數(shù)據(jù)庫(kù)表的方法

此時(shí)查看一下數(shù)據(jù)庫(kù)表會(huì)發(fā)現(xiàn)新建有em_t表

springboot+mybatis通過(guò)實(shí)體類(lèi)自動(dòng)生成數(shù)據(jù)庫(kù)表的方法

新建表就這樣完成。

如出現(xiàn)Error creating bean with name ‘startUpHandlerImpl’: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/ArrayUtils錯(cuò)誤

springboot+mybatis通過(guò)實(shí)體類(lèi)自動(dòng)生成數(shù)據(jù)庫(kù)表的方法

說(shuō)明pom缺省包或者包不正確

<!--以下兩個(gè)類(lèi)需要加入,否則報(bào)錯(cuò)無(wú)法注入--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier><exclusions><exclusion><artifactId>commons-logging</artifactId><groupId>commons-logging</groupId></exclusion></exclusions></dependency>

如需要項(xiàng)目源碼或者對(duì)代碼有疑問(wèn)的評(píng)論留言,下期會(huì)動(dòng)手實(shí)現(xiàn)mybatis plus內(nèi)嵌的CRUD自動(dòng)增刪改查

到此這篇關(guān)于springboot+mybatis通過(guò)實(shí)體類(lèi)自動(dòng)生成數(shù)據(jù)庫(kù)表的方法的文章就介紹到這了,更多相關(guān)springboot mybatis實(shí)體類(lèi)生成數(shù)據(jù)庫(kù)表內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司_龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司 | 釜溪印象网络 - Powered by Discuz! | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 广西资质代办_建筑资质代办_南宁资质代办理_新办、增项、升级-正明集团 | 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 广东泵阀展|阀门展-广东国际泵管阀展览会 | 六自由度平台_六自由度运动平台_三自由度摇摆台—南京全控科技 | 走心机厂家,数控走心机-台州博城智能科技有限公司 | DWS物流设备_扫码称重量方一体机_快递包裹分拣机_广东高臻智能装备有限公司 | 机房监控|动环监控|动力环境监控系统方案产品定制厂家 - 迈世OMARA | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 南京和瑞包装有限公司| 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 知网论文检测系统入口_论文查重免费查重_中国知网论文查询_学术不端检测系统 | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | 吹塑加工_大型吹塑加工_滚塑代加工-莱力奇吹塑加工有限公司 | 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 塑胶跑道施工-硅pu篮球场施工-塑胶网球场建造-丙烯酸球场材料厂家-奥茵 | 干培两用箱-细菌恒温培养箱-菲斯福仪器 | 环氧铁红防锈漆_环氧漆_无溶剂环氧涂料_环氧防腐漆-华川涂料 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 电车线(用于供电给电车的输电线路)-百科| 专业的新乡振动筛厂家-振动筛品质保障-环保振动筛价格—新乡市德科筛分机械有限公司 | 煤粉取样器-射油器-便携式等速飞灰取样器-连灵动 | 小型单室真空包装机,食品单室真空包装机-百科 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 粉碎机_塑料粉碎机_塑料破碎机厂家-星标机械 | 福建自考_福建自学考试网 | 媒介云-全网整合营销_成都新闻媒体发稿_软文发布平台 | 全国国际学校排名_国际学校招生入学及学费-学校大全网 | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 成都茶楼装修公司 - 会所设计/KTV装修 - 成都朗煜装饰公司 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 楼承板-开口楼承板-闭口楼承板-无锡海逵 | 化工ERP软件_化工新材料ERP系统_化工新材料MES软件_MES系统-广东顺景软件科技有限公司 | 板式换网器_柱式换网器_自动换网器-郑州海科熔体泵有限公司 |