ملخص
في الوقت الحاضر ، لا تزال معظم تطبيقات الإنترنت تستخدم MyBatis لتشغيل قواعد البيانات. يقدم هذا المقال كيفية دمج MyBatis في SPRING BOOT.
تقدم المقالة السابقة طريقة تشغيل المشاريع مباشرة في Spring Boot مع JAR. إذا كنت بحاجة إليها ، فانقر لعرضه.
إنشاء مشروع التمهيد الربيع
في فتحة التمهيد الربيع - إنشاء مقالة وتشغيلها ، يوجد قسم يصف كيفية استخدام مكونات Boot Spring لإنشاء مشاريع. إذا كنت ترغب في دمج MyBatis ، فأنت بحاجة فقط إلى التحقق من المكونين MySQL و MyBatis.
بالطبع ، يمكنك أيضًا إضافة تبعيات مباشرة في ملف pom.xml بدون هذه الطريقة. اخترت إضافة تبعيات مباشرة في ملف pom.xml.
التبعية> <rougiD> org.mybatis.spring.boot </groupId> <intifactid> mybatis-spring-boot-starter </stifactid> <الإصدار> 1.3.1 </version> </rependency> <sperency> <soph> 5.1.34 </version> </sependency> <reperency> <roupiD> com.alibaba </rougeid> <StifactId> druid </stifactid> <الإصدار> 1.1.7 </version> </respency>
يستخدم مصدر البيانات Druid من Alibaba. محتوى ملف pom.xml الكامل هو كما يلي:
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <Dodelvent> 4.0.0 </sodelverversion> <soph> 0.0.1-snapshot </version> <packaging> جرة </packaging> <name> الدراسة </name> <description> مشروع تجريبي لـ Spring Boot </description> <parent> <roupiD> org.springframework.boot </groupaD> <!-Lookup Parent from ropository-> </paret> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.utputencoding> <Eperenceies> <Rependency> <roupiD> org.mybatis.spring.boot </rougiD> <StifactId> mybatis-spring-boot-starter </stifactid> <sophy> 1.3.1 </version> </premitency> <sperency> <roupled> org.springframework.boot </grouped> <StifactId> Spring-Boot-Starter-Web </stifactid> </sependency> <reperence> <roupiD> mysql </rougled> <StifactId> mysql-connector-java </ropliD> <splement> <StifactId> druid </stifactid> <soph> 1.1.7 </version> </sependency> <reperence> <roupiD> com.alibaba </rougeid> <StifactId> fastjson </stifactid> <splement> 1.2.45 </version> <StifactId> Spring-boot-starter-test </artifactid> <scope> test </scope> </reperence> </sependencies> <build> <lopins> <roupl> org.springframewwork.
إنشاء جدول
قم بإنشاء جدول `user` (`id` bigint (20) وليس null auto_increment ،` name` varchar (30) not null default '، key key (`id`)) engine = innodb auto_increment = 8 default charset = utf8 comment =' معلومات المستخدم '؛
إنشاء كيان
package com.springboot.entity ؛ مستخدم الفئة العامة {private long id ؛ اسم السلسلة الخاصة ؛ Public Long getId () {return id ؛ } public void setId (id long) {this.id = id ؛ } السلسلة العامة getName () {return name ؛ } public void setName (اسم السلسلة) {this.name = name ؛ } Override public string toString () {return "user {" + "id =" + id + "، name = '" + name +'/' +'} '؛ }}إنشاء ملفات رسم خرائط myBatis وإعادة الريبو
userrepo.java
package com.springboot.repo ؛ استيراد com.springboot.entity.user ؛ استيراد org.apache.ibatis.annotations.mapper ؛ استيراد org.springframework.stereotype.component ؛ import java.util.list ؛@mapperpublic interface. user selectByPrimaryKey (معرف طويل) ؛ int updateByPrimaryKey (مستخدم المستخدم) ؛ int deleteByPrimaryKey (id id) ؛ قائمة <Sether> Selectall () ؛}
usermapper.xml
<؟ <resultmap id = "baseresultmap" type = "com.springBoot.entity.user"> <id column = "id" property = "id" id = "selectByPrimaryKey" resultMap = "BaseresultMap" parametertype = "java.lang.long"> SELECT <include refid = "base_column_list" /> من المستخدم حيث id = #{id ، jdbctype = Bigint} refid = "base_column_list"/> من المستخدم </select> <update id = "updateByPrimaryKey" parametertype = "com.springboot.entity.user"> update <set> <test = "name! #{id ، jdbctype = bigint} </update> <delete id = "deletebermarykey" parametertype = "java.lang.long"> delete من user where id = #{id ، jdbctype = bigint} </delete> <insert id = "insert" parametertype = usegeneratedKeys = "true" keyproperty = "id"> insert في المستخدم <trim previx = "(" suffix = ")" suffixoverrides = "،"> name </trim> <trim prefix = "dorder (" suffix = ")" suffixoverrides = "،"اكتب application.properties
أضف ما يلي إلى ملف Application.Properties الذي تم إنشاؤه بواسطة SPRING BOOT بالنسبة لنا:
spring.datasource.name = spring_boot_study spring.datasource.url = jdbc: mysql: // localhost: 3306/test spring.datasource.username = com.datasource spring.datasource.type = com.alibaba.druid.pool.druiddatasource mybatis.mapper-locations = classpath: mapper/*
اختبار الوحدة
package com.springboot ؛ import com.springboot.entity.user ؛ import com.springboot.repo.userrepo ؛ import org.junit org.springframework.boot.test.context.springBoottest ؛ استيراد org.springframework.test.context.junit4.springRunner ؛ eSpress java.util.list ؛ runwith (springrunner.class) test public void testInsert () {user user = new user () ؛ user.setName ("test2") ؛ userrepo.insert (المستخدم) ؛ } test public void testUpDate () {user user = new user () ؛ user.setid (6L) ؛ user.setName ("test3") ؛ userrepo.updateByPrimaryKey (المستخدم) ؛ } test public void testDelete () {userrepo.deleteByPrimaryKey (6l) ؛ } test public void testselectbyprimarykey () {user user = userrepo.selectbyprimarykey (7L) ؛ System.out.println (user) ؛ } test public void testselectall () {list <serve> userList = userrepo.selectall () ؛ System.out.println (userlist) ؛ }}لخص
ما ورد أعلاه هو رمز المثال (الإصدار المبسط) لتكامل التمهيد الربيعي الذي قدمه لك MyBatis. آمل أن يكون ذلك مفيدًا لك. إذا كان لديك أي أسئلة ، فيرجى ترك رسالة لي وسوف يرد المحرر إليك في الوقت المناسب. شكرا جزيلا لدعمكم لموقع wulin.com!