您的位置:首页 > 其它

[置顶] SSM基础框架的搭建和测试

2017-08-15 16:47 357 查看
一.配置server端的web.xml

配置监听器

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
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"> <!-- 上下文配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>


二,配置application-context.xml

1.头文件

<beans xmlns="http://www.springframework.org/schema/beans"
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"> <!-- 数据源  、事务 、扫描、MYbatis、ActiveMQ、Freemarker、Redis、Solr。。。。。 -->
<import resource="config/*.xml"/>

</beans>

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

<!-- 数据源  、事务 、扫描、MYbatis、ActiveMQ、Freemarker、Redis、Solr。。。。。 -->
<import resource="config/*.xml"/>

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

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

1.数据源搭建

          <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  <!-- 驱动 -->
  <property name="driverClassName" value="${driverClassName}"/>
  <property name="url" value="${url}"/>
  <property name="username" value="${username}"/>
  <property name="password" value="${password}"/>
 </bean>

2.属性文件

<!-- redis.properties  solr.properties  -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

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.导入数据库配置

<!-- sqlSessionFactory 工厂   mybatis-3.2.7   mybaits-spring-1.2.2.jar-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Mybatis核心配置文件所在位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

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

如何实现mapper有3种方式,可参阅上一遍博文
<!-- 3种  第一种:原始Dao开始  接口 实现类 Mapper -->
<!-- 3种  第二种:接口   Mapper  手动实例化-->
<!-- 3种  第三种:接口   Mapper 扫描方式 自动 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描的包 -->
<property name="basePackage" value="cn.itcast.core.dao"/>

3.配置mybits-config.xml

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

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- 不能联想 sts-->

<configuration>
<!-- setting 迟延加载  二级缓存 (不用)   分布式缓存  -->
<!-- 别名 -->

<typeAliases>
<package name="cn.itcast.core.bean"/>
</typeAliases>
<!-- Mapper 位置 配置
<mappers>
</mappers>
-->

</configuration>

四.配置控制端的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
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">
<!-- POST  过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>console</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 默认找 WEB-INF/[servlet的名称]-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-console.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<!-- /  :拦截所有请求 (但不包括.jsp)   .js .png .css   (配置对静态资源放行)
/* :拦截所有请求   真全拦截   (基本上不用)   
*.do  : .do 
-->
<servlet-name>console</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>

五.配置spring-mvc

一个中心,前端控制器即DispatcherServlet

三个基本点,处理器映射器 处理器适配器 视图解释器

<!-- 扫描 @Contr -->
<context:component-scan base-package="cn.itcast" />
<!-- 处理器 映射器  适配器 -->
<mvc:annotation-driven/>
<!-- 视图解释器 jsp -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/console/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 图片上传 -->定义一个bean,可进行相关spring配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<import resource="dubbo-consumer.xml"/>




六 测试基础框架

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: