发表于: 2022-04-19 19:38:15
1 595
尝试测试
1、创建sping项目,配置applicationContext.xml
代码片段:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--把testDao对象的创建权交给Spring-->
<bean id="testDao" class="dao.TestDaoImpl"></bean>
</beans>
2、创建test包,在test包下创建测试类SpringTest
package test;
import dao.TestDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
public class SpringTest {
@Test
public void demo(){
//初始化Spring容器ApplicationContext,加载配置文件
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取testDao实例
TestDao testDao = (TestDao) application.getBean("testDao");
testDao.sayHello();
}
}
创建dao包,在dao包下创建TestDao接口和TestDao接口的实现类
Test接口代码片段:
package dao;
public interface TestDao {
public void sayHello();
}
TestDogimpl代码片段:
package dao;
public class TestDaoImpl implements TestDao {
@Override
public void sayHello() {
System.out.println("Hello,Spring!");
}
}
测试运行结果:
四月 19, 2022 1:48:29 下午 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
四月 19, 2022 1:48:29 下午 com.mchange.v2.c3p0.C3P0Registry
信息: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Hello,Spring!
创建SpringBoot项目
创建完成测试时,jdk版本问题导致测试失败。
19:31 All files are up-to-date
19:31 Error running 'MybitisApplication': No jdk for module 'mybitis'
19:31 All files are up-to-date
19:31 Error running 'MybitisApplication': No jdk for module 'mybitis'
19:31 All files are up-to-date
19:31 Error running 'MybitisApplication': No jdk for module 'mybitis'
jdk模块缺失。
补回jdk模块
将pom文件代码片段补全:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>mybitis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybitis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
测试结果:
对于基础的知识了解的还是够全面,很多的基础经常出现错误。
评论