私人用,勿在意!

来源:互联网 发布:淘宝退货运费多少钱 编辑:程序博客网 时间:2024/06/10 01:18

<?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="cn.itheima.dao.CustomerMapper"><sql id="customer_where"><where>    <if test="custName != null and custName != ''">and a.cust_name like '%${custName}%'</if><if test="custSource != null and custSource != ''">and a.cust_source=#{custSource}</if><if test="custIndustry != null and custIndustry != ''">and a.cust_industry=#{custIndustry}</if><if test="custLevel != null and custLevel != ''">and a.cust_level=#{custLevel}</if></where></sql><select id="findCustomerByVo" parameterType="cn.itheima.pojo.QueryVo" resultType="cn.itheima.pojo.Customer">select a.cust_id,a.cust_name, b.dict_item_name cust_source, c.dict_item_name cust_industry, d.dict_item_name cust_level,a.cust_phone,a.cust_mobile, a.cust_linkman, a.cust_zipcode, a.cust_address, a.cust_createtimefrom customer aleft join base_dict b on a.cust_source = b.dict_idleft join base_dict c on a.cust_industry = c.dict_idleft join base_dict d on a.cust_level = d.dict_id<include refid="customer_where"></include>limit #{start}, #{size}</select><select id="findCustomerByVoCount" parameterType="cn.itheima.pojo.QueryVo" resultType="int">select count(*)from customer aleft join base_dict b on a.cust_source = b.dict_idleft join base_dict c on a.cust_industry = c.dict_idleft join base_dict d on a.cust_level = d.dict_id<include refid="customer_where"></include></select><select id="findCustomerById" parameterType="long" resultType="cn.itheima.pojo.Customer">select * from customer where cust_id=#{id}</select><update id="updateCustomerById" parameterType="cn.itheima.pojo.Customer">update customer <!-- set标签作用:第一可以自动添加set关键字, 第二可以去掉最后一个更新的逗号 --><set><if test="cust_name != null and  cust_name != ''">cust_name=#{cust_name} ,</if><if test="cust_source != null and cust_source  != ''">cust_source=#{cust_source},</if><if test="cust_industry != null and  cust_industry != ''">cust_industry=#{cust_industry},</if><if test="cust_level != null and  cust_level != ''">cust_level=#{cust_level},</if><if test="cust_linkman != null and  cust_linkman != ''">cust_linkman=#{cust_linkman},</if><if test=" cust_phone != null and  cust_phone != ''">cust_phone=#{cust_phone},</if><if test="cust_mobile != null and cust_mobile  != ''">cust_mobile=#{cust_mobile},</if><if test="cust_zipcode != null and  cust_zipcode != ''">cust_zipcode=#{cust_zipcode},</if><if test="cust_address != null and   cust_address!= ''">cust_address=#{cust_address},</if></set>where cust_id=#{cust_id}</update><delete id="delCustomerById" parameterType="long">delete from customer where cust_id=#{id}</delete></mapper>

