مقدمة:
في الماضي ، عندما كنا نقوم ببناء مشروع ، قمنا إما ببناء جميع الحزم في مشروع واحد ، والذي كان مناسبًا حقًا عند معالجة المراجع. لا تقلق ، لا يمكن تكوين أو قراءة بعض الأشياء.
أو افصل الحزمة في مشروع أو مشروع فرعي. في هذا الوقت ، لا تزال المعالجة المرجعية في المشروع مزعجة بعض الشيء. ومع ذلك ، فإن الفوائد أكثر ، لذلك لن أعبر عنها بعد الآن.
في Idea ، يوصى باستخدام مشاريع البناء متعددة الوحدات بدلاً من الطريق في Eclipse. بعد ذلك ، حاول تقسيم مشروع Springboot إلى وحدة فرعية لمعرفة كيفية عمله.
انقسام المشروع:
1. تغييرات الدليل
2. مشروع الوالدين
من الناحية النظرية ، يجب أن يكون pom.xml الأصل قيدًا على المراجع في مشاريع الأطفال ، وخاصة قيود الإصدار.
لذلك ، في POM.xm الأصل ، يجب استخدام الإدارة التبعية لتقييد إصدار حزمة JAR في الحجم الفرعي.
بعد ذلك ، للمراجع المفيدة لبعض المشروعات الفرعية ، يمكنك ذكرها في المشروع الأصل.
<؟ XSI: schemalocation = "http://maven.apache.org/Pom/4.0.0 <soph> 0.0.1-snapshot </version> <packaging> pom </packaging> <name> parent </same> project Demo Project for Spring Boot </description> <parent> <roupiD> org.springframework.boot </groupaid> البحث عن الوالد من المستودع-> </parent> <modules> <module> pojo </somule> <الوحدة النمطية> web </somule> <module> dao </somed> <module> خدمة </module> <module> simple </project <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> <crouid.version> 1.1.3 </druid.version> <Mysql.connector.java.verse> 5.1.44 </mysql.connector.java.versa> </properties> <RependencyManagement> <reperence> <reperence> <rougiD> com.alibaba </groupiD> <ProwerId> org.mybatis.spring.boot </rougeid> <StifactId> mybatis-spring-boot-starter </shintifactid> <sophy> $ {mybatis.boot.starter.version} </version> <StifactId> mysql-connector-java </stifactid> <الإصدار> $ {mysql.connector.java.version} </version> </sependency> </respency> </sempendency <StifactId> Spring-Boot-Starter-Web </stifactid> </sependency> <reperence> <roupiD> org.springframework.boot </rougeid> <shintifactid> <roupringid> org.springframework.boot </rougiD> <StifactId> Spring-boot-maven-plugin </stifactid> </spliexin> </sultiins> </build> </project>3.
3.1 pom.xml
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 <soph> 0.0.1-snapshot </version> </paret> <roupiD> cn.elvinle </groupId> <StifactId> pojo </shintifactid> <sored> 0.0.1-snapshot </version> <backaging> jar </packaging> <same> pojo> <speciver <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> <roupringid> org.springframework.boot </rougiD> <StifactId> Spring-boot-maven-plugin </stifactid> </spliexin> </sultiins> </build> </project>
3.2 ديسيبل كيان
حزمة cn.elvinle.pojo ؛/** * Author: elvin */public class user {private int id ؛ اسم السلسلة الخاصة ؛ public int getId () {return id ؛ } public void setId (int id) {this.id = id ؛ } السلسلة العامة getName () {return name ؛ } public void setName (اسم السلسلة) {this.name = name ؛ }}بالنسبة للمحتوى في Pojo و Dao ، يمكنك استخدام MyBatis Reverse Engineering لإنشاءه ، مما قد يوفر الكثير من المتاعب.
4
4.1 pom.xml
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 <soph> 0.0.1-snapshot </version> </paret> <roupiD> cn.elvinle </rougeid> <StifactId> dao </shintifactid> <sored> 0.0.1-snapshot </version> <backaging> jar </packaging> <name> dao </name> demo project </spring> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputing> <java.version> <roupiD> cn.elvinle </rougiD> <StifactId> pojo </stifactid> <soph> 0.0.1-snapshot </version> </sependency> <!-مرجع خارجي-> <!-https://mvnrepository.com/artifact/com.aliba/druid-> <! <StifactId> druid </stifactid> </sependency> <reperence> <roupiD> mysql </rougiD> <intifactid> mysql-connector-java </stifactid> </reperency> <redenced> <rouplencid> org.mybatis.spring.boot </rougeid> <StifactId> mybatis-spring-boot-starter </artifactId> </reperency> </premitency> <build> <ult plugins> <ultipin> <roupled> org.springframework.boot </ropractor> </spraction> </spriction>
4.2 MAPPER
usermapper.java:
الواجهة العامة usermapper {قائمة عامة <Seter> getall () ؛}usermapper.xml:
<؟ مساحة الاسم = "cn.elvinle.dao.mapper.usermapper"> <select id = "getall" resulttype = "cn.elvinle.pojo.user"> حدد * من المستخدم </select> </mapper>
5
5.1 pom.xml
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 <soph> 0.0.1-snapshot </version> </inpart> <roupiD> cn.elvinle </groupId> <StifactId> خدمة </artifactid> <sored> 0.0.1-snapshot </version> <backaging> جرة </packaging> <name> خدمة </name> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> <StifactId> pojo </stifactid> <soph> 0.0.1-snapshot </version> </reperence> </repreadencies> <build> <clupins> <roupiD> org.springframewwork.
5.2 Userveservice.java
الواجهة العامة UsperService {القائمة العامة <Seter> getAll () ؛}6. المشروع الفرعي - البساطة
6.1 pom.xml
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 <soph> 0.0.1-snapshot </version> </inpart> <roupiD> cn.elvinle </rougeid> <StifactId> Simple </shintifactid> <sored> 0.0.1-snapshot </version> <backaging> جرة </packaging> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> <StifactId> pojo </stifactid> <soph> 0.0.1-snapshot </version> </reperency> <reperency> <roupiD> cn.elvinle </groupId> <SefActID> خدمة </groupid> <splex> <StifactId> dao </suntifactid> <الإصدار> 0.0.1-snapshot </version> </premotency> </sependency> </sependency> </busticen> <clupins> <roupiD> org.springframework.boot </groupiD>
6.2 usersimpl
servicepublic class useredImpl تنفذ userversevice {Autowired usermapper usermapper ؛ Override Public List <Sether> getAll () {system.out.println ("usersimpl getall") ؛ إرجاع usermapper.getAll () ؛ }}7. المشاريع الفرعية - الويب
المكان الرئيسي هنا.
7.1 pom.xml
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 <soph> 0.0.1-snapshot </version> </inpart> <roupiD> cn.elvinle </groupId> <StifactId> web </suntifactid> <sored> 0.0.1-snapshot </version> <backaging> جرة </packaging> web </name> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> <StifactId> Simple </shintifactid> <الإصدار> 0.0.1-snapshot </version> </sependency> </sependencies> <build> <ultipins> <roupl> org.springframework.
7.2 application.yml
الخادم: مسار السياق:/المنفذ الأصل: 8080MYBATIS: config location: /mapper/*.
7.3 وحدة تحكم
@restController @requestMapping ("API") الفئة العامة apicontroller {Autowired Private UsperService UsperService ؛ requestmapping ("index") قائمة عامة <Sether> index () {list <served> all = userservice.getall () ؛ إرجاع كل شيء ؛ }}حتى الآن ، لا شيء مميز ، تم تعديله بشكل طبيعي. بعد ذلك ، ستكون هناك اختلافات من تلك التي لا تحتوي على وحدات.
7.4 تعديل عند المدخل
componentscan ({"cn.elvinle"})@SpringBootApplicationPublicpublicpublication webapplication {public static void main (string [] args) {springapplication.run (webapplication.class ، args) ؛ }}في هذا الوقت ، إذا تم تشغيل البرنامج مباشرة ، فلن يتم الوصول إليه بنجاح. والسبب هو أن تكوين MyBatis الآلي وإنشاء الأتمتة غير مدعوم في وحدات متعددة.
في هذا الوقت ، نحتاج إلى تكوين وإنشاء يدويًا.
7.5 تكوين MyBatis Java
<!-MyBatis Factory-> <bean id = "sqlsessionfactory"> <property name = "dataSource" ref = "datasource"/> <property name = "configlocation" value = "classpath: mampatis/sqlmapconfig.xml"/> <! value = "classpath: mapper /*. value = "sqlsessionfactory"/> </ban>
يمكنك استخدام تكوين Java لمقارنة ملف التكوين أعلاه
mybatisconfig:
ConfigurationPublic Class MyBatisconfig {value ("$ {myBatis.Config-location}") سلسلة خاصة mapperlocationPattern ؛ bean @configurationProperties (prefix = "spring.datasource") datasource dataSource () } bean (name = "sqlsessionfactory") sqlsessionfactory sqlsessionfactory () يلقي الاستثناء {sqlsessionfactorybean sqlsessionfactorybean = sqlsessionfactorybean الجديد () ؛ sqlsessionfactorybean.setDatasource (datasource ()) ؛ pathmatchingResourCesournResolver Resolver = New PathMatchingResourCednResolver () ؛ sqlsessionfactorybean.setMapperLocations (Resolver.getResources (MapPerLocationPattern)) ؛ إرجاع sqlsessionfactorybean.getObject () ؛ }}myBatismappersCannerConfig:
@configuration@autoconfigureafter (mybatisconfig.class) mapperscan ( mapperscannerconfigurer () ؛ mapperscannerconfigurer.setsqlsessionfactorybeanname ("sqlsessionFactory") ؛ mapperscannerconfigurer.setBasePackage ("cn.elvinle.dao.mapper") ؛ إرجاع mapperscannerconfigurer ؛ }}حسنًا ، هنا يمكنك تشغيل البرنامج ومشاهدة النتائج:
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.