mybatis自动建表的实现方法
程序员文章站
2022-10-29 13:47:02
1.添加actable依赖 com.gitee.su...
1.添加actable依赖
<!--mybatis-plus自动建表--> <dependency> <groupid>com.gitee.sunchenbin.mybatis.actable</groupid> <artifactid>mybatis-enhance-actable</artifactid> <version>1.1.1.release</version> </dependency>
2.配置(在此需要注意配置的路径部分需要改成自己项目路径)
<!--mybatis-plus自动建表--> <dependency> <groupid>com.gitee.sunchenbin.mybatis.actable</groupid> <artifactid>mybatis-enhance-actable</artifactid> <version>1.1.1.release</version> </dependency>
3.添加配置类
package com.jpxx.clsh.autoconfig; 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; /** * @classname testconfig * @description * @author administrator * @date 2020/11/2 0002 11:30 * @version 1.0 */ @configuration @componentscan(basepackages = {"com.gitee.sunchenbin.mybatis.actable.manager.*"}) public class datasourceconfig{ //此处的路径按照yml或properties文件路径 @value("${jpxx.datasource.druid.driverclassname}") private string driver; @value("${jpxx.datasource.druid.url}") private string url; @value("${jpxx.datasource.druid.username}") private string username; @value("${jpxx.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.yml")); 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.jpxx.clsh.entity.*"); return sqlsessionfactorybean; } }
package com.jpxx.clsh.autoconfig; /** * @classname mybatismapperscannerconfig * @description * @author administrator * @date 2020/11/2 0002 10:15 * @version 1.0 */ 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(datasourceconfig.class) public class mybatismapperscannerconfig { @bean public mapperscannerconfigurer mapperscannerconfigurer() throws exception{ mapperscannerconfigurer mapperscannerconfigurer = new mapperscannerconfigurer(); mapperscannerconfigurer.setbasepackage("com.jpxx.clsh.dao.*;com.gitee.sunchenbin.mybatis.actable.dao.*"); mapperscannerconfigurer.setsqlsessionfactorybeanname("sqlsessionfactory"); return mapperscannerconfigurer; } }
4.实体类
package com.jpxx.clsh.entity; import com.gitee.sunchenbin.mybatis.actable.annotation.column; import com.gitee.sunchenbin.mybatis.actable.annotation.table; import com.gitee.sunchenbin.mybatis.actable.constants.mysqltypeconstant; import lombok.data; /** * @classname test * @description * @author administrator * @date 2020/10/30 0030 16:48 * @version 1.0 */ @data @table(name = "aaaaaaaaaaaaaa") public class test { @column(name = "role_id", type = mysqltypeconstant.int, isnull = false,iskey = true, isautoincrement = true, comment = "自增id") private long id; @column(name = "name", type = mysqltypeconstant.varchar, isnull = false, length = 20, comment = "角色名字") private string name; @column(name = "name_zh", type = mysqltypeconstant.varchar, isnull = true, length = 20, comment = "角色的中文名字") private string name_zh; }
运行日志
到此这篇关于mybatis自动建表的实现方法的文章就介绍到这了,更多相关mybatis 自动建表内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 年轻时还有这么段辉煌的事迹
下一篇: 倒贴25万没人要你