Put the source code first: dream blog
Project Structure Analysis
If you are already familiar with the SSM development process, you can simplify the creation of SpringBoot project artifacts and directly add the name you want. It is best to be the same as here. src/java is a third-level directory. Don’t complicate this directory.
I skip some annotations by default @Autowired and @Mapper @Repository etc.
- I don’t know what AOP is doing, it seems to be a log event anyway, I can’t understand it
- config is the background login class intercepted by the interceptor.
- The controller is the control layer to realize the interaction between the back-end data and the front-end page
- interceptor is a special class for interceptor settings corresponding to intercepting login
- mapper database operation management
- The pojo (or entity) entity class and its attributes correspond to the database table name and column name
- The service service layer is just to perform database operations (mapper)
- The utils toolkit generally involves algorithm encryption or plug-ins, etc. I don't quite understand it.
- The resources resource package static static resource SpringBoot has defaulted to static/this path refers to the direct/package name. The templates package is a web page, which needs to be configured by mvc.
- Generally, the applicaiton.yml configuration file is used
The database datasource does not explain that thymeleaf is the key for the controller to jump to the corresponding webpage. Mybatis locks the location of mapper.xml. This project has no mapper-locaitons configuration because the interface and xml file are put together.
···xml
server: port: 8080 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/myblog thymeleaf: cache: false mode: HTML prefix: classpath:/templates/ suffix: .html mybatis: type-aliases-package: com.dreamchaser.pojo configuration: log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl log-prefix: mapper.
```
Regarding the pom.xml dependency, I am too lazy to upload, because the source code is already very detailed
Personally recommended development ideas:
Database --> entity class --> dao layer --> service layer --> controller layer --> other support: tools or interception
If you do not use automatic data table generation, you need to create a database and import data first.
Backend src/java:
Entity class
The attribute name is the same as the column name of the data table, so I can understand it by looking at the code.
dao class
The interface of the database operation method, what functions need to be written, and what functions are commonly added, deleted, modified and checked. Pay attention to the use of annotation @mapper statement
or @Repository implements an injectable bean
service class
Contains service interface and service implementation class. The service interface is consistent with the dao interface. The implementation class annotations @Service and @Autowired inject the required dao class to implement the database method you want to operate. Brainless alt+insert.....
The front end uses the thymeleaf template:
Note the use of <html xmlns:th="//www.thymeleaf.org"> to introduce support
extra theme
Here comes the point, using mybatis-generator or Spring data jpa to automatically generate code in two cases
mybatis-generator generates code:
Oral basic configuration:
pom.xml requires mysql mybatis generator-core generator-maven-plugin
maven remember to add
```xml
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
```
```yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/myblog thymeleaf: cache: false mode: HTML prefix: classpath:/templates/ suffix: .html
#Use mybatis-plus to pay attention to the path similar to the following
mybatis: type-aliases-package: com.dreamchaser.pojo mapper-locations: your path
```
Next, generatorConfig.xml puts down the resources root directory
Change jar package
jdbc connection information
Generate entity class address
Generate map.xml file storage address
Generate interface
table table name
If you want to generate files corresponding to multiple tables, just add table tags
Add maven command line mybatis-generator:generate -e Execute
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置生成器 -->
<generatorConfiguration>
<!--classPathEntry: The JDBC driver of the database, you can choose your own driver location-->
<classPathEntry location="D:\mysql-connector-java-8.0.28.jar"/>
<!-- One database, one context, defaultModelType="flat" large data field, regardless of table -->
<context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- Automatically identify database keywords, default false, if set to true, according to the keyword list defined in SqlReservedWords; generally keep the default value, encounter database keywords (Java keywords), use columnOverride to override -->
< property name="autoDelimitKeywords" value="true"/>
<!-- The encoding of the generated Java file-->
<property name="javaFileEncoding" value="utf-8"/>
<!-- beginningDelimiter and endingDelimiter: indicates the symbol used to mark the database object name of the database, such as ORACLE is double quotes, MYSQL defaults to `backticks; --> <
property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- Format java code -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代码 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- Comments -->
<commentGenerator>
<property name="suppressAllComments" value="true"/><!-- Whether to cancel comments -->
<property name="suppressDate" value="false"/> < !-- Whether to generate a comment generation timestamp -->
</commentGenerator>
<!-- jdbc连接-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssmtest?serverTimezone=UTC" userId="root"
password="123456"/>
<!-- Type conversion-->
<javaTypeResolver>
<!-- Whether to use bigDecimal, false can automatically convert the following types (Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false "/>
</javaTypeResolver>
<!-- Generate entity class address -->
<javaModelGenerator targetPackage="com.my.entity" targetProject="src/main/java">
<!-- Whether to use schema as the package suffix -->
<property name= "enableSubPackages" value="false"/>
<!-- Remove leading and trailing spaces from the value returned from the database -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- Generate map.xml file storage address -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成接口dao -->
<javaClientGenerator targetPackage="com.my.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- There can be multiple tables, each table in the database can write a table, tableName indicates the database table to be matched, or you can use % wildcards in the tableName attribute to match all database tables, only matching tables Only will it automatically generate the file EnableSelectByprimarykey corresponding configuration to indicate whether the corresponding interface is generated->
<table tablename = "user_info" enableCountbyexample = "False"
EnableupDatexample = "Falsee "
Enabledeletebyexample =" False
"EnableSelectByexample =" False "
SelectByexampleQueryid =" False ""
enableSelectByPrimaryKey="true"
enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true">
</table>
</context>
</generatorConfiguration>
```
After successful operation, the xml file of entity class dao interface class and mapper can be generated
Spring Data Jpa automatically generates data tables
Create a springboot project and add jpa. I won’t say much about other mysql drivers.
```yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/myblog thymeleaf: cache: false mode: HTML prefix: classpath:/templates/ suffix: .html jpa: hibernate: ddl-auto: update show-sql: true
#Use mybatis-plus to pay attention to the path similar to the following
mybatis: type-aliases-package: com.dreamchaser.pojo mapper-locations: your path
```
The way the entity class is written is very important:
@Entity //Tell jpa that this is an entity class and a data table mapping class
@Table(name = "tbl_user") //Specify which data table corresponds to If the table name is omitted, the class name is lowercase
@Id //This is a primary key
@GeneratedValue(strategy = GenerationType.IDENTITY) //Auto-increment primary key
@Column(name="last_name",length = 50) //The column corresponding to the data table
@Column //Omitting the default column name is the attribute name
handwriting an example
```java
@Entity
//Table(name="myname") default table name test
public class Test(){
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@Column
private String TestName;
}
```
Just start the SpringBoot project
The resource is introduced, and the server cannot bear the traffic