Day12(junit与spring整合、TestsNginx与spring整合、jdbcTemplate与spring整合、springMVC对web工程支持)

19 篇文章 0 订阅
16 篇文章 0 订阅

junit与spring 整合

	<properties>
		<org.springframework-version>5.1.9.RELEASE</org.springframework-version>
	</properties>		
<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-test</artifactId> 
			<version>${org.springframework-version}</version>
			<scope>provided</scope> 
		</dependency>

junit固定写法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cAYskukV-1590736794985)(Day12.assets/image-20200511124448698.png)]

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.testfan.ioc.User;

@RunWith(SpringJUnit4ClassRunner.class)//junit整合spring的 测试//立马开启了spring的注解
@ContextConfiguration(locations= {"classpath:spring.xml"})// 加载核心配置文件 classpath:
public class JunitTest {

	@Autowired
	private User user;
	
	@Test
	public void name() {
		System.out.println(user);
	}
}

TestNg与spring整合

	<properties>
		<org.springframework-version>5.1.9.RELEASE</org.springframework-version>
	</properties>		
		<!-- Test -->
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>7.0.0</version>
		</dependency>
		
		<dependency> 
			<groupId>org.springframework</groupId> 
			<artifactId>spring-test</artifactId> 
			<version>${org.springframework-version}</version>
			<scope>provided</scope> 
		</dependency>

TestNg固定写法,读取xml信息且继承AbstractTestNGSpringContextTests

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WN2KSGWx-1590736794988)(Day12.assets/image-20200511125345956.png)]

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import com.testfan.ioc.User;

@ContextConfiguration(locations= {"classpath:spring.xml"})
public class TestNgSpring extends AbstractTestNGSpringContextTests{
	@Autowired
	private User user;
	
	@Test
	public void name() {
		System.out.println(user);
	}
}

jdbcTemplate与spring整合

springmvc项目下载地址

链接:https://pan.baidu.com/s/1IPOhh8gZy1gocd6LfIkVKA
提取码:w9k3

        <dependency>  
       <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
          <version>${org.springframework-version}</version>
        </dependency> 

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.18</version>
		</dependency>

		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.4</version>
		</dependency>

需要结合配置文件一起使用

jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://118.24.13.38\:3308/goods?characterEncoding=utf-8&useSSL=false
jdbc.username=zhangsan
jdbc.password=123123
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

spring-db.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans.xsd
	  http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd">


		<bean id="dataSource" 
       class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
           <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
			<property name="jdbcUrl" value="jdbc:mysql://118.24.13.38:3308/goods?characterEncoding=utf8&amp;useSSL=false"></property>
			<property name="user" value="zhangsan"></property>
			<property name="password" value="123123"></property>
			<property name="initialPoolSize" value="2"></property>	
		    <property name="maxPoolSize" value="50"></property>
      </bean> 
    
    
    <!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置 c3p0 数据源 -->
	<bean id="dataSource2"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.username}"></property>	
		<property name="password" value="${jdbc.password}"></property>	
		<property name="jdbcUrl" value="${jdbc.url}"></property>	
		<property name="driverClass" value="${jdbc.driverClass}"></property>	
		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>	
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>	
	</bean>
	
	<!-- 方式1-->
      <bean id="dbUtilsTemplate" class="com.testfan.dbutils.DbUtilsTemplate"> 
        <property name="dataSource" ref="dataSource2" /> 
      </bean>
      
      <bean id="jdbcTemplate"
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource2"></property>
	</bean>
</beans>

jdbc数据库对象使用

import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.testfan.dbutils.TestUser;

@RunWith(SpringJUnit4ClassRunner.class)	
@ContextConfiguration(locations = {"classpath:spring-db.xml"})
public class SpringJdbcTest {
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Test
	public void testUpdate() {
		String sql = "UPDATE t_user_test SET loginname = ? WHERE uid = ?";
		jdbcTemplate.update(sql, "test1","1");
	}
	
	@Test
	public void testQueryOne() {
		String sql = "select * from t_user_test where uid =?";
		RowMapper<TestUser> rowMapper = new BeanPropertyRowMapper<>(TestUser.class);
		TestUser user = jdbcTemplate.queryForObject(sql, rowMapper,"1");
		System.out.println(user);
	}
	
	@Test
	public void testQueryForList() {
		String sql = "select * from t_user_test";
		RowMapper<TestUser> rowMapper = new BeanPropertyRowMapper<>(TestUser.class);
		List<TestUser> user = jdbcTemplate.query(sql, rowMapper);
		System.out.println(user.size());
	}
	
	@Test
	public  void testQueryMap() {
		String sql = "select * from t_user_test";
		List<Map<String, Object>> user = jdbcTemplate.queryForList(sql);
		System.out.println(user);
	}

}

spring mvc对web工程支持

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

查看web.xml

1 spring mvc如何和web工程集成

2 包扫描@Controller 支持servlet

3 servlet 协议和路径

@RequestMapping 访问路径,协议

@GetMapping/@PostMapping

4 ModelAndView 返回参数

5 jsp路径 通过spring提供对象控制

参数的自动注入,最大亮点

@Controller被扫描通过注解

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lfFdnc6K-1590736794989)(Day12.assets/image-20200511135743679.png)]

二种方式效果一样,都可以跳转到home.jsp页面

第一种使用ModelAndView ,第二种使用传统方式request

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值