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);
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章