springboot+mybatismybatis-plus根据实体类⾃动创建数据库表1、导⼊依赖
<!--mybatisplus启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<!--创建表的插件-->
<dependency>
<groupId>com.batis.actable</groupId>
<artifactId>mybatis-enhance-actable</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
2、l配置⽂件
#数据库配置
spring:
datasource:
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myblog?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password:123456
type: com.alibaba.druid.pool.DruidDataSource
#mybatisplus配置
mybatis:
table:
auto: create
#create 系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。
#update 系统会⾃动判断哪些表是新建的,哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。
#none 系统不做任何处理。
#add 新增表/新增字段/新增索引/新增唯⼀约束的功能,不做做修改和删除(只在版本1.0.9.RELEASE及以上⽀持)。
model:
pack: com.ity #扫描⽤于创建表的对象的包名,多个包⽤“,”隔开
database:
type: mysql #数据库类型⽬前只⽀持mysql
mybatis-plus:
mapper-locations: classpath:/mapper/*l
3、编写DataSourceConfig、MyBatisMapperScannerConfig这两个配置类(不能将这两个配置类合并)
DataSourceConfig
@ComponentScan(basePackages ={"com.batis.actable.manager.*"})
public class DataSourceConfig {
@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;
@Bean
public PropertiesFactoryBean configProperties()throws Exception{
PropertiesFactoryBean propertiesFactoryBean =new PropertiesFactoryBean();
PathMatchingResourcePatternResolver resolver =new PathMatchingResourcePatternResolver();
propertiesFactoryBean.Resources("classpath*:application.properties"));
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.Resources("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("ity.*");
return sqlSessionFactoryBean;
}
}
MyBatisMapperScannerConfig
@AutoConfigureAfter(DataSourceConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer()throws Exception{
MapperScannerConfigurer mapperScannerConfigurer =new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.blog.table.mapper.*;com.batis.actable.dao.*"); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}
3、实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name ="user")//设置表名
public class User extends BaseModel {
@TableId(type = IdType.AUTO)//mybatis-plus主键注解
@IsAutoIncrement//⾃增
springboot结构@IsKey//actable主键注解
@Column(comment ="⽤户ID")//对应数据库字段,不配置name会直接采⽤属性名作为字段名comment是注解
private Long id;
@Column(comment ="昵称")
private String nickName;
@Column(comment ="邮箱")
private String email;
@Column(name ="create_time",comment ="创建时间")
private Date createTime;
@Column(name ="update_time",comment ="修改时间")
private Date updateTime;
@Column(comment ="头像")
private String avatar;
@Column(comment ="⽤户名")
private String username;
@Column(comment ="密码")
private String password;
}
配置完这些之后,启动springboot应⽤
启动完成之后可以在控制台到这样的⼀组信息:
如果是这样的话就说明⾃动建表成功!
可以跳转到数据库⾥⾯查看是否创建成功
可以看到我们这⾥的数据库表已经⾃动创建成功了
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论