SSM框架整合之练习篇

SSM的练习 :
        1开发环境
        数据库:mysql5.5以上版本。
        Jdk:1.7
        开发环境:Eclipse mars2
        Spring:4.2.4
        Mybatis:3.2.7
        Tomcat:7
        2数据库
        数据库使用mysql 数据库。

        1、创建crm数据库
        2、将参考资料中的sql脚本导入到数据库中

        3工程搭建
        工程使用Springmvc、spring、mybatis框架整合完成。

        Dao层:SqlMapConfig.xml(空)
                applicationContext-dao.xml:数据库连接池、SqlSessionFactory、Mapper的扫描器。
        Service层:
            配置包扫描器,扫描所有带@Service注解的类。事务管理器、切面。
        表现层:
                Springmvc.xml:包扫描器@Controller、配置注解驱动、视图解析器。
                Jsp:bootstrap
        Web.xml:配置spring监听器,前端控制器。
        3.1SqlMapConfig.xml
        
        

        

        
        3.2applicationContext-dao.xml
        
            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" 
            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-4.2.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-4.2.xsd
                http://www.springframework.org/schema/task
                http://www.springframework.org/schema/task/spring-task-4.2.xsd">

            
            

            
            class="com.alibaba.druid.pool.DruidDataSource">
                
                
                
                
                
                
                
                
            

            
            class="org.mybatis.spring.SqlSessionFactoryBean">
                
                
                
                
                
                
            

            
            class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                
            

        

        Jdbc.properties
        jdbc.driver=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
        jdbc.username=root
        jdbc.password=root
        3.3applicationContext-service.xml
        
            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.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-4.2.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-4.2.xsd
                http://www.springframework.org/schema/task
                http://www.springframework.org/schema/task/spring-task-4.2.xsd
                http://code.alibabatech.com/schema/dubbo        
                http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
                
                
                
                package="cn.baidu.core.service"/>
                
                
                
        

        3.4applicationContext-trans.xml
        
        
            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.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.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-4.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
            
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                
                
            
            
            
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
            
            
            
                
                    pointcut="execution(* cn.baidu.core.service.*.*(..))" />
            
        
        3.5Springmvc.xml
        
            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"
            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-4.2.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-4.2.xsd
                http://www.springframework.org/schema/task
                http://www.springframework.org/schema/task/spring-task-4.2.xsd">        
                
                
                
                package="cn.baidu.core.web.controller"/>
                
                
                
                
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    
                    
                
                
        

        3.6Web.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">
            
                customer.action
            
            
            
                contextConfigLocation
                classpath:applicationContext-*.xml
            
            
            
                class>org.springframework.web.context.ContextLoaderListenerclass>
            


            
            
                encoding
                class>org.springframework.web.filter.CharacterEncodingFilterclass>
                
                    encoding
                    UTF-8
                
            

            
                encoding
                *.action
            
            
            
                crm
                class>org.springframework.web.servlet.DispatcherServletclass>
                
                    contextConfigLocation
                    
                    classpath:springmvc.xml
                
                1
            
            
                crm
                
                *.action
            
        


        3.7加入jsp及分页标签

        Tld文件需要放到WEB-INF目录下, tomcat的规定。当tomcat启动时会自动加载。

        Jsp中使用标签:



        4查询条件初始化
        4.1需求

        初始化查询条件下拉列表。

        4.2Sql
        SELECT * from base_dict WHERE dict_type_code='006'
        4.3Dao
        
        http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        
            
        

        4.4Service
        @Service
        public class BaseDictServiceImpl implements BaseDictService {

            @Autowired
            private BaseDictDao baseDictDao;
            
            @Override
            public List getBaseDictList(String typeCode) {
                List list = baseDictDao.getBaseDictList(typeCode);
                return list;
            }

        }

        4.5Controller












        规则:子容器可以访问父容器的对象,父容器不能访问子容器的对象。
        public class CustomerController {
            
            @Autowired
            private BaseDictService baseDictService;
            @Value("${customer.source.code}")
            private String CustomerSourceCode;
            @Value("${customer.industry.code}")
            private String CustomerIndustryCode;
            @Value("${customer.level.code}")
            private String CustomerLevelCode;

            @RequestMapping("/list")
            public String showCustomerList(Model model) {
                //查询字典表初始化下拉列表
                List custSourceList = baseDictService.getBaseDictList(CustomerSourceCode);
                List custIndustryList = baseDictService.getBaseDictList(CustomerIndustryCode);
                List custLevelList = baseDictService.getBaseDictList(CustomerLevelCode);
                //把列表传递给jsp页面
                model.addAttribute("fromType", custSourceList);
                model.addAttribute("industryType", custIndustryList);
                model.addAttribute("levelType", custLevelList);
                
                return "customer";
            }
        }

        5客户列表展示
        5.1需求

        展示商品列表,并且可以根据查询条件过滤查询结果,并且实现分页处理。

        5.2Sql
        SELECT
            a.cust_id,
            a.cust_name,
            a.cust_user_id,
            a.cust_create_id,
            b.dict_item_name cust_source,
            c.dict_item_name cust_industry,
            d.dict_item_name cust_level,
            a.cust_linkman,
            a.cust_phone,
            a.cust_mobile,
            a.cust_zipcode,
            a.cust_address,
            a.cust_createtime
        FROM
            customer a
        LEFT JOIN base_dict b ON a.cust_source = b.dict_id
        LEFT JOIN base_dict c ON a.cust_industry = c.dict_id
        LEFT JOIN base_dict d ON a.cust_level = d.dict_id
        WHERE
            cust_name LIKE '%马%'
        AND cust_source = 6
        AND cust_industry = 2
        AND cust_level = 22

        5.3Dao
        

        增加count后的dao
        
            
            
                
                    
                        AND    cust_name LIKE '%${custName}%'
                    
                    
                        AND cust_source = #{custSource}
                    
                    
                        AND cust_industry = #{custIndustry}
                    
                    
                        AND cust_level = #{custLevel}
                    
                
            
            
            
            
        

        5.4Service
        根据查询条件查询数据库得到客户列表。分页条件。
        接收查询条件QueryVo接收,使用page接收页码。
        1、通过page计算start。
        2、调用dao查询客户列表。
        3、做count处理。计算出此查询条件中共查询到多少条记录。

        返回结果:Page对象。
        条件:QueryVo
        @Service
        public class CustomerServiceImpl implements CustomerService {

            @Autowired
            private CustomerDao customerDao;
            
            @Override
            public Page getCustList(QueryVo queryVo) {
        queryVo.setStart((queryVo.getPage() - 1) * queryVo.getRows());
                List custList = customerDao.getCustList(queryVo);
                Page page = new Page();
                //设置客户列表
                page.setRows(custList);
                page.setPage(queryVo.getPage());
                page.setSize(queryVo.getRows());
                //计算查询总记录数
                int total = customerDao.getCustListCount(queryVo);
                page.setTotal(total);
                return page;
            }

        }

        5.5Controller
        5.5.1分析
        1、接收页面提交的查询参数:
        保证jsp页面提交的表单中的input 的name属性和QueryVo中的属性一致
        2、调用Service查询客户列表
        3、把客户列表传递给页面。


        @RequestMapping("/list")
            public String showCustomerList(Model model, QueryVo queryVo) throws Exception {
                
                String custName = queryVo.getCustName();
                if (custName != null && !"".equals(custName)) {
                    custName = new String(custName.getBytes("iso8859-1"), "utf-8");
                    queryVo.setCustName(custName);
                }
                //查询字典表初始化下拉列表
                List custSourceList = baseDictService.getBaseDictList(CustomerSourceCode);
                List custIndustryList = baseDictService.getBaseDictList(CustomerIndustryCode);
                List custLevelList = baseDictService.getBaseDictList(CustomerLevelCode);
                //查询客户列表
                Page page = customerService.getCustList(queryVo);
                //把page放到request中
                model.addAttribute("page", page);
                
                //把列表传递给jsp页面
                model.addAttribute("fromType", custSourceList);
                model.addAttribute("industryType", custIndustryList);
                model.addAttribute("levelType", custLevelList);
                
                //页面回显
                model.addAttribute("custName", queryVo.getCustName());
                model.addAttribute("custSource", queryVo.getCustSource());
                model.addAttribute("custIndustry", queryVo.getCustIndustry());
                model.addAttribute("custLevel", queryVo.getCustLevel());
                
                
                return "customer";
            }


        6修改客户信息
        6.1需求

        1、点击客户列表中的“修改”按钮弹出客户信息修改对话框,并初始化客户信息
        2、点击“保存修改”按钮将修改后的结果保存到数据库中

        6.2展示客户信息

        6.2.1分析
        请求的url:
        customer/edit.action
        参数:cust_id客户id
        返回值:响应json数据,直接由Customer转换而来。需要使用@ResponseBody注解。

        6.2.2Dao层
        根据客户id查询客户信息。
        

        6.2.3Service层
            @Override
            public Customer getCustomerById(long custId) {
                Customer customer = customerDao.getCustomerById(custId);
                return customer;
            }

        6.2.4Controller
        要求返回json数据
        请求的url:
        customer/edit.action
        参数:cust_id客户id
        返回值:响应json数据
        @RequestMapping("/edit")
            @ResponseBody
            public Customer getCustomerById(Long id) {
                Customer customer = customerService.getCustomerById(id);
                return customer;
            }


        6.3提交修改
        6.3.1分析
        请求的url:customer/update.action
        请求的方法:post
        参数:表单中的数据。
        返回结果:OK

        6.3.2Dao层
        
                UPDATE customer
                
                    
                    cust_name=#{cust_name},
                    
                    
                    cust_user_id=#{cust_user_id},
                    
                    
                    cust_create_id=#{cust_create_id},
                    
                    
                    cust_source=#{cust_source},
                    
                    
                    cust_industry=#{cust_industry},
                    
                    
                    cust_level=#{cust_level},
                    
                    
                    cust_linkman=#{cust_linkman},
                    
                    
                    cust_phone=#{cust_phone},
                    
                    
                    cust_mobile=#{cust_mobile},
                    
                    
                    cust_zipcode=#{cust_zipcode},
                    
                    
                    cust_address=#{cust_address},
                    
                    
                    cust_createtime=#{cust_createtime},
                    
                
                WHERE
                    cust_id = #{cust_id}
            
        6.3.3Service层
        @Override
            public void updateCustomerById(Customer customer) {
                customerDao.updateCustomerById(customer);
            }

        6.3.4Controller
        接收参数:Customer接收。
        响应结果:OK字符串。使用@ResponseBody
        请求的url:customer/update.action
        @RequestMapping(value="/update", method=RequestMethod.POST)
            @ResponseBody
            public String updateCustomer(Customer customer) {
                customerService.updateCustomerById(customer);
                //直接向浏览器响应字符串需要使用@ResponseBody
                return "OK";
                
            }

        7删除客户
        7.1需求

        点击客户列表中的删除按钮,提示“警告信息”

        点击确定后删除用户信息,并刷新页面。

        分析:
        请求的url:customer/delete.action

        参数:id(客户id)
        响应的内容:OK(字符串需要使用@ResponseBody)

 

你可能感兴趣的:(SSM框架整合之练习篇)