1. idea to create a maven project
1-1: delete
src
,target
directory, keep onlypom.xml
1-2: The root directory
pom.xml
can be inherited by submodules, so the project is just a demo, without considering too many performance issues, so many dependencies都写在根级`pom.xml`,子模块只需继承就可以使用。
1-3: Root-level
pom.xml
files are in appendix 11-4: Dependent modules mybatis spring-boot related modules
2. Create a submodule (module)
2-1:
file > new > module
Inputmodel
2-2:
file > new > module
Inputdao
2-3:
file > new > module
Inputservice
2-4:
file > new > module
Inputwebapi
3. Modify the submodule pom.xml configuration
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>parent</artifactId> <groupId>com.luyh.projectv1</groupId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>projectv1-model</artifactId> </project>
Note: <font color="red"> <relativePath>../pom.xml</relativePath>
</font> This section must be added to inherit the parent module
At this point, the basic structure of the project is completed, and then you can come to the code, oh wait, let me introduce the job responsibilities of each sub-module first.
4. The 'job responsibilities' of submodules in the project
model
This module stores all entity classesdao
This module stores the specific implementation of data interaction for service callsservice
This module stores the business code implementation for the API layer to callwebapi
This module can also not appear in the project. In order to write demo, the webapi layer is put in
5. model
Layer entity class writing
build package name
com.luyh.projectv1.model
Please clone my git for the specific code of the entity class
Member.java
, the git address is at the bottom
6. dao
Layer database operation layer
Create com.luyh.projectv1.dao.config, there are only 2 configuration java classes in this package that allow spring boot to automatically load the configuration
Create MemberMapper.java See the code for specific content
Create MemberMapper.xml under resources/mybatis
Build IMember.java
Create Member.java to implement the Imember interface
Create a resources/application.properties file for configuring database connections
7. service
编写业务逻辑
建立
com.luyh.projectv1.service
包建立IMemberService.java接口
建立MemberService.java实现类
MemberService.java 类中自动注入DaoMember 并调用其方法获取数据
8. webapi
编写webapi获取json数据
建立Application.java 启动应用
建立
com.luyh.projectv1.webapi.controller.MemberController.java
写个rest风格Controller启动
附录1
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.luyh.projectv1</groupId> <artifactId>parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <modules> <module>model</module> <module>dao</module> <module>service</module> <module>webapi</module> </modules> <!--申明依赖关系--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <!--设置maven仓库--> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project>
转载自:https://segmentfault.com/a/1190000005020589