spring---整合Junit,整合web

29 篇文章 0 订阅

整合Junit

l  导入jar包

       基本 :4+1

       测试:spring-test...jar

 

1.让Junit通知spring加载配置文件

2.让spring容器自动进行注入

 

 

l  修改测试类

package com.hcx;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hcx.service.AccountService;

@RunWith(SpringJUnit4ClassRunner.class)  //与spring进行整合
@ContextConfiguration(locations="classpath:applicationContext.xml")  //加载配置文件
public class TestApp {
	
	@Autowired  //自动注入   与junit整合,不需要在spring xml配置扫描
	private AccountService accountService;
	
	@Test
	public void demo01(){
//		String xmlPath = "applicationContext.xml";
//		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//		AccountService accountService =  (AccountService) applicationContext.getBean("accountService");
		accountService.transfer("jack", "rose", 1000);
	}

}

 

 

整合web

 

 

1.导入jar包

spring-web.xml

spring-web-3.2.0.RELEASE.jar

2.tomcat启动加载配置文件

       servlet--> init(ServletConfig) --> <load-on-startup>2

       filter--> init(FilterConfig)  --> web.xml注册过滤器自动调用初始化

       listener--> ServletContextListener --> servletContext对象监听【】

       spring提供监听器 ContextLoaderListener  --> web.xml  <listener><listener-class>....

              如果只配置监听器,默认加载xml位置:/WEB-INF/applicationContext.xml

3.确定配置文件位置,通过系统初始化参数

       ServletContext初始化参数web.xml 

              <context-param>

                     <param-name>contextConfigLocation

                     <param-value>classpath:applicationContext.xml

 

 <!-- 确定配置文件位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置spring 监听器,加载xml配置文件 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

 

4.从servletContext作用域 获得spring容器

// 从application作用域(ServletContext)获得spring容器
		//方式1: 手动从作用域获取
		ApplicationContext applicationContext = 
				(ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		//方式2:通过工具获取
		ApplicationContext apppApplicationContext2 = 
				WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于Spring Boot整合MyBatis和JUnit的问题,可以按照以下步骤进行操作: 1. 首先,确保你已经添加了相关的依赖。在`pom.xml`文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- JUnit 5 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` 2. 创建数据库配置。在`application.properties`文件中添加数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/db_name spring.datasource.username=db_username spring.datasource.password=db_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver # MyBatis配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.domain ``` 3. 创建MyBatis映射配置文件。在`resources`目录下创建`mapper`目录,并编写对应的XML文件,例如`UserMapper.xml`: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <!-- SQL语句定义 --> </mapper> ``` 4. 创建实体类和Mapper接口。在`com.example.domain`包下创建实体类和对应的Mapper接口,例如`User.java`和`UserMapper.java`: ```java // User.java public class User { private Long id; private String name; // 省略getter和setter } // UserMapper.java @Mapper public interface UserMapper { User findById(Long id); // 其他方法定义 } ``` 5. 编写Service层和Controller层代码。按照你的业务需求编写对应的Service和Controller代码。 6. 编写JUnit测试代码。在`src/test/java`目录下创建测试类,例如`UserMapperTest.java`: ```java @SpringBootTest @RunWith(SpringRunner.class) public class UserMapperTest { @Autowired private UserMapper userMapper; @Test public void testFindById() { Long id = 1L; User user = userMapper.findById(id); Assert.assertNotNull(user); System.out.println(user.getName()); } } ``` 以上就是简单的Spring Boot整合MyBatis和JUnit的步骤。根据你的具体需求,可以适当调整代码和配置。希望能对你有所帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值