日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Springboot,Mybatis根据实体类自动建表

發(fā)布時(shí)間:2024/3/13 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot,Mybatis根据实体类自动建表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Springboot,Mybatis根據(jù)實(shí)體類(lèi)自動(dòng)建表

在創(chuàng)建實(shí)體類(lèi)的同時(shí)還要在數(shù)據(jù)庫(kù)建表,如果只是很少的屬性那么無(wú)所謂,但是當(dāng)實(shí)體類(lèi)很多或者字段很多時(shí)這就是一個(gè)非常讓人不爽的事情了

通常有兩種思路,一種是根據(jù)數(shù)據(jù)庫(kù)來(lái)自動(dòng)創(chuàng)建實(shí)體類(lèi),這個(gè)mybatis-generator已經(jīng)提供了方法
還有一種思路就是根據(jù)實(shí)體類(lèi)來(lái)自動(dòng)建立表格

這里需要用到A.CTable框架,也就是mybatis-enhance-actable

一個(gè)小Demo,一步步來(lái)

首先添加依賴項(xiàng)

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.17</version></dependency><!-- 添加mybatis依賴 --><!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.1</version></dependency><!--添加A.CTable框架 --><dependency><groupId>com.gitee.sunchenbin.mybatis.actable</groupId><artifactId>mybatis-enhance-actable</artifactId><version>1.0.3</version></dependency><!-- 阿里系的Druid依賴包 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.9</version></dependency><!-- Druid 依賴 log4j包 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>

項(xiàng)目結(jié)構(gòu)如下

然后在application.yml文件里添加配置信息

#服務(wù)配置 server:port: 8080spring:#數(shù)據(jù)庫(kù)配置datasource:username: yourusernamepassword: yourpassworddriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://yoururl#阿里druid連接池驅(qū)動(dòng)配置信息type: com.alibaba.druid.pool.DruidDataSource#連接池的配置信息druid:#初始化大小,最小,最大initial-size: 2#A.CTable配置 mybatis:#自動(dòng)更新表table:auto: true#實(shí)體類(lèi)掃描地址model:pack: com.boot_demo.demo1.entity#數(shù)據(jù)庫(kù)類(lèi)型 database:type: mysql

下面開(kāi)始寫(xiě)配置信息

@Configuration @ComponentScan(basePackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"}) public class TestConfig {@Value("${spring.datasource.driver-class-name}")private String driver;@Value("${spring.datasource.url}")private String url;@Value("${spring.datasource.username}")private String username;@Value("${spring.datasource.password}")private String password;@Beanpublic YamlPropertiesFactoryBean configProperties() throws Exception{YamlPropertiesFactoryBean propertiesFactoryBean = new YamlPropertiesFactoryBean();PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();propertiesFactoryBean.setResources(resolver.getResources("classpath*:application.yml"));return propertiesFactoryBean;}@Beanpublic 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;}@Beanpublic DataSourceTransactionManager dataSourceTransactionManager() {DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource());return dataSourceTransactionManager;}@Beanpublic 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.example.entity.*");return sqlSessionFactoryBean;}} @Configuration @AutoConfigureAfter(TestConfig.class) public class MyBatisMapperScannerConfig {@Beanpublic MapperScannerConfigurer mapperScannerConfigurer() throws Exception{MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();mapperScannerConfigurer.setBasePackage("com.example.dao.*;com.gitee.sunchenbin.mybatis.actable.dao.*");mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");return mapperScannerConfigurer;} }

下面創(chuàng)建實(shí)體類(lèi)
根據(jù)Column注解在數(shù)據(jù)庫(kù)創(chuàng)建指定的結(jié)構(gòu)

@Table(name="test") public class Test {@Column(name="id",type= MySqlTypeConstant.VARCHAR)private String id;@Column(name="value",type = MySqlTypeConstant.VARCHAR)private String value;@Column(name="comment",type = MySqlTypeConstant.VARCHAR)private String comment;}

如果指定了某個(gè)字段為主鍵且要自增,那么該字段必須為Integer-MySqlTypeConstant.INT且需要在Column注解中添加如下屬性

isKey = true,isAutoIncrement = true

mybatis-enhance-actable本身BaseMysqlCRUDManager提供了根據(jù)實(shí)體類(lèi)進(jìn)行簡(jiǎn)單增刪改查的方法,當(dāng)然如果有特殊需要還是要自己寫(xiě)mapper映射的,這里就不寫(xiě)了,來(lái)看看它提供的基礎(chǔ)方法

先寫(xiě)dao層

@Repository public class TestDao {@Autowiredprivate BaseMysqlCRUDManager baseMysqlCRUDManager;public void delete(Test test){baseMysqlCRUDManager.delete(test);}public void insert(Test test){baseMysqlCRUDManager.save(test);} }

然后寫(xiě)一下service層

@Service public class TestServiceImpl implements TestService {@Autowiredprivate TestDao testDao;@Overridepublic void insert(Test test) {testDao.insert(test);}@Overridepublic void delete(Test test) {testDao.delete(test);} }

controller層

@RestController @RequestMapping("/test") public class TestController {@Autowiredprivate TestService testService;@RequestMapping("/hello")public String hello(){return "hello";}@RequestMapping("/add")public String add(){Test test=new Test();test.setId(UUID.randomUUID().toString());test.setValue("插入測(cè)試");test.setComment("插入測(cè)試");try {testService.insert(test);} catch (Exception e) {e.printStackTrace();return "failed";}return "success";}@RequestMapping("/remove")public String remove(){Test test=new Test();test.setId("1b2c3d4e");try {testService.delete(test);} catch (Exception e) {e.printStackTrace();return "failed";}return "success";} }

下面啟動(dòng)項(xiàng)目,我們可以看到控制臺(tái)日志出現(xiàn)這樣的信息

這就代表數(shù)據(jù)庫(kù)表已經(jīng)建立成功了

如果控制臺(tái)出現(xiàn)ClassNotFound:apache.common.lang.ArrayUtils的報(bào)錯(cuò),就需要再添加一下依賴

<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency>

下面測(cè)試一下add和delete方法
我先在數(shù)據(jù)庫(kù)中添加數(shù)據(jù)



數(shù)據(jù)添加成功


數(shù)據(jù)刪除成功

參考文章
spring-boot + mybatis-enhance-actable 實(shí)現(xiàn)mybatis自動(dòng)建表
SpringBoot+Mybatis 自動(dòng)創(chuàng)建數(shù)據(jù)表

總結(jié)

以上是生活随笔為你收集整理的Springboot,Mybatis根据实体类自动建表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。