通过JNDI从服务器容器中获取资源_Spring JNDI+Mysql+Tomcat 通过JNDI从服务器容器中获取DataSource资源 (由容器管理,不要关闭它,容器自己会处理)上一篇我们使用的是dbcp,这里使用JNDI: 使用JNDI连接数据: 在Spring注释 bean id=dataSource class=org.a
通过JNDI从服务器容器中获取资源_Spring JNDI+Mysql+Tomcat在Spring注释
java:comp/env/jdbc/joba 
通过数据源访问数据库,由于数据源由tomcat创建并维护,所以必须把MySql的驱动包拷贝到Tomcat根目录\lib中
二、配置数据源 
在tomcat根目录\conf\context.xml里的
这里我们介绍一下
测试:必须在tomcat容器里面运行,要启动tomcat不能用junit
我这里用的是我练习项目的测试,在action的login方法前面加下面代码登陆的时候就会执行这段代码:这段代码是下面的testAdd考过来的
package com.jboa.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jboa.model.Department;
import com.jboa.model.Employee;
import com.jboa.model.Postion;
public class EmployeeServiceTest {
	@Test
	public void testAdd() {
	ApplicationContext ac = new ClassPathXmlApplicationContext("/*ApplicationContext.xml");
	EmployeeService employeeService = (EmployeeService) ac.getBean("employeeService");
	Employee employee = new Employee();
	employee.setSn("user11111112");
	employee.setPassword("user11111112");
	employee.setStatus("1");
	employee.setName("user1111112");
	Postion p = new Postion();
	p.setId(2);
	employee.setPostion(p);
	Department d = new Department();
	d.setId(1);
	employee.setDepartment(d);
	employeeService.add(employee);
	}
}
添加成功,测试成功; 
下载本文