日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

springboot+mybatis通過實體類自動生成數據庫表的方法

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

前言

本章介紹使用mybatis結合mysql數據庫自動根據實體類生成相關的數據庫表。

首先引入相關的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><!--以下兩個類需要加入,否則報錯無法注入--><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>

添加數據庫配置文件application.propertiesapplication.properties這里是單獨配置mybatis自動建表的相關信息。

mybatis.table.auto=updatemybatis.model.pack=com.xxx.xxx.entity//實體類的路徑mybatis.database.type=mysql

mybatis.table.auto=

create:每次加載hibernate會自動創建表,以后啟動會覆蓋之前的表,所以這個值基本不用,嚴重會導致的數據的丟失。

create-drop :每次加載hibernate時根據model類生成表,但是sessionFactory一關閉,表就自動刪除,下一次啟動會重新創建。

update:加載hibernate時根據實體類model創建數據庫表,這是表名的依據是@Entity注解的值或者@Table注解的值,sessionFactory關閉表不會刪除,且下一次啟動會根據實體。

model:更新結構或者有新的實體類會創建新的表。

validate:啟動時驗證表的結構,不會創建表 none:啟動時不做任何操作

mybatis.model.pack=com.xxx.xxx.entity//你實體類的路徑

個人項目配置文件,非統一,根據項目需求配置

springboot+mybatis通過實體類自動生成數據庫表的方法

進行生成數據庫表相關配置

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 {//連接數據庫配置文件的地址,具體查閱配置文件的結構 @Value('${spring.datasource.druid.driver-class-name}') private String driver;//連接數據庫配置文件的地址,具體查閱配置文件的結構 @Value('${spring.datasource.druid.url}') private String url;//連接數據庫配置文件的地址,具體查閱配置文件的結構 @Value('${spring.datasource.druid.username}') private String username;//連接數據庫配置文件的地址,具體查閱配置文件的結構 @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.*替換成你的實體類地址 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)//上面第一點配置文件類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; }}

新建實體進行測試

注:@Table(name = “”)及@Column(name = “id”)注解使用,實體類繼承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')//新建表數據庫表名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方法}

運行項目

會控制臺會顯示說新建表完成2020-07-08 11:02:13.895 INFO 48536 — [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 開始創建表:em_t2020-07-08 11:02:13.983 INFO 48536 — [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 完成創建表: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,開始執行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類型的掃描2020-07-08 11:02:13.895 INFO 48536 --- [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 開始創建表:em_t2020-07-08 11:02:13.983 INFO 48536 --- [ main] s.m.a.m.s.SysMysqlCreateTableManagerImpl : 完成創建表: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通過實體類自動生成數據庫表的方法

此時查看一下數據庫表會發現新建有em_t表

springboot+mybatis通過實體類自動生成數據庫表的方法

新建表就這樣完成。

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

springboot+mybatis通過實體類自動生成數據庫表的方法

說明pom缺省包或者包不正確

<!--以下兩個類需要加入,否則報錯無法注入--><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>

如需要項目源碼或者對代碼有疑問的評論留言,下期會動手實現mybatis plus內嵌的CRUD自動增刪改查

到此這篇關于springboot+mybatis通過實體類自動生成數據庫表的方法的文章就介紹到這了,更多相關springboot mybatis實體類生成數據庫表內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美精品九九| 99久久亚洲精品蜜臀| 亚洲精品系列| 欧美天堂在线| 精品一区av| 四虎影视精品| 亚洲精华国产欧美| 日韩欧美激情| 粉嫩av一区二区三区四区五区| 精品视频在线观看网站| 韩国精品主播一区二区在线观看| 在线一区欧美| 国产精品视频一区二区三区四蜜臂| 国产999精品在线观看| 欧美日韩国产在线观看网站| 亚洲欧美网站在线观看| 精品久久电影| 伊人久久婷婷| 国产日韩中文在线中文字幕| 美女福利一区二区三区| 免费人成网站在线观看欧美高清| 国产日韩一区| 亚洲香蕉网站| 国产欧美91| 色老板在线视频一区二区| 日韩精品一区第一页| 精品一区二区三区中文字幕视频 | 国产一区久久| 日本不卡在线视频| 日韩久久视频| 日韩中文av| 久久久精品网| 国产情侣一区在线| 欧美午夜精彩| 国产精品最新| 欧美日韩国产一区二区三区不卡| 国产探花一区在线观看| 狠狠干成人综合网| 免费在线观看一区| 久热re这里精品视频在线6| 久久精品三级| 日韩影片在线观看| 免费久久久久久久久| 国产伦理一区| 性一交一乱一区二区洋洋av| 精品国产欧美日韩一区二区三区| 影音先锋久久精品| 欧美特黄一级大片| 国产精品久久乐| 久久aⅴ国产紧身牛仔裤| 激情久久一区二区| 日本成人一区二区| 欧美粗暴jizz性欧美20| 你懂的亚洲视频| 亚洲毛片网站| 91精品成人| аⅴ资源天堂资源库在线| 日韩欧美另类中文字幕| 欧美不卡视频| 波多野结衣久久精品| 欧美日韩1区| 爽好久久久欧美精品| 桃色一区二区| 久久久久伊人| 亚洲免费一区三区| 合欧美一区二区三区| 色在线视频观看| 美腿丝袜亚洲一区| 97成人在线| 亚洲精品成人一区| 亚洲一区网站| 欧美一区三区| 日韩国产在线| 精品欠久久久中文字幕加勒比| 日韩精品视频网站| 在线精品视频一区| 亚洲欧美日韩国产一区| 三级精品视频| 国产传媒在线| 欧美成人精品一级| 日韩精品免费视频人成| 性欧美精品高清| 一区福利视频| 影音国产精品| 狠狠色综合网| 亚洲欧美日韩高清在线| 久久九九99| 亚洲成人va| 久久免费高清| 欧美日韩精品在线一区| 日韩不卡在线| 三级精品视频| 亚洲91视频| 99精品在线观看| 人人香蕉久久| 天堂网在线观看国产精品| 欧美日一区二区| 麻豆精品久久久| 日本va欧美va精品发布| 亚洲少妇自拍| 激情丁香综合| 午夜av不卡| 国产va在线视频| 欧美精品97| 欧美日韩一区二区三区不卡视频 | 日韩一区欧美二区| 色天使综合视频| 91日韩欧美| 久久精品三级| 免费日韩成人| 国产亚洲精品美女久久| 日韩三级视频| 日欧美一区二区| 亚洲精品自拍| 婷婷精品在线| 日韩精品三区四区| 日韩激情中文字幕| 日韩和欧美一区二区三区| 亚洲免费网址| 一本一本久久| 国产精品美女| 在线成人动漫av| 激情欧美一区| 亚洲国产成人精品女人| 欧美精品一二| 国产模特精品视频久久久久| 亚洲激情社区| 中文一区一区三区免费在线观| 羞羞答答国产精品www一本| 久久亚洲风情| 天堂va欧美ⅴa亚洲va一国产| 亚洲免费毛片| 911精品国产| 国产精品久久久久久久免费软件| 国产精品激情| 国产一区二区三区久久久久久久久| 久久福利在线| 麻豆精品在线视频| 肉色欧美久久久久久久免费看 | 欧美韩一区二区| 国精品产品一区| 日韩国产一区二区| 免费黄色成人| 亚洲一级淫片| 国产亚洲一区二区三区啪| 免费一级欧美在线观看视频 | 亚洲性图久久| 视频一区中文字幕国产| 免费成人av在线播放| 日本成人中文字幕在线视频| 欧美一区二区三区久久| 久久影院资源站| 三级在线看中文字幕完整版| 香蕉精品久久| 亚洲三区欧美一区国产二区| 国产精品一区2区3区| 成人午夜在线| 免费观看不卡av| 一区二区电影在线观看| 国产精品色在线网站| 日本黄色精品| 亚洲精品在线观看91| 日韩激情av在线| 成人在线视频区| 欧美影院三区| 综合国产视频| 国产精品久久久久久久久久10秀| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 欧美日韩精品一区二区视频| 中日韩男男gay无套| 欧美欧美黄在线二区| 成人三级高清视频在线看| 欧美日韩一区二区三区视频播放| 亚洲欧洲日韩| 日韩成人三级| 亚洲精品乱码日韩| 日产精品一区二区| 亚洲综合不卡| 国产aⅴ精品一区二区四区| re久久精品视频| 亚洲免费毛片| 三级精品视频| 久久精品99国产精品| 最新中文字幕在线播放| 99日韩精品| 精品色999| 首页欧美精品中文字幕| 国产成人久久| 亚洲欧美专区| 成人精品天堂一区二区三区| 日本天堂一区| 美女久久久久| 精品视频黄色| 免费美女久久99| 久久91导航| 欧美亚洲国产日韩| 蜜桃视频欧美| 国产一区二区精品福利地址| 男女男精品网站| 日韩中文字幕高清在线观看|