MVC三层写法解剖


I.[repository]


1.IBaseDao基本接口 
如:IBaseDao


2.IxxxDao二级接口
如:IxxxDao extends IBaseDao
规定输入具体类型的方法的重载


3.@Repository("IxxxDao") I.2
  class yyDao extends JdbcDaoSupport implements IxxxDao 继承JdbcDaoSupport类的实现类
logger
方法如:
String sql = "SELECT count(*) FROM product";
return this.getJdbcTemplate().queryForLong(sql);


II.[service]


1.IxxxService接口


2.@Service  
  @Transactional
  class xxxServiceImpl implements IxxxService
logger
  @Autowired
  I.2(即IxxxDao xxxDao;);
  调用xxxDao对象,实现所有方法


III.[controller]


1.@Controller
  yyyController
logger
  @RequestMapping(value="/xx/",method =RequestMethod.GET)
  public String qwer(Locale locale,Model model)//Locale 国际化的实现,需要配置locale.xml等
  @Autowired
  II.1(即IxxxService ixxxservice;)
  model.addAttribute("servertime",dataFormat);
return "yy";


src/test/java:                 src/test/resources: datasource.xml(数据库配置)、root-context.xml(事务配置)、
                                                   servlet-context.xml(springMVC的一些配置如拦截器                                                  (使用@Controller注解必备)等)


IV.[repository]
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath*:root-context.xml",
"classpath*:servlet-context.xml",
"classpath*:datasource.xml" })
@TransactionConfiguration
@Transactional
I.3+Tests(即xxxDaoTests)
logger
@Autowired
I.2(即IxxxDao xxxDao;);


@BeforeClass
    public static void oneTimeSetUp() {
        // one-time initialization code   
    logger.debug("@BeforeClass - oneTimeSetUp");
    }
 
    @AfterClass
    public static void oneTimeTearDown() {
        // one-time cleanup code
    logger.debug("@AfterClass - oneTimeTearDown");
    }


    @BeforeTransaction
    public void verifyInitialDatabaseState() {
        // logic to verify the initial state before a transaction is started
    logger.debug("@BeforeTransaction verifyInitialDatabaseState");
    }


    @AfterTransaction
    public void verifyFinalDatabaseState() {
        // logic to verify the final state after transaction has rolled back
    logger.debug("@AfterTransaction verifyFinalDatabaseState");
    }
@Before
public void setUp() {
logger.debug("@Before setUp");
}


@After
public void tearDown() {
logger.debug("@After tearDown");
}
    @Test
    public void testxxx(){
Assert.assertEquals(String message,Object[] expected,Object[] actual);
}


V.[service]


@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath*:root-context.xml",
"classpath*:servlet-context.xml",
"classpath*:datasource.xml" })
II.2+Tests(即xxxServiceImplTests)
@Autowired
II.1(即IxxxService xxxService)
@Test
public void xxyy(){
Assert.assertEquals();
}


VI.[controller]
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath*:root-context.xml",
"classpath*:servlet-context.xml", "classpath*:datasource.xml" })
III.1(即xxxControllerTests)
private WebApplicationContext wac;


private MockMvc mockMvc;//对于服务器端的Spring MVC测试支持主入口点


@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}


@After
public void teardown() {
}

你可能感兴趣的:(JAVA学习资料)