万金流
以码会友。 吾Q:578751655。 水平有限,轻喷,谢!

思想:

重点:spring boot项目可以配置mybatis必须的内容。默认配置文件为“main/resources/application.properties”(yml为其另一种写法,感兴趣自己去查)

一切编码跟普通的mybatis相同。

注意:

xml文件最好放在资源文件夹resources下面,以便编译时直接复制。

由此引起需要在springboot项目配置文件下指定mapper文件夹位置,详见操作。


操作:

1、官网新建项目:

主要目录结构如下:

 2、编写项目配置文件"application.properties"

 1 # JDBC 驱动
 2 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
 3 # JDBC URL
 4 spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx:xxxx/d1?useSSL=false
 5 # 用户名
 6 spring.datasource.username=root
 7 # 密码
 8 spring.datasource.password=xxxxxxxx
 9 #mapper资源文件
10 mybatis.mapper-locations=classpath:map/*.xml
11 #实体文件位置。因mapper文件中都使用了全限定名,故可以没有。
12 #mybatis.type-aliases-package=hc.entity

前面都好理解。

第10行:编译以后,资源文件夹下的“map文件夹及其内容”会被复制到“target\classes”下面,故需要指定编译后mapper文件的位置。

3、编写实体文件、表操作接口、表操作实现。

实体文件t1.java:

1 package hc.entity;
2 
3 public class t1 {
4     public String xm;
5     public int nl;
6 }

表操作接口t1Mapper.java:

1 package hc.data;
2 import org.apache.ibatis.annotations.Mapper;
3 
4 import hc.entity.t1;
5 @Mapper
6 public interface t1Mapper {
7     t1 getByXm(String xm);
8 }

说明:第五行的Mapper注解必不可少。它除了实现Component注解的容器注册功能,还通知mybatis需要查找实现(xml)

表操作实现t1Mapper.xml:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4   "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="hc.data.t1Mapper">
6   <select id="getByXm" resultType="hc.entity.t1">
7     select * from t1 where xm = #{xm}
8   </select>
9 </mapper>

说明:xml本质上不属于java程序,建议放在资源文件夹下,在编译时自动复制。

4、配置测试文件"C1ApplicationTests.java"

 1 package hc;
 2 
 3 import org.junit.jupiter.api.Test;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.boot.test.context.SpringBootTest;
 6 
 7 import hc.data.t1Mapper;
 8 import hc.entity.t1;
 9 
10 @SpringBootTest(classes=C1Application.class)
11 class C1ApplicationTests {
12     @Autowired
13     t1Mapper tm;
14     @Test
15     void contextLoads() {
16         t1 myt1=tm.getByXm("ls");
17         System.out.println(myt1.xm);
18         System.out.println(myt1.nl);
19     }
20 
21 }

说明:第12行实现t1Mapper对象tm的自动装配, 得益于其接口java文件中的Mapper注解。

5、运行测试

 1 PS C:\Hc\1\Test1> mvn test 
 2 [INFO] Scanning for projects...
 3 [INFO] 
 4 [INFO] ------------------------------< hc:Test1 >------------------------------
 5 [INFO] Building C1 0.0.1-SNAPSHOT
 6 [INFO]   from pom.xml
 7 [INFO] --------------------------------[ jar ]---------------------------------
 8 [INFO] 
 9 [INFO] --- resources:3.3.1:resources (default-resources) @ Test1 ---
10 [INFO] Copying 1 resource from src\main\resources to target\classes
11 [INFO] Copying 1 resource from src\main\resources to target\classes
12 [INFO] 
13 [INFO] --- compiler:3.11.0:compile (default-compile) @ Test1 ---
14 [INFO] Changes detected - recompiling the module! :source
15 [INFO] Compiling 3 source files with javac [debug release 21] to target\classes
16 [INFO] 
17 [INFO] --- resources:3.3.1:testResources (default-testResources) @ Test1 ---
18 [INFO] skip non existing resourceDirectory C:\Hc\1\Test1\src\test\resources
19 [INFO]
20 [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ Test1 ---
21 [INFO] Changes detected - recompiling the module! :dependency
22 [INFO] Compiling 1 source file with javac [debug release 21] to target\test-classes
23 [INFO] 
24 [INFO] --- surefire:3.0.0:test (default-test) @ Test1 ---
25 [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
26 [INFO] 
27 [INFO] -------------------------------------------------------
28 [INFO]  T E S T S
29 [INFO] -------------------------------------------------------
30 [INFO] Running hc.C1ApplicationTests
31 
32   .   ____          _            __ _ _
33  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
34 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
35  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
36   '  |____| .__|_| |_|_| |_\__, | / / / /
37  =========|_|==============|___/=/_/_/_/
38  :: Spring Boot ::                (v3.1.4)
39 
40 2023-10-04T23:00:09.707+08:00  INFO 7968 --- [           main] hc.C1ApplicationTests                    : Starting C1ApplicationTests using Java 21 with PID 7968 (started by Morning in C:\Hc\1\Test1)
41 2023-10-04T23:00:09.710+08:00  INFO 7968 --- [           main] hc.C1ApplicationTests                    : No active profile set, falling back to 1 default profile: "default"   
42 2023-10-04T23:00:11.314+08:00  INFO 7968 --- [           main] hc.C1ApplicationTests                    : Started C1ApplicationTests in 1.916 seconds (process running for 2.825)
43 Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
44 WARNING: A Java agent has been loaded dynamically (C:\Users\Morning\.m2\repository\net\bytebuddy\byte-buddy-agent\1.14.8\byte-buddy-agent-1.14.8.jar)
45 WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
46 WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
47 WARNING: Dynamic loading of agents will be disallowed by default in a future release
48 2023-10-04T23:00:12.181+08:00  INFO 7968 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
49 2023-10-04T23:00:12.448+08:00  INFO 7968 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@3de56885
50 2023-10-04T23:00:12.450+08:00  INFO 7968 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
51 ls
52 18
53 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.537 s - in hc.C1ApplicationTests
54 [INFO] 
55 [INFO] Results:
56 [INFO]
57 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
58 [INFO]
59 [INFO] ------------------------------------------------------------------------
60 [INFO] BUILD SUCCESS
61 [INFO] ------------------------------------------------------------------------
62 [INFO] Total time:  6.936 s
63 [INFO] Finished at: 2023-10-04T23:00:12+08:00
64 [INFO] ------------------------------------------------------------------------

在第51、52行能看到测试的输出结果。

(完)

posted on 2023-10-31 23:37  万金流  阅读(545)  评论(0编辑  收藏  举报