①: Introduce dependencies (pom)
1. Introduce basic dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2. Introduce mysql, myBatis, and data sources
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!--Plug-in to create tables--> <dependency> <groupId>com.gitee.sunchenbin.mybatis.actable</groupId> <artifactId>mybatis-enhance-actable</artifactId> <version>1.5.0.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.0</version> </dependency> <!--druid data source--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency>
3. maven plug-in (optional)
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
②: Configure yaml (myBatis, data connection)
server: port: 8089 spring: datasource: url: jdbc:mysql://your ip address:3306/self_Project?characterEncoding=UTF-8 & amp;serverTimezone=Asia/Shanghai username: root password: www.Coke.com driver-class-name: com.mysql.cj.jdbc.Driver servlet: multipart: max-file-size: 20MB max-request-size: 20MB #jpa: # database-platform: org.hibernate.dialect.MySQLDialect mybatis: table: auto: add #create After the system starts, all tables will be deleted, and then the tables will be rebuilt according to the structure configured in the model. This operation will destroy the original data. #update The system will automatically determine which tables are newly created, which fields need to be modified, etc., which fields need to be deleted, and which fields need to be added. This operation will not destroy the original data. #none The system does not perform any processing. #add is the function of adding a new table/adding a field/adding an index/adding a unique constraint without modification or deletion (only supported in version 1.0.9.RELEASE and above). model: pack: com.it.entity #Scan the package name of the object used to create the table. Multiple packages are separated by "," database: type: mysql #Database type Currently only supports mysql
③: Configuration class
1. Data source configuration DataSourceConfig
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.yaml")); 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.it.entity.*"); return sqlSessionFactoryBean; } }
2. myBatis related configuration 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(DataSourceConfig.class) public class MyBatisMapperScannerConfig {<!-- --> @Bean public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{<!-- --> MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("com.it.mapper.*;com.gitee.sunchenbin.mybatis.actable.dao.*"); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); return mapperScannerConfigurer; } }
④: Create entity class
1. Create entity class User
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; 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 com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant; import lombok.Data; import java.io.Serializable; import java.util.Date; @Data @Table(name = "user_test", comment = "Test table")//Set table name comment public class User extends BaseModel implements Serializable {<!-- --> private static final long serialVersionUID = 9435278593475930L; @IsAutoIncrement //Self-increment @IsKey //primary key @Column(comment = "User ID")//Field comments private Long id; /** * Creation time */ @Column(name = "create_time", type = MySqlTypeConstant.DATETIME, comment = "Creation time") private Date createTime; /** * Change the time */ @Column(name = "update_time", type = MySqlTypeConstant.DATETIME, comment = "Modification time") private Date updateTime; /** * Contact email */ @Column(name = "email",comment = "Contact Email", length = 255) private String email; /** *Contact mobile phone */ @Column(name = "mobile",comment = "Contact mobile phone", length = 255) private String mobile; /** Is it a super administrator? 0 No 1 Yes */ @Column(name = "super_admin",comment = "Is it a super administrator", defaultValue = "0",length = 10) private Integer superAdmin = 0; /** * Name */ @Column(name = "name", comment = "name", length = 255) private String name; /** * avatar */ @Column(name = "avatar", comment = "avatar", defaultValue = "https://img01.yzcdn.cn/vant/leaf.jpg", length = 255) private String avatar; /** Login Username */ @Column(name = "username", comment = "Login username", length = 255) private String username; /** * password */ @Column(name = "password", comment = "password", length = 200) private String password; /** *Account expiry */ @JsonIgnore @Column(name = "prescription", comment = "Account expiry", length = 200) private Integer prescription; /** * Password salt */ @JsonIgnore @Column(name = "salt", comment = "Password Salt", length = 255) private String salt; /** * Status 0, normal 1, locked */ @Column(name = "state",comment = "State 0, normal 1, locked", defaultValue = "0", length = 10) private Integer state; /** * The first 5 passwords */ @JsonIgnore @Column(name = "his_pass", comment = "First 5 passwords", length = 255) private String hisPass; /** * Delete status 0, normal 1, delete */ @Column(name = "del_state", comment = "Delete state 0, normal 1, delete", defaultValue = "0", length = 10) private Integer delState; /** * Last password modification time */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") @Column(name = "up_pass_time", type = MySqlTypeConstant.DATETIME, comment = "Last password modification time") private Date upPassTime; /** *Deletion time */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") @Column(name = "del_pass_time", type = MySqlTypeConstant.DATETIME, comment = "Delete time") private Date delPassTime; /** * last login time */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") @Column(name = "last_login_time", type = MySqlTypeConstant.DATETIME, comment = "Last login time") private Date lastLoginTime; /** * Automatically add time when adding new data */ @PrePersist protected void onCreate() {<!-- --> create_time = new Date(); } /** * Automatically update time when updating data */ @PreUpdate protected void onUpdate() {<!-- --> pwd_update_time = new Date(); } /** * Automatically update time when deleting data */ @PreRemove protected void onDelete() {<!-- --> is_deleted = 1; } /** * Automatically update time when logging in */ @PreUpdate protected void onLogin() {<!-- --> last_login_time = new Date(); } }
⑤:Test
1. Start the project
2. Created successfully