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

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

1.dao

public class Hero {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

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);
    }
}
上一篇:Java Spring MVC项目搭建(一)——Spring MVC框架集成


下一篇:Spring MVC 项目搭建 -4- spring security-添加自定义登录页面