采用注解式的spring文件,切省略dao层的实现类
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 引入外部资源文件--> <context:property-placeholder location="classpath:mybatis.properties"></context:property-placeholder> <!--配置数据源--> <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!-- 整合数据源--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="entity"></property> <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property> </bean> <!-- 映射器 Spring自动生成dao实现类--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="dao"></property> </bean> <!-- 创建扫描器--> <context:component-scan base-package="service.Impl,dao"></context:component-scan> </beans>
spring-mvc的.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 扫描注解--> <context:component-scan base-package="controller"></context:component-scan> <!-- 配置该标签自动注册处理映射器 处理适配器 --> <mvc:annotation-driven/> <!-- 第一种: 处理静态资源文件 是为了过滤静态文件(js,css,image)--> <mvc:default-servlet-handler/> <!-- 第二种: 处理静态资源文件 较多--> <!-- <mvc:resources mapping="/static/**" location="/statics/"></mvc:resources>--> <!-- 视图解析器 配置跳转页--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀--> <property name="prefix" value="/"></property> <!-- 后缀--> <property name="suffix" value=".jsp"></property> </bean> </beans>
ssm项目的web.xml文件的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置spring监听--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring的上下文配置信息--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 核心前端控制器--> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--中文乱码处理问题--> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
这个项目都是采用注解式来开发的
注解解释:
@Repository//dao实现类 @Service//业务层 @Controller//Controller(也就是servlet层)
在Controller层
@RequestMapping(value = "ajaxlogin",produces = "text/html;charset=utf-8")//设置命名空间 @RequestParam(value = "参数名",required = false,defaultValue = "xxx")//设置参数 required是设置传入参数可为空 defaultValue为空时的默认值