欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SSM框架整合之练习篇

程序员文章站 2022-07-10 21:41:59
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脚本导入到数据库中 ... ......
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
        <?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>
        3.2applicationcontext-dao.xml
        <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" 
            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">

            <!-- 配置 读取properties文件 jdbc.properties -->
            <context:property-placeholder location="classpath:jdbc.properties" />

            <!-- 配置 数据源 -->
            <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource">
                <!-- 驱动 -->
                <property name="driverclassname" value="${jdbc.driver}" />
                <!-- url -->
                <property name="url" value="${jdbc.url}" />
                <!-- 用户名 -->
                <property name="username" value="${jdbc.username}" />
                <!-- 密码 -->
                <property name="password" value="${jdbc.password}" />
            </bean>

            <!-- 配置 mybatis的工厂 -->
            <bean class="org.mybatis.spring.sqlsessionfactorybean">
                <!-- 数据源 -->
                <property name="datasource" ref="datasource" />
                <!-- 配置mybatis的核心 配置文件所在位置 -->
                <property name="configlocation" value="classpath:sqlmapconfig.xml" />
                <!-- 配置pojo别名 -->
                <property name="typealiasespackage" value="cn.baidu.core.bean"></property>
            </bean>

            <!-- 配置 1:原始dao开发 接口实现类 mapper.xml 三个 2:接口开发 接口 不写实现类 mapper.xml 二个 (userdao、productdao 
                、branddao。。。。。。。) 3:接口开发、并支持扫描 cn.baidu.core.dao(userdao。。。。。) 写在此包下即可被扫描到 -->
            <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
                <property name="basepackage" value="cn.baidu.core.dao" />
            </bean>

        </beans>

        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
        <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.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">
                
                
                <!-- 配置  扫描   @service -->
                <context:component-scan base-package="cn.baidu.core.service"/>
                
                
                
        </beans>

        3.4applicationcontext-trans.xml
        <?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.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">
            <!-- 事务管理器 -->
            <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="add*" propagation="required" />
                    <tx:method name="create*" 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="select*" 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.baidu.core.service.*.*(..))" />
            </aop:config>
        </beans>
        3.5springmvc.xml
        <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"
            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">        
                <!-- 加载属性文件 -->
                <context:property-placeholder location="classpath:resource.properties"/>
                <!-- 配置扫描 器 -->
                <context:component-scan base-package="cn.baidu.core.web.controller"/>
                <!-- 配置处理器映射器  适配器 -->
                <mvc:annotation-driven/>
                
                <!-- 配置视图解释器 jsp -->
                <bean id="jspviewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver">
                    <property name="prefix" value="/web-inf/jsp/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
                
        </beans>

        3.6web.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">
            <welcome-file-list>
                <welcome-file>customer.action</welcome-file>
            </welcome-file-list>
            <!-- 上下文的位置 -->
            <context-param>
                <param-name>contextconfiglocation</param-name>
                <param-value>classpath:applicationcontext-*.xml</param-value>
            </context-param>
            <!-- spring的监听器 -->
            <listener>
                <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
            </listener>


            <!-- post提交过滤器 utf-8 -->
            <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>*.action</url-pattern>
            </filter-mapping>
            <!-- 前端控制器 -->
            <servlet>
                <servlet-name>crm</servlet-name>
                <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
                <init-param>
                    <param-name>contextconfiglocation</param-name>
                    <!-- 此处不配置 默认找 /web-inf/[servlet-name]-servlet.xml -->
                    <param-value>classpath:springmvc.xml</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
            </servlet>
            <servlet-mapping>
                <servlet-name>crm</servlet-name>
                <!-- 1:*.do *.action 拦截以.do结尾的请求 (不拦截 jsp png jpg .js .css) 2:/ 拦截所有请求 
                    (不拦截.jsp) 建议使用此种 方式 (拦截 .js.css .png) (放行静态资源) 3:/* 拦截所有请求(包括.jsp) 此种方式 不建议使用 -->
                <url-pattern>*.action</url-pattern>
            </servlet-mapping>
        </web-app>


        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
        <?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.baidu.crm.dao.basedictdao">
            <select id="getbasedictlist" parametertype="string" resulttype="basedict">
                select * from base_dict where dict_type_code=#{typecode}
            </select>
        </mapper>

        4.4service
        @service
        public class basedictserviceimpl implements basedictservice {

            @autowired
            private basedictdao basedictdao;
            
            @override
            public list<basedict> getbasedictlist(string typecode) {
                list<basedict> 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<basedict> custsourcelist = basedictservice.getbasedictlist(customersourcecode);
                list<basedict> custindustrylist = basedictservice.getbasedictlist(customerindustrycode);
                list<basedict> 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
        <select id="getcustlist" parametertype="queryvo" resulttype="customer">
                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>
                    <if test="custname!=null and custname!=''">
                        and    cust_name like '%${custname}%'
                    </if>
                    <if test="custsource!=null and custsource!=''">
                        and cust_source = #{custsource}
                    </if>
                    <if test="custindustry!=null and custindustry!=''">
                        and cust_industry = #{custindustry}
                    </if>
                    <if test="custlevel!=null and custlevel!=''">
                        and cust_level = #{custlevel}
                    </if>
                </where>
                limit #{start},#{rows}
            </select>

        增加count后的dao
        <mapper namespace="com.baidu.crm.dao.customerdao">
            
            <sql id="cust_query_where">
                <where>
                    <if test="custname!=null and custname!=''">
                        and    cust_name like '%${custname}%'
                    </if>
                    <if test="custsource!=null and custsource!=''">
                        and cust_source = #{custsource}
                    </if>
                    <if test="custindustry!=null and custindustry!=''">
                        and cust_industry = #{custindustry}
                    </if>
                    <if test="custlevel!=null and custlevel!=''">
                        and cust_level = #{custlevel}
                    </if>
                </where>
            </sql>
            
            <select id="getcustlist" parametertype="queryvo" resulttype="customer">
                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
                <include refid="cust_query_where"/>
                limit #{start},#{rows}
            </select>
            <select id="getcustlistcount" parametertype="queryvo" resulttype="int">
                select count(*)
                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
                <include refid="cust_query_where"/>
            </select>
        </mapper>

        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<customer> getcustlist(queryvo queryvo) {
        queryvo.setstart((queryvo.getpage() - 1) * queryvo.getrows());
                list<customer> custlist = customerdao.getcustlist(queryvo);
                page<customer> page = new page<customer>();
                //设置客户列表
                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<basedict> custsourcelist = basedictservice.getbasedictlist(customersourcecode);
                list<basedict> custindustrylist = basedictservice.getbasedictlist(customerindustrycode);
                list<basedict> custlevellist = basedictservice.getbasedictlist(customerlevelcode);
                //查询客户列表
                page<customer> 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查询客户信息。
        <select id="getcustomerbyid" parametertype="long" resulttype="customer">
                select * from customer where cust_id = #{custid}
            </select>

        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 id="updatecustomerbyid" parametertype="customer">
                update customer
                <set>
                    <if test="cust_name!=null">
                    cust_name=#{cust_name},
                    </if>
                    <if test="cust_user_id!=null">
                    cust_user_id=#{cust_user_id},
                    </if>
                    <if test="cust_create_id!=null">
                    cust_create_id=#{cust_create_id},
                    </if>
                    <if test="cust_source!=null">
                    cust_source=#{cust_source},
                    </if>
                    <if test="cust_industry!=null">
                    cust_industry=#{cust_industry},
                    </if>
                    <if test="cust_level!=null">
                    cust_level=#{cust_level},
                    </if>
                    <if test="cust_linkman!=null">
                    cust_linkman=#{cust_linkman},
                    </if>
                    <if test="cust_phone!=null">
                    cust_phone=#{cust_phone},
                    </if>
                    <if test="cust_mobile!=null">
                    cust_mobile=#{cust_mobile},
                    </if>
                    <if test="cust_zipcode!=null">
                    cust_zipcode=#{cust_zipcode},
                    </if>
                    <if test="cust_address!=null">
                    cust_address=#{cust_address},
                    </if>
                    <if test="cust_createtime!=null">
                    cust_createtime=#{cust_createtime},
                    </if>
                </set>
                where
                    cust_id = #{cust_id}
            </update>
        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)