package cn.itheima.controller;import java.io.File;import java.util.List;import java.util.UUID;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.servlet.ModelAndView;import cn.itheima.exception.CustomException;import cn.itheima.pojo.Items;import cn.itheima.service.ItemsService;import cn.itheima.vo.QueryVo;@Controller// 窄化请求映射:为防止你和你的队友在conroller方法起名的时候重名,所以相当于在url中多加了一层目录,防止重名// 例如:当前list的访问路径 localhost:8081/ssm0523-1/items/list.action@RequestMapping("/items")public class ItemsController {@Autowiredprivate ItemsService itmesService;// @RequestMapping(value="/list", method=RequestMethod.GET)@RequestMapping("/list")public ModelAndView itemsList() throws Exception {// 测试运行时异常// int i= 0/0;// 测试自定义异常// if(true){// CustomException customException = new CustomException();// customException.setMessage("对不起哦, 您已经抢购过, 不要太贪心哦!");// throw customException;// }List<Items> list = itmesService.list();ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("itemList", list);modelAndView.setViewName("itemList");return modelAndView;}/** * springMvc中默认支持的参数类型:也就是说在controller方法中可以加入这些也可以不加, 加不加看自己需不需要,都行. * HttpServletRequest HttpServletResponse HttpSession Model * * 通过@PathVariable可以接收url中传入的参数 @RequestMapping("/itemEdit/{id}")中接收参数使用大括号中加上变量名称, @PathVariable中的变量名称要和RequestMapping * 中的变量名称相同 */@RequestMapping("/itemEdit/{id}")public String itemEdit(@PathVariable("id") Integer id, HttpServletRequest reuqest, Model model) throws Exception {// String idStr = reuqest.getParameter("id");Items items = itmesService.findItemsById(id);// Model模型:模型中放入了返回给页面的数据// model底层其实就是用的request域来传递数据,但是对request域进行了扩展.model.addAttribute("item", items);// 如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称return "editItem";}// springMvc可以直接接收基本数据类型,包括string.spirngMvc可以帮你自动进行类型转换.// controller方法接收的参数的变量名称必须要等于页面上input框的name属性值// spirngMvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo的属性名称@RequestMapping("/updateitem")// public String update(Integer id, String name, Float price, String detail)// throws Exception{public String update(MultipartFile pictureFile, Items items, Model model, HttpServletRequest request)throws Exception {// 1. 获取图片完整名称String fileStr = pictureFile.getOriginalFilename();// 2. 使用随机生成的字符串+源图片扩展名组成新的图片名称,防止图片重名String newfileName = UUID.randomUUID().toString() + fileStr.substring(fileStr.lastIndexOf("."));// 3. 将图片保存到硬盘pictureFile.transferTo(new File("D:\\image\\" + newfileName));// 4.将图片名称保存到数据库items.setPic(newfileName);itmesService.updateItems(items);// 返回数据// request.setAttribute("", arg1);// 指定返回的页面(如果controller方法返回值为void,则不走springMvc组件,所以要写页面的完整路径名称)// request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request,// response);// 重定向:浏览器中url发生改变,request域中的数据不可以带到重定向后的方法中// model.addAttribute("id", items.getId());// 在springMvc中凡是以redirect:字符串开头的都为重定向return "redirect:itemEdit/" + items.getId();// 请求转发:浏览器中url不发生改变,request域中的数据可以带到转发后的方法中// model.addAttribute("id", items.getId());// spirngMvc中请求转发:返回的字符串以forward:开头的都是请求转发,// 后面forward:itemEdit.action表示相对路径,相对路径就是相对于当前目录,当前为类上面指定的items目录.在当前目录下可以使用相对路径随意跳转到某个方法中// 后面forward:/itemEdit.action路径中以斜杠开头的为绝对路径,绝对路径从项目名后面开始算// return "forward:/items/itemEdit.action";}// 如果Controller中接收的是Vo,那么页面上input框的name属性值要等于vo的属性.属性.属性.....@RequestMapping("/search")public String search(QueryVo vo) throws Exception {System.out.println(vo);return "";}@RequestMapping("/delAll")public String delAll(QueryVo vo) throws Exception {// 如果批量删除,一堆input复选框,那么可以提交数组.(只有input复选框被选中的时候才能提交)System.out.println(vo);return "";}@RequestMapping("/updateAll")public String updateAll(QueryVo vo) throws Exception {System.out.println(vo);return "";}// 导入jackson的jar包在// controller的方法中可以使用@RequestBody,让spirngMvc将json格式字符串自动转换成java中的pojo// 页面json的key要等于java中pojo的属性名称// controller方法返回pojo类型的对象并且用@ResponseBody注解,springMvc会自动将pojo对象转换成json格式字符串@RequestMapping("/sendJson")@ResponseBodypublic Items json(@RequestBody Items items) throws Exception {System.out.println(items);return items;}}

package cn.itheima.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/login")public class LoginController {//跳转到登录页面@RequestMapping("/login")public String login() throws Exception{return "login";}@RequestMapping("/submit")public String submit(String username, String pwd ,HttpServletRequest request) throws Exception{HttpSession session = request.getSession();//判断用户名密码的正确性,如果正确则将登录信息放入session中//这里简写,真正项目中需要去数据库中校验用户名和密码if(username != null){session.setAttribute("username", username);}//跳转到列表页return "redirect:/items/list";}}

package cn.itheima.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 Interceptor1 implements HandlerInterceptor {//执行时机:controller已经执行,modelAndview已经返回//使用场景: 记录操作日志,记录登录用户的ip,时间等.@Overridepublic void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {System.out.println("======Interceptor1=======afterCompletion========");}//执行时机:Controller方法已经执行,ModelAndView没有返回//使用场景: 可以在此方法中设置全局的数据处理业务@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)throws Exception {System.out.println("======Interceptor1=======postHandle========");}//返回布尔值:如果返回true放行,返回false则被拦截住//执行时机:controller方法没有被执行,ModelAndView没有被返回//使用场景: 权限验证@Overridepublic boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {System.out.println("======Interceptor1=======preHandle========");return true;}}

Dao:

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

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<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>

<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>

<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itheima.dao"/>
</bean>

</beans>



Service:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


<!-- @Service扫描 -->
<context:component-scan base-package="cn.itheima.service"></context:component-scan>
</beans>



事务tran:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<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="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.itheima.service.*.*(..))" />
</aop:config>

</beans>


database.properties

:


jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=sml
jdbc.password=sunmingliang

log4j.properties


# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n


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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 引入字典资源文件 -->
    <context:property-placeholder location="classpath:resource.properties"/>
    
    <!-- @Controller注解扫描 -->
    <context:component-scan base-package="cn.itheima.controller"></context:component-scan>
    
    <!-- 注解驱动:
    替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    <!-- 配置视图解析器 
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 =  前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 配置自定义转换器 
注意: 一定要将自定义的转换器配置到注解驱动上
-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 指定自定义转换器的全路径名称 -->
<bean class="cn.itheima.controller.converter.CustomGlobalStrToDateConverter"/>
</set>
</property>
</bean>


</beans>


sqlmapconfig.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>

</configuration>

web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssm-crm</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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:SpringMvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>