Spring MVC 项目搭建 -2- 添加service ,dao,junit

1.dao

public interface TestDao {
	public List<Hero> getSomeData();
}

@Repository
public class TestDaoImpl implements TestDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	private final static RowMapper<Hero> HERO_MAPPER = BeanPropertyRowMapper.newInstance(Hero.class);
	@Override
	public List<Hero> getSomeData() {
		try {
			return this.jdbcTemplate.query("SELECT * FROM hero", HERO_MAPPER);
		} catch (Exception e) {
			return null;
		}
	}
}


2.service

public interface MyTestService {
public void initDao();
}

@Service
public class MyTestServiceImpl implements MyTestService{
	@Autowired
	private TestDao testDao ;
	public void initService(){
		System.out.println("Init Service");
	}
	@Override
	public void initDao(){
		List<Hero> herosList = testDao.getSomeData();
		System.out.println(herosList.get(0));
	}
}

3.controller

@Controller
@RequestMapping(value = "/mytest")
public class TestController{
	@Autowired
	private MyTestService myTestService;
	@RequestMapping(value="/helloSpring",method=RequestMethod.GET)
	public @ResponseBody String HelloWorld(){
		return "Hello Spring";
	}
	
	@RequestMapping(value="/initMvcTest",method=RequestMethod.GET)
	public @ResponseBody String initMvctest(){
		myTestService.initDao();
		return "init run";
	}
}


4.配置文件修改 添加jdbc 配置

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"
    xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">		
	<!--添加读取 properties 的实例  -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" ref="propertyLocations"/>
	</bean>
	<!--设置读取的properties的路径 需要添加util标签-->
	<util:list id="propertyLocations">
		<value>classpath:jdbc.properties</value>
	</util:list>
	
	<!-- 设置数据库连接 需要jar包 commons-pool-1.3.jar,commons-dbcp.jar-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name = "dataSource"  ref="dataSource"/>
	</bean>
	<bean id="baseDAO" abstract="true">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- jdbcTemplate -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
		<property name="url"><value>${jdbc.url}</value></property>
		<property name="username"><value>${jdbc.username}</value></property>
		<property name="password"><value>${jdbc.password}</value></property>
		
        <property name="validationQuery"><value>${jdbc.validationQuery}</value></property>
        <property name="testOnBorrow"><value>${jdbc.testOnBorrow}</value></property>
       
        <property name="maxActive"><value>${jdbc.maxActive}</value></property>
        <property name="maxIdle"><value>${jdbc.maxIdle}</value></property>
        <property name="maxWait"><value>${jdbc.maxWait}</value></property>
	</bean>
</beans>

5.junit Test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
	"classpath:applicationContext.xml",
	"file:WebContent/WEB-INF/spring-test-servlet.xml"
})
public class MyTest {
	@Autowired
	private MyTestService myTestService;
	@Test
	public void thisTestRunsWith_MyRunner() {
		myTestService.initDao();
		Assert.assertTrue(true);
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值