SSM小Demo

来源:互联网 发布:一千零一夜知乎 编辑:程序博客网 时间:2024/06/08 01:36

 

 

 

 

Mybatis、Springmvc练习

 

CRM系统

 


 

1  数据库

数据库使用mysql 数据库。

1、创建crm数据库

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

 

2  工程搭建

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

 

2.1   SqlMapConfig.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

 

<configuration>

 

</configuration>

2.2   applicationContext-dao.xml

<beansxmlns="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.0.xsd

            http://www.springframework.org/schema/mvc

           http://www.springframework.org/schema/mvc/spring-mvc-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/task

           http://www.springframework.org/schema/task/spring-task-4.0.xsd

           http://code.alibabatech.com/schema/dubbo       

           http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 

      <!-- 配置 读取properties文件 jdbc.properties -->

      <context:property-placeholderlocation="classpath:jdbc.properties"/>

 

      <!-- 配置 数据源 -->

      <beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource">

           <!-- 驱动 -->

           <propertyname="driverClassName"value="${jdbc.driver}"/>

           <!-- url -->

           <propertyname="url"value="${jdbc.url}"/>

           <!-- 用户名 -->

           <propertyname="username"value="${jdbc.username}"/>

           <!-- 密码 -->

           <propertyname="password"value="${jdbc.password}"/>

      </bean>

 

      <!-- 配置Mybatis的工厂 -->

      <beanclass="org.mybatis.spring.SqlSessionFactoryBean">

           <!-- 数据源 -->

           <propertyname="dataSource"ref="dataSource"/>

           <!-- 配置Mybatis的核心 配置文件所在位置 -->

           <propertyname="configLocation"value="classpath:SqlMapConfig.xml"/>

           <!-- 配置pojo别名 -->

           <propertyname="typeAliasesPackage"value="cn.itcast.core.bean"></property>

      </bean>

 

      <!-- 配置 1:原始Dao开发 接口实现类 Mapper.xml 三个 2:接口开发 接口 不写实现类 Mapper.xml 二个 (UserDao、ProductDao

           、BrandDao。。。。。。。) 3:接口开发、并支持扫描 cn.itcast.core.dao(UserDao。。。。。) 写在此包下即可被扫描到 -->

      <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

           <propertyname="basePackage"value="cn.itcast.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

2.3   applicationContext-service.xml

<beansxmlns="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.0.xsd

           http://www.springframework.org/schema/mvc

           http://www.springframework.org/schema/mvc/spring-mvc-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/task

           http://www.springframework.org/schema/task/spring-task-4.0.xsd

           http://code.alibabatech.com/schema/dubbo       

           http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

          

          

           <!-- 配置 扫描   @Service -->

           <context:component-scanbase-package="cn.itcast.core.service"/>

          

          

          

</beans>

 

2.4   applicationContext-trans.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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">

      <!-- 事务管理器 -->

      <beanid="transactionManager"

           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

           <!-- 数据源 -->

           <propertyname="dataSource"ref="dataSource"/>

      </bean>

      <!-- 通知 -->

      <tx:adviceid="txAdvice"transaction-manager="transactionManager">

           <tx:attributes>

                 <!-- 传播行为 -->

                 <tx:methodname="save*"propagation="REQUIRED"/>

                 <tx:methodname="insert*"propagation="REQUIRED"/>

                 <tx:methodname="add*"propagation="REQUIRED"/>

                 <tx:methodname="create*"propagation="REQUIRED"/>

                 <tx:methodname="delete*"propagation="REQUIRED"/>

                 <tx:methodname="update*"propagation="REQUIRED"/>

                 <tx:methodname="find*"propagation="SUPPORTS"read-only="true"/>

                 <tx:methodname="select*"propagation="SUPPORTS"read-only="true"/>

                 <tx:methodname="get*"propagation="SUPPORTS"read-only="true"/>

           </tx:attributes>

      </tx:advice>

      <!-- 切面 -->

      <aop:config>

           <aop:advisoradvice-ref="txAdvice"

                 pointcut="execution(* cn.itcast.core.service.*.*(..))"/>

      </aop:config>

</beans>

2.5   Springmvc.xml

<beansxmlns="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.0.xsd

           http://www.springframework.org/schema/mvc

           http://www.springframework.org/schema/mvc/spring-mvc-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/task

           http://www.springframework.org/schema/task/spring-task-4.0.xsd

           http://code.alibabatech.com/schema/dubbo       

           http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

          

           <!-- 加载属性文件 -->

           <context:property-placeholderlocation="classpath:resource.properties"/>

           <!-- 配置扫描 器 -->

           <context:component-scanbase-package="cn.itcast.core.web.controller"/>

           <!-- 配置处理器映射器 适配器 -->

           <mvc:annotation-driven/>

          

           <!-- 配置视图解释器jsp -->

           <beanid="jspViewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                 <propertyname="prefix"value="/WEB-INF/jsp/"/>

                 <propertyname="suffix"value=".jsp"/>

           </bean>

          

</beans>

 

2.6   Web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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结尾的请求 (不拦截 jsppngjpg .js .css) 2:/ 拦截所有请求

                 (不拦截.jsp)建议使用此种 方式 (拦截 .js.css .png) (放行静态资源) 3:/* 拦截所有请求(包括.jsp) 此种方式 不建议使用 -->

           <url-pattern>*.action</url-pattern>

      </servlet-mapping>

</web-app>

 

 

2.7   加入jsp及分页标签

3  查询条件初始化

3.1   需求

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

 

4  客户列表展示

4.1   需求

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

 

5  修改客户信息

5.1   需求

1、点击客户列表中的“修改”按钮弹出客户信息修改对话框,并初始化客户信息

2、点击“保存修改”按钮将修改后的结果保存到数据库中

 

6  删除客户

6.1   需求

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

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