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

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

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

瀏覽:117日期: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久久,三上悠亚国产精品一区二区三区
国模 一区 二区 三区| 天堂av在线| 国产视频一区二区在线播放| 日韩精品一区二区三区中文| 日韩精品成人| 久久精品伊人| 日韩精品免费一区二区三区| 蜜桃视频一区二区三区| 亚洲视频二区| 国产精品xvideos88| 91亚洲成人| 91高清一区| 一区二区电影| 久久亚州av| 欧美 日韩 国产一区二区在线视频 | 国产a亚洲精品| 国产精品一区二区免费福利视频| 97精品国产99久久久久久免费| 日韩不卡一二三区| 国产精品一区二区精品| 风间由美中文字幕在线看视频国产欧美| 国产精品白浆| 日韩电影免费网站| 国产精品免费看| 91福利精品在线观看| 麻豆国产一区| 欧美.日韩.国产.一区.二区 | av一区二区高清| 亚洲精品高潮| 国产精品亚洲欧美| 成人福利视频| 三级一区在线视频先锋| 久久国产生活片100| 国产精品麻豆久久| 免费视频国产一区| 在线观看视频免费一区二区三区| 国产麻豆一区二区三区精品视频| 鲁鲁在线中文| 免费成人av在线播放| 国产精品99精品一区二区三区∴| 日韩毛片在线| 日本在线成人| 国产精品成人一区二区不卡| 高清一区二区| 伊人久久婷婷| 国产午夜精品一区在线观看| 蜜桃成人精品| 丝瓜av网站精品一区二区 | 视频一区在线视频| 国产精品扒开腿做爽爽爽软件| 99久久精品网| 日韩av黄色在线| 999国产精品视频| 欧美日一区二区在线观看| 日韩三区免费| 日本午夜精品| 欧美成人综合| 久久天堂影院| 亚洲综合色婷婷在线观看| 手机在线电影一区| 日本中文字幕一区二区| 亚洲va中文在线播放免费| 日本不卡视频在线| 一区二区三区四区日本视频| 亚洲一区欧美二区| 精品久久久久中文字幕小说| aⅴ色国产欧美| 精品久久久网| 日韩精品福利一区二区三区| 中文另类视频| 国产精品网址| 免费在线欧美视频| 秋霞影视一区二区三区| 久久国产乱子精品免费女| 狠狠干综合网| 国产盗摄——sm在线视频| 色8久久久久| 国产二区精品| 黄色精品视频| 欧美在线观看天堂一区二区三区| 欧美日韩四区| 国产v日韩v欧美v| 国产免费播放一区二区| 亚洲中午字幕| 久久精品主播| 国产一区二区三区探花| 日韩av午夜在线观看| 黄色亚洲精品| 亚洲二区视频| 亚洲三级欧美| 成人精品久久| 久久99精品久久久野外观看| 亚洲制服欧美另类| 国产综合色产| 久久精品卡一| 精品日韩视频| 日本免费一区二区三区四区| 久久一区精品| 国产精品久久久久毛片大屁完整版| 视频在线观看国产精品| 精品1区2区3区4区| 亚洲国产综合在线看不卡| аⅴ资源天堂资源库在线| 国产精品探花在线观看| 日韩和欧美的一区| 亚洲欧美久久精品| 一区二区精彩视频| 伊人国产精品| 天堂va蜜桃一区二区三区| 欧美日韩色图| 久久久久国产精品一区二区| 日韩av二区| 成人在线视频中文字幕| 开心激情综合| 精品视频网站| 精品久久久亚洲| 狠狠久久伊人中文字幕| 成人在线超碰| 色婷婷亚洲mv天堂mv在影片| 成人高清一区| 亚洲综合电影| 久久精品国产99久久| 久久婷婷av| 91精品一区二区三区综合在线爱| av一区在线| 欧美精品一区二区久久| 五月精品视频| 亚洲在线一区| 四虎在线精品| 日韩av不卡一区二区| 国产日产一区| 久久一区欧美| 日韩免费福利视频| 久久一区二区三区电影| 精精国产xxxx视频在线野外| 久久久五月天| 久久中文字幕av一区二区不卡| 狠狠干成人综合网| 性色av一区二区怡红| 免费精品视频在线| 欧美日本久久| 国产一区二区三区免费在线| 成人久久一区| 视频在线观看一区| 国产欧美久久一区二区三区| 国产成人精品一区二区免费看京| 日韩在线不卡| 136国产福利精品导航网址| 国产精品三上| 91伊人久久| av中文字幕在线观看第一页| 欧美99久久| 日本麻豆一区二区三区视频| 国产乱码精品一区二区亚洲| 国产激情久久| 香蕉成人av| 亚洲精品无播放器在线播放| 国产精品黄色片| 四虎4545www国产精品 | 韩国精品主播一区二区在线观看 | 国产精品试看| 日本国产亚洲| 欧美国产偷国产精品三区| 国产综合精品一区| 婷婷综合成人| 成年男女免费视频网站不卡| 夜夜嗨一区二区| 国产精品第十页| 婷婷精品视频| 日韩精品午夜| 日韩精品a在线观看91| 视频在线不卡免费观看| 国产精品普通话对白| 国产精品xvideos88| 久久精品国产亚洲夜色av网站| 日精品一区二区三区| 欧美激情aⅴ一区二区三区| 91tv亚洲精品香蕉国产一区| 国产亚洲精品v| 老牛国内精品亚洲成av人片| 五月天综合网站| 国产精品成人国产| 亚洲免费黄色| 麻豆国产精品| 久久国产精品久久久久久电车| 久久不卡日韩美女| 国内激情久久| 免费日韩一区二区三区| 欧美另类专区| 久久亚洲人体| 免费人成黄页网站在线一区二区| 国产精品一区二区三区av麻 | 欧美日韩国产在线一区| 国产伦精品一区二区三区在线播放 | 久久天堂影院| 天海翼亚洲一区二区三区| 99久久久久国产精品| 国产日产精品一区二区三区四区的观看方式| 激情久久久久久| 久久在线91|