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

Spring MVC 项目搭建 -2- 添加service ,dao,junit的更多相关文章

  1. Spring MVC 项目搭建 -3- 快速 添加 spring security

    Spring MVC 项目搭建 -3- 快速 添加 spring security 1.添加 spring-sample-security.xml <!-- 简单的安全检验实现 --> & ...

  2. Spring MVC 项目搭建 -6- spring security 使用自定义Filter实现验证扩展资源验证,使用数据库进行配置

    Spring MVC 项目搭建 -6- spring security使用自定义Filter实现验证扩展url验证,使用数据库进行配置 实现的主要流程 1.创建一个Filter 继承 Abstract ...

  3. Spring MVC 项目搭建 -5- spring security 使用数据库进行验证

    Spring MVC 项目搭建 -5- spring security 使用数据库进行验证 1.创建数据表格(这里使用的是mysql) CREATE TABLE security_role ( id ...

  4. Spring MVC 项目搭建 -4- spring security-添加自定义登录页面

    Spring MVC 项目搭建 -4- spring security-添加自定义登录页面 修改配置文件 <!--spring-sample-security.xml--> <!-- ...

  5. Spring MVC 项目搭建 -1- 创建项目

    Spring MVC 项目搭建 -1- 创建项目 1.创建 Dynamic Web project (SpringDemo),添加相关jar包 2.创建一个简单的controller类 package ...

  6. 简单Spring MVC项目搭建

    1.新建Project 开发环境我使用的是IDEA,其实使用什么都是大同小异的,关键是自己用的顺手. 首先,左上角File→New→Project.在Project页面选择Maven,然后勾上图中所示 ...

  7. IDEA 创建Spring MVC项目搭建

    概述 IntelliJ IDEA是一款更加集成智能的开发工具,相对Myeclipse开发而言,使用起来相对更加的方便:初步手动使用IDEA搭建Spring MVC项目,现将操作流程整理记录如下. 环境 ...

  8. Java Spring MVC项目搭建(二)——项目配置

    1.站点配置文件web.xml 每一个Spring MVC 项目都必须有一个站点配置文件web.xml,他的主要功能吗....有一位大哥已经整理的很好,我借来了,大家看看: 引用博客地址: http: ...

  9. Java Spring MVC项目搭建(一)——Spring MVC框架集成

    1.Java JDK及Tomcat安装 我这里安装的是JDK 1.8 及 Tomcat 8,安装步骤详见:http://www.cnblogs.com/eczhou/p/6285248.html 2. ...

随机推荐

  1. bootstrap学习笔记之为导航条添加标题、二级菜单及状态 http://www.imooc.com/code/3120

    为导航条添加标题.二级菜单及状态 加入导航条标题 在Web页面制作中,常常在菜单前面都会有一个标题(文字字号比其它文字稍大一些),其实在Bootstrap框架也为大家做了这方面考虑,其通过" ...

  2. .Net程序员学用Oracle系列(29):PLSQL 之批量应用和系统包

    1.批量数据操作 1.1.批量生成数据 1.2.批量插入数据 2.批量生成脚本 3.生成数据字典 4.常见系统包 4.1.DBMS_OUTPUT 4.2.DBMS_RANDOM 4.3.其它系统包及常 ...

  3. Vue之Vuex

    一.什么是vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.简单来说就是一个数据统一 ...

  4. javaWeb学习总结(10)- EL函数库(2)

    一.EL函数库介绍 由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用. 这些EL函数在JSTL开发包中进行描述,因此在JSP页 ...

  5. php原生自定义验证码,5分钟搞定你的问题

    当然现在很多php的框架里面自带了很多很多验证码,我的这个验证码,也是当初刚刚入行的时候学习模仿的.现在照搬出来,希望对刚入门的朋友有所帮助. **************************** ...

  6. Cohort Analysis and LifeCycle Grids mixed segmentation with R(转)

    This is the third post about LifeCycle Grids. You can find the first post about the sense of LifeCyc ...

  7. SpringMVC——请求映射

    SpringMVC中,如何处理请求是很重要的任务.请求映射都会使用@RequestMapping标注.其中,类上的标注相当于一个前缀,表示该处理器是处理同一类请求:方法上的标注则更加细化.如,类的标注 ...

  8. Java文件流之练习

    1 )将"今年是反法西斯胜利70周年,举国欢庆,所以要放假啦" 字符串 使用文件字符输出流 写入到oldhappy.txt文件中,复写10000行, 要求换行 在文件的开头写入当前 ...

  9. 深入探索C++对象模型(五)

    构造.解构.拷贝语意学(Semantics of Construction,Destruction, and Copy) 一般而言,class的data member应该被初始化,并且只在constr ...

  10. [Leetcode] Binary search--436. Find Right Interval

      Given a set of intervals, for each of the interval i, check if there exists an interval j whose st ...