本文采用myEclipse 整合SSM框架
第一步jar包 WEB-INF > lib
链接: https://pan.baidu.com/s/1CtToolxjDXVyVBvQb_L7wQ 提取码: 9af7
ark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FSUkFZSEVQUFk=,size_16,color_FFFFFF,t_70)
第二步
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 过滤器允许通过的格式 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.assets</url-pattern>
<url-pattern>*.images</url-pattern>
</servlet-mapping>
<!-- springmvc 的配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value> <!-- classpath 路径到src -->
</init-param>
</servlet>
<servlet-mapping>
<!--spring容器监听器,加载spring容器 -->
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value> <!-- 加载所有此路径下的xml文件 -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
第三步
mybatis.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">
<configuration>
<!-- setting配置根据需要再添加 -->
<!-- 配置别名 -->
<typeAliases>
<package name="com.nb.entity"/><!-- 扫描实体类包 实体类可小写如User>user -->
</typeAliases>
<!-- mapper这里不需要配置了,因为跟spring整合后,在spring那边会进行mapper的扫描
但必须遵循:mapper.xml和mapper.java必须同名且在一个目录
-->
</configuration>
第四步
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/crmdb
jdbc.username=root
jdbc.password=admin
第五步
springmvc.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 一个配置节解决映射器和适配器的配置注解配置 -->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:default-servlet-handler/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:mapping path="/CRM/**" />
<bean class="com.nb.interceptor.AccessInterceptor">
</bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:mapping path="/CRM/**" />
<mvc:exclude-mapping path="/CRM/login.jsp" />
<mvc:exclude-mapping path="/CRM/js/**" />
<bean class="com.nb.interceptor.Login"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 扫描所有的Controller -->
<context:component-scan base-package="com.nb.controller"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" /><!-- 注意WEB-INF前面带个/,否则restful不能实现 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 静态资源解析,包括js,css,img... -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
</beans>
第六步
applicationContext-transaction.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 事务管理器 -->
<!-- 对mybatis操作数据事务控制,spring使用jdbc的事务控制类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源dataSource在applicationContex-dao.xml中配置了 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.nb.service.UsersServiceImpl.*(..))"/>
</aop:config>
</beans>
第七步
applicationContext-service.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.nb.service"/><!-- 扫描所有的service包里的注解 -->
<mvc:annotation-driven/>
</beans>
第八步
applicationContext-dao.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.nb.service"/>
<context:component-scan base-package="com.nb.interceptor"/>
<mvc:annotation-driven/>
<!-- 加载db.properties文件中的内容 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com.nb.mapper.*.xml"/>
</bean>
<!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包的路径,如果需要扫描多个包,中间使用半角 逗号隔开-->
<property name="basePackage" value="com.nb.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
package com.nb.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
- @author Administrator
*/
public class AccessInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
return true;
}
}
`
package com.nb.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class Login implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object obj, Exception err)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object obj, ModelAndView mav)
throws Exception {
// response.sendRedirect("login.jsp");
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object obj) throws Exception {
String uname = (String) request.getSession().getAttribute("uname");
String uri = request.getRequestURI();
System.out.println("uname===============" + uname);
if (uri.equals("/CRM/login")) {
return true;
} else if (uname != null && !"".equals(uname)) {
return true;
} else {
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
return false;
}
}
Mapper映射文件格式
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.nb.mapper.ModulesMapper">
<resultMap id="modules" type="modules">
<id property="id" column="Id" jdbcType="INTEGER" />
<result property="name" column="Name" jdbcType="VARCHAR" />
<result property="parentId" column="ParentId" jdbcType="INTEGER" />
<result property="path" column="Path" jdbcType="VARCHAR" />
<result property="weight" column="Weight" jdbcType="INTEGER" />
<result property="ops" column="Ops" jdbcType="VARCHAR" />
<result property="int0" column="Int0" jdbcType="INTEGER" />
</resultMap>
<sql id="columns">
Id id,Name name,ParentId parentId,Path path,Weight weight,
Ops ops,Int0 int0
</sql>
<!--查询是否已存在的模块 -->
<select id="getModuleByNID" parameterType="modules" resultType="modules">
select * from modules where ParentId=#{parentId} and Name=#{name}
</select>
</mapper>
serviceimpl注解
@Component
public class ConsultingServiceImpl implements ConsultingService {
@Autowired
private ConsultingMapper conMapper;
controller注解
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nb.entity.Abc;
import com.nb.entity.Askers;
import com.nb.entity.Netfollows;
import com.nb.entity.Students;
import com.nb.entity.Userchecks;
import com.nb.entity.ui.Log;
import com.nb.mapper.NetAskerMapper;
import com.nb.service.ConsultingService;
import com.nb.service.UsersService;
import com.nb.util.Result;
@Controller
public class ConsultingController {
@Autowired
private ConsultingService conService;
@Autowired
private NetAskerMapper netService;
@Autowired
private UsersService uService;
/**
* 网络追踪多条件查询
*
* @param page
* @param rows
* @param Name
* @param FollowTime
* @param NextFollowTime
* @param FollowType
* @param FollowState
* @return
* @throws ParseException
*/
@RequestMapping("/getAllConsulting")
public @ResponseBody
Map<String, Object> getAllConsult(int page, int rows, String Name,
String FollowTime, String NextFollowTime, String FollowType,
String FollowState,int uid) throws ParseException {
int pageSize = (page - 1) * rows;
Map<String, Object> map = new HashMap<String, Object>();
Askers asker=uService.getAskersbyUserId(uid);
if(asker!=null){
map.put("askerId", asker.getAskerId());
}
map.put("pageSize", pageSize);
map.put("rows", rows);
map.put("Name", Name);
map.put("FollowTime", FollowTime);
map.put("NextFollowTime", NextFollowTime);
map.put("FollowType", FollowType);
map.put("FollowState", FollowState);
System.out.println("map======================="+map);
List<Netfollows> roleslist = conService.getAllfollowcount(map);
int total = conService.getNetfollowcount();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("rows", roleslist);
map1.put("total", total);
return map1;
}