Préface:
Dans le passé, lorsque nous construisions un projet, nous avons construit tous les packages d'un projet, ce qui était vraiment pratique lors du traitement des références. Ne vous inquiétez pas, certaines choses ne peuvent pas être configurées ou lues.
Ou, séparez le package en un projet ou un sous-projet. À l'heure actuelle, le traitement de référence dans le projet est encore un peu gênant. Cependant, les avantages sont plus, donc je ne l'exprimerai plus.
Dans l'idée, il est recommandé d'utiliser des projets de construction multi-modules au lieu de la voie dans Eclipse. Ensuite, essayez de diviser un projet Springboot en un sous-module pour voir comment cela fonctionne.
Split du projet:
1. Modifications du répertoire
2. Projet parent
En théorie, Parent Pom.xml devrait être une contrainte sur les références dans les projets enfants, principalement des contraintes de version.
Par conséquent, dans le parent pom.xm, la gestion de dépendance doit être utilisée pour contraindre la version du package JAR dans le sous-projet.
Ensuite, pour les références utiles pour certains sous-projets, vous pouvez les mentionner dans le projet parent.
<? 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: ScheMalation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modersversion> 4.0.0 </ Modelversion> </prôlid> CN.ELVINLE </prouverId> <ArtifActid> Parent </ ArtiFact> CN.ELVINLE </prouverID> <version> 0.0.1-snapshot </ version> <packaging> pom </packaging> <name> parent </name> <description> Projet de démonstration pour Spring Boot </ Description> <Artifactid> Spring-Boot-starter-Parent </ ArtifActid> <! Lookup Parent from Repository -> </parent> <odules> <odule> pojo </odule> <odule> web </ module> <odule> dao </odule> </ module> Service </ module> </ module> Simpl </codule> </dudules> <properties> <pandbuild.sourceencoming> utf-8 </prorst. <project.reportting.outputencoding> utf-8 </project.reportting.outputencoding> <java.version> 1.8 </java.version> <druid.version> 1.1.3 </druid.version> <mybatis.boot.starter.version> 1.3.1 </mybatis.boot.starter.version> 1.3.1 </mybatis.boot.starter.version> <mysql.connector.java.version> 5.1.44 </mysql.connector.java.version> </properties> <dedentencymanagement> <dependency> <dependency> <proupId> com.alibaba </proncId> <Artifactid> druid </ artifactid> <version> $ {druid.version} <GroupId> org.mybatis.spring.boot </rompuprid> <ArtifactId> mybatis-spring-boot-starter </retifactid> <version> $ {mybatis.boot.starter.version} </-version> </ Dependency> <Dedidency> <proupId> Mysql </prouventid> <ArtefactId> MySQL-Connector-Java </ Artifactid> <Dersion> $ {mysql.connector.java.version} </ version> </Dendency> </Dependency> </Dedencymanagement> <! - Déclare les dépendances -> <Detences> <Dependance> <proupId> Org.SpringFramework. <ArtefactId> Spring-Boot-starter-web </ artifactid> </dependance> <dependency> <proupId> org.springframework.boot </proupId> <Artifactid> Spring-Boot-Starter-Test </ ArfactId> </Dependency> </Dependency> </Dedency> </Duild> <gluns> <GroupId> org.springframework.boot </prôdId> <ArtefactId> Spring-Boot-Maven-Plugin </ ArfactId> </Glugin> </Glugins> </ Build> </ Project>3. Sous-projection - Pojo
3.1 pom.xml
<? 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 </dérapifion> </rom groupent> CN.ELVINLE </prenid> <artifact> </proulid> cn.elvinle </proncID> <version> 0.0.1-snapshot </ version> </parent> <proupId> cn.elvinle </prôdId> <Artifactid> pojo </letifactid> <version> 0.0.1-snapshot </preinte> <packaging> jar </packaging> <name> pojo </name> <description> Demo Project for Spring Boot </ Description> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reportting.outputencoding> utf-8 </project.reportting.outputending> <java.version> 1.8 </java.version> </ / Properties> <uild> <glunsion> <GroupId> org.springframework.boot </prôdId> <ArtefactId> Spring-Boot-Maven-Plugin </ ArfactId> </Glugin> </Glugins> </ Build> </ Project>
3,2 DB ENTITY
package cn.elvinle.pojo; / ** * @author: elvin * / public class utilisateur {private int id; nom de chaîne privé; public int getID () {return id; } public void setid (int id) {this.id = id; } public String getName () {Nom de retour; } public void setName (string name) {this.name = name; }}Pour le contenu de Pojo et DAO, vous pouvez utiliser MyBatis inverse ingénierie pour le générer, ce qui peut éviter beaucoup de problèmes.
4. Sous-projection de dao
4.1 pom.xml
<? 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 </dérapifion> </rom groupent> CN.ELVINLE </prenid> <artifact> </proulid> cn.elvinle </proncID> <version> 0.0.1-snapshot </-version> </parent> <proupId> cn.elvinle </prôdId> <ErtifactId> dao </letifactid> <version> 0.0.1-snapshot </ version> <packaging> jar </packaging> <name> dao </name> <description du projet de démo pour Spring Boot </, description> <polging <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reportting.outputencoding> utf-8 </project.reportting.outpudencoming> <java.version> 1.8 </java.version> </properties> <pèbres> <! <GroupId> CN.ELVINLE </PROUPID> <ARFACTID> POJO </ ARTIFACTID> <DERSE> 0.0.1-SNAPSHOT </ Version> </ Dependency> <! - Référence externe -> <! <ArtefactId> Druid </ artifactid> </Dependency> <Dependency> <GroupId> MySQL </ProupId> <ArtefactId> MySQL-Connector-Java </ Artifactid> </Dependency> <Dependance> <ProupId> Org.mybatis.spring.boot </roulid> <ArtefactId> Mybatis-Spring-Boot-Starter </ Artifactid> </Dependency> </Dependency> <Duild> <Glugins> <Glugin> <ProupId> org.SpringFramework.boot </prôdId> <Artifactid> Spring-Boot-Maven-Plugin </Rifactid> </GluciN> </Glugins>
4.2 Mappeur
UserMapper.java:
Interface publique UserMapper {public List <Serser> getall ();}UserMapper.xml:
<? xml version = "1.0" Encoding = "UTF-8"?> <! Doctype Mappep Public "- // Mybatis.org//dtd Mappen 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace = "cn.elvinle.dao.mapper.usermapper"> <select id = "getall" resultType = "cn.elvinle.pojo.user"> SELECT * From User </lect> </caper>
5. Sous-service
5.1 pom.xml
<? 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 </dérapifion> </rom groupent> CN.ELVINLE </prenid> <artifact> </proulid> cn.elvinle </proncID> <version> 0.0.1-snapshot </-version> </parent> <proupId> cn.elvinle </prôdId> <ArtifActid> Service </ artifactId> <version> 0.0.1-snapshot </ version> <packaging> jar </packaging> <name> Service </name> <description> Demo Project for Spring Boot </, description> <packageties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reportting.outputencoding> utf-8 </project.reportting.outputending> <java.version> 1.8 </java.version> </properties> <preascies> <dependence> <ArtefactId> Pojo </ ArfactId> <Sease> 0.0.1-Snapshot </DERNIERSE> </DENDENCENCE> </DENDEFENCES> <Duild> <Glugins> <GroupId> Org.SpringFramework.boot </prouprid> <eRtifactid> Spring-Booot-Maven-Plugin </RetifActid> </GluciN>
5.2 UserService.java
Interface publique UserService {public List <Serser> getall ();}6. Sous-projection - Simpl
6.1 pom.xml
<? 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 </dérapifion> </rom groupent> CN.ELVINLE </prenid> <artifact> </proulid> cn.elvinle </proncID> <version> 0.0.1-snapshot </-version> </parent> <proupId> cn.elvinle </prôdId> <Artifactid> Simplt </letifactid> <version> 0.0.1-snapshot </ version> <packaging> jar </packaging> <name> Simppl </name> <Description> Demo Project for Spring Boot </, description> <poltingties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reportting.outputencoding> utf-8 </project.reportting.outputending> <java.version> 1.8 </java.version> </properties> <preascies> <dependence> <ArtefactId> Pojo </ ArfactId> <Dersion> 0.0.1-Snapshot </DERNIERSE> </DENDENCENCE> <Dependance> <ProupID> CN.ELVINLE </proupId> <TefactId> Dao </ Artifactid> <Dersion> 0.0.1-Snapshot </DERNIFRODAGE> </DENDENCENCY> </ Dependency> </Dependency> </ Build> <Glugins> <GroupId> Org.SpringFramework.Boot </proncId> <Artifactid> Spring-Boot-Maven-Plugin </ Artifactid> </ragin> </proffin>
6.2 utilisateurs imprimables
@ServicePublic Class UsersIMPL implémente UserService {@Autowired Private UserMapper UserMapper; @Override public list <serv> getall () {System.out.println ("UsersImplt getall"); retourner userMapper.getall (); }}7. Sous-projection - Web
L'endroit clé est ici.
7.1 pom.xml
<? 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 </dérapifion> </rom groupent> CN.ELVINLE </prenid> <artifact> </proulid> cn.elvinle </proncID> <version> 0.0.1-snapshot </-version> </parent> <proupId> cn.elvinle </prôdId> <ArtifActid> web </ artifactId> <version> 0.0.1-snapshot </ version> <packaging> jar </packaging> <name> web </name> <description> Demo Project for Spring Boot </, description> <packagingties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reportting.outputencoding> utf-8 </project.reportting.outputending> <java.version> 1.8 </java.version> </properties> <preascies> <dependence> <ArtefactId> Simppl </ artifactId> <DERSE> 0.0.1-snapshot </-version> </ Dependency> </Dependces> <Duild> <Glugins> <GroupId> org.springFramework.boot </prôdId> <Artinsactid> Spring-Booot-Maven-Plugin </ Artifactid> </GluciN> </gardins>
7.2 Application.yml
Server: context-Path: / Port Port: 8080Mybatis: config-location: /mapper/*.xmlspring: dataSource: nom d'utilisateur: mot de passe root: URL root: jdbc: mysql: // localhost: 3306 / shop? useunicode = true & caractéristique = utf-8 pilote-cllass-name: com.mysql.jdbc.Diverat
7.3 Contrôleur
@ RestController @ requestmapping ("API") classe publique APIController {@Autowired Private UserService UserService; @RequestMapping ("index") public List <Derser> index () {list <ser utilisateur> all = userService.getall (); retourner tous; }}Jusqu'à présent, rien de spécial, il a été modifié normalement. Ensuite, il y aura des différences par rapport à celles sans modules.
7.4 Modification à l'entrée
@ComponentScan ({"cn.elvinle"}) @ SpringbootApplicationPublic class webApplication {public static void main (String [] args) {SpringApplication.Run (webApplication.class, args); }}Pour le moment, si le programme est exécuté directement, il ne sera pas accessible avec succès. La raison en est que la configuration automatisée et la création d'automatisation de MyBatis ne sont pas prises en charge dans plusieurs modules.
Pour le moment, nous devons configurer et créer manuellement.
7.5 Mybatis Java Configuration
<! - MyBatis Factory -> <bean id = "sqlSessionFactory"> <propriété name = "dataSource" ref = "dataSource" /> <propriété name = "configLocation" value = "classpath: mybatis / sqlmapconfig.xml" /> <! - Mapping mapping.xml fichier -> <! Value = "ClassPath: Mapper / *. Xml" /> -> </ Bean> <! - Le nom du package où se trouve l'interface DAO, Spring trouvera automatiquement la classe sous elle -> <Bean Id = "MAPPERSCANNERCONFIGURER"> <Propriété Name = "BASEPACKAGE" Value = "SqlSessionFactory" /> </EAN>
Vous pouvez utiliser la configuration Java pour comparer le fichier de configuration ci-dessus
Mybatisconfig:
@Configurationpublic class MyBatiSConfig {@Value ("$ {mybatis.config-location}") String private mapperlocationpattern; @Bean @configurationproperties (prefix = "spring.datasource") public dataSource dataSource () {return new com.alibaba.druid.pool.druiddatasource (); } @Bean (name = "sqlSessionFactory") public sqlSessionFactory sqlSessionFactory () lève une exception {sqlSessionFactoryBean sqlSessionFactoryBean = new SQLSessionFactoryBean (); SQLSessionFactoryBean.SetDataSource (DataSource ()); PathMatchingResourcePatterNResolver Resolver = new PathMatchingResourcePatterNResolver (); SqlSessionFactoryBean.SetMapperLocations (Resolver.getResources (MapperLocationPattern)); return sqlSessionFactoryBean.getObject (); }}MybatispperscannerConfig:
@ Configuration @ AutoConfiGurefter (mybatisconfig.class) @mapperscan ("cn.elvinle.dao.mapper") public MybatisperscannerConfig {public MapperscannerConfiger MapperscannerConnerConiGur = New MAPPERScannerConfigurer (); MAPPERSCANNERCONFIGURER.SETSQLSESSESSEFACTORYBEAnNAME ("SqlSessionFactory"); MAPPERSCANNERCONFIGURER.SETBASEPACKAGE ("CN.ELVINLE.DAO.MAPPER"); retour MAPPERScannerConfigurer; }}Ok, ici, vous pouvez exécuter le programme et voir les résultats:
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.