How to create a database from an entity class
Entity classes generate database tables
Method 1 This method supports only the mysql database
<dependency> <groupId>com.gitee.sunchenbin.mybatis.actable</groupId> <artifactId>mybatis-enhance-actable</artifactId> < version > 1.1.1. RELEASE < / version > < / dependency > < the dependency > < groupId > com. Alibaba < / groupId > <artifactId>druid</artifactId> <version>1.1.21</version> </dependency> <dependency> <groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> < the groupId > org. Mybatis. Spring. The boot < / groupId > < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 2.2.0 < / version > </dependency> <! -- Mybatisplus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> The < version > 3.3.2 rainfall distribution on 10-12 < / version > < / dependency >Copy the code
The configuration file
# database configuration spring: a datasource: driver - class - name: the mysql. Cj.. JDBC driver url: JDBC: mysql: / / localhost: 3306 / test? useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC username: root password: "000000" type: Com. Alibaba. Druid. Pool. # DruidDataSource mybatisplus mybatis configuration: table: auto: Create #create When the system starts up, all the tables will be dropped, and the tables will be rebuilt according to the structure configured in model. This operation will destroy the original data. #update the system automatically determines which tables are newly created, which fields are to be changed, which fields are to be deleted, and which fields are to be added. This operation does not destroy the original data. #none The system does nothing. #add Add table/field/index/unique constraint (only supported in version 1.0.9.RELEASE) Model: pack: com. Test. Tongydayi. Entity # scan the package name to create a table of an object, multiple packages ", "separated from the database: type: # mysql database type Currently only supports mysql mybatis - plus: mapper-locations: classpath:/mapper/*mapper.xmlCopy the code
The configuration class
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 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.setLocations(resolver.getResources("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.setMapperLocations(resolver.getResources("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/ */*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("com.test.tongdayi.entity.*"); return sqlSessionFactoryBean; }}Copy the code
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.blog.table.mapper.*;com.gitee.sunchenbin.mybatis.actable.dao.*");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}
Copy the code
Entity class
import com.alibaba.druid.sql.visitor.functions.Char; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.gitee.sunchenbin.mybatis.actable.annotation.Column; import com.gitee.sunchenbin.mybatis.actable.annotation.IsAutoIncrement; import com.gitee.sunchenbin.mybatis.actable.annotation.IsKey; import com.gitee.sunchenbin.mybatis.actable.annotation.Table; import com.gitee.sunchenbin.mybatis.actable.command.BaseModel; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; Public class user extends BaseModel {public class user extends BaseModel {public class user extends BaseModel { @tableId (type = idtype.auto)//mybatis-plus primary key annotation @isautoincrement // increment @iskey //actable primary key annotation @column (comment = "User ID") private Long ID; private Long ID; @column (comment = "nickName ") private String nickName; @column (comment = "email ") private String Email; @column (name = "create_time",comment = "create_time") private Date createTime; @column (name = "update_time",comment = "updateTime ") private Date updateTime; @column (comment = "avatar ") private String avatar; @column (comment = "username ") private String username; @column (comment = "password ") private String password; @column (comment = "gender ",type = "char") private char sex; }Copy the code
– Method 2 has tried MySQL PostgresQL database but not other tests
- You can choose between relying on database drivers
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>Copy the code
The configuration file
# spring.datasource. Driver -class-nameSpring. Datasource. Url = JDBC \:mysql\:/ / 127.0.0.1 \ : 3306 / xiaomi? useUnicode=true&characterEncoding=UTF-8
#spring.datasource.url=jdbc:postgresql:/ / 127.0.0.1:5432 / xinruishanghu# database account spring. The datasource. The username = # root database password spring. The datasource. Password =000000# naming strategy of hibernate spring. Jpa. Hibernate. Naming. The strategy = org. Hibernate. CFG. ImprovedNamingStrategy # automatically update create | | authentication database table structure # create: Each time Hibernate is loaded, delete the last generated table #update: When hibernate is loaded for the first time, the model class will automatically set up the table structure (provided that the database has been set up first). Spring.jpa.hibernate. DDL -auto=updateCopy the code
Entity class
// Indicates that this class is an entity class
@Entity
// Do not use table the default is the entity class name
@Table(name = "aaa")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id1;
private int age;
private String name;
// Omit the set get method
}
Copy the code
postgresql