SSM基础框架的搭建和测试

一.配置server端的web.xml

配置监听器


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


contextConfigLocation
classpath:application-context.xml



org.springframework.web.context.ContextLoaderListener



二,配置application-context.xml

1.头文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://code.alibabatech.com/schema/dubbo        
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">



2.导入其他所有配置文件,或者将下面所有文件都合并到Application-context.xml 除jdbc.properties



注:后面配至文件统一放到classpath即resource/config下


二.数据库搭建(mysql&Druid)

1.数据源搭建

         
 
 
 
 
 
 

2.属性文件





classpath:jdbc.properties



3.mysql外部配置文件(外部文件需名为jdbc.properties,且路径放在resource下,不可加目录)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/babasport191?characterEncoding=UTF-8
username=root
password=root


三.配置mybaits

1.导入数据库配置









2.mapper实现类的配置,采用MapperScannerConfigurer扫描dao;

如何实现mapper有3种方式,可参阅上一遍博文






3.配置mybits-config.xml

别名的作用是方便在DAO.xml中不用输入类的全名

<?xml version="1.0" encoding="UTF-8" ?>
"http://mybatis.org/dtd/mybatis-3-config.dtd">









四.配置控制端的web.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">



encoding
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8



encoding
*.do



console
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:springmvc-console.xml

1




console
*.do



五.配置spring-mvc

一个中心,前端控制器即DispatcherServlet
三个基本点,处理器映射器 处理器适配器 视图解释器

<!-- 扫描 @Contr -->








定义一个bean,可进行相关spring配置



六 测试基础框架

1.测试dao和service

方法一

注junit需2.5以上,加载spring的application-context.xml,导入相关jar包

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-context.xml"})
public class TestTbTest {
@Autowired
private TestTbService testTbService;
@Test
public void testAdd() throws Exception {
TestTb testTb = new TestTb();
testTb.setName("范冰冰2");
testTb.setBirthday(new Date());
testTbService.insertTestTb(testTb);
}

方法二,读取spring配置文件方式有改变

使用ApplicationContext读取applicationContext.xml文件,然后获取相应bean;

public class SpringTest {

private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testBean() throws Exception {
TestAction testAction = (TestAction) ac.getBean("testAction");
System.out.println(testAction);
}
// 测试SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
// 测试事务
@Test
public void testTransaction() throws Exception {
TestService testService = (TestService) ac.getBean("testService");
// testService.saveTwoUsers();
testService.saveUsers25();
}

七,测试Spring-MVC

输入UPL,看是否输入静态页面

@RequestMapping(value = "/index.do")
public String toAdd(Model model){

return index;

}

你可能感兴趣的:(框架)