ssm总结(二)

该博客围绕CRM系统展开Mybatis、Springmvc练习。使用MySQL数据库,创建crm数据库并导入sql脚本。工程采用Springmvc、spring、mybatis框架整合。还涉及查询条件初始化、客户列表展示、修改客户信息、删除客户等功能,各功能有明确需求。

 

 

 

 

MybatisSpringmvc练习

 

CRM系统

 

 

 

  1. 数据库

数据库使用mysql 数据库。

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

 

  1. 工程搭建

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

 

  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>

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

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

 

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

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

 

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

 

 

  1. 加入jsp及分页标签

  1. 查询条件初始化
  2. 需求

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

 

  1. 客户列表展示
  2. 需求

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

 

  1. 修改客户信息
  2. 需求

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

 

  1. 删除客户
  2. 需求

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

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

### SSM框架中的注解及其使用总结 SSM(Spring + Spring MVC + MyBatis)是一种常见的Java开发架构组合,其中涉及了大量的注解来简化配置和增强功能。以下是关于SSM框架中常用注解的总结以及其具体用法。 #### 一、Spring核心注解 1. **@Component** - 表示该类是一个组件,可以被Spring容器管理。 - 常用于标记普通的业务逻辑层或工具类[^1]。 2. **@Service** - 是@Component的一个特化版本,专门用来标注服务层的Bean。 - 提高代码可读性和语义清晰度。 3. **@Controller** - 标记控制器层的类,通常配合@RequestMapping一起使用。 - 主要负责接收HTTP请求并返回模型数据给前端页面[^2]。 4. **@Repository** - 用于标注数据访问层(DAO层),表示这是一个持久化的操作单元。 - 可以捕获数据库异常并将它们转换成Spring定义的数据访问异常体系。 5. **@Autowired** - 自动装配依赖项,默认按照类型匹配注入相应的Bean实例。 - 如果存在多个相同类型的Bean,则可以通过`@Qualifier`指定具体的Bean名称。 6. **@Resource** - 和@Autowired类似,但它优先按名字查找目标Bean;如果找不到再尝试通过类型定位。 - 需要注意的是@Resource属于JSR-250标准的一部分而非Spring专有特性。 7. **@Value** - 从外部资源文件或者环境变量中加载属性值到字段上。 - 支持占位符语法`${propertyName}`以便动态替换实际值。 8. **@Configuration & @Bean** - 定义基于Java Config方式创建Spring上下文中所需的Beans。 - `@Configuration`声明当前类为配置类,而@Bean则用于方法级别指示此方法会生产一个bean供spring ioc container管理。 9. **@Scope** - 设置bean的作用域范围,比如singleton(单例模式), prototype(多例模式)等等。 - 默认情况下所有的非web相关的beans都是singletons unless otherwise specified via this annotation or xml configuration. --- #### 、MyBatis相关注解 1. **@Mapper** - 将接口映射成为mybatis mapper interface,使得可以直接调用sql查询而不必编写xml形式的mapper file. - 这种做法减少了繁琐的手工维护工作量同时也提高了灵活性。 2. **@Select / @Insert / @Update / @Delete** - 分别对应四种基本SQL命令的操作。 - 直接写入原生SQL字符串作为参数传递即可完成相应CRUD动作。 3. **@Param** - 当方法接受两个及以上参数传参至sql时需要用到@param关键字明确指出各个参数的名字便于区分识别。 4. **@Results & @Result** - 描述如何把结果集映射回java对象实体类成员变量上去的过程。 - 其中@result单独处理某个列名与属性之间的关系[@^2] --- #### 三、Spring MVC常见注解 1. **@RequestMapping** - 映射http requests onto specific handler classes and/or methods within them. - 能够精确控制url路径风格支持ant-style pattern matching like "/foo/**", "*.do" etc. 2. **@GetMapping/@PostMapping/@PutMapping/@DeleteMapping** - 更加直观简洁的形式分别代表get post put delete http method mappings respectively. - 减少了重复书写@RequestMapping(method=RequestMethod.GET/POST...)的情况提升了编码效率. 3. **@ResponseBody** - 返回json/xml格式的内容而不是view name to resolve view resolver logic. - 结合RestController共同作用下可以让整个controller默认开启responsebody行为无需逐一手动添加. 4. **@RequestBody** - 解析request body里面携带过来的信息自动封装进对应的pojo class instance里去. - 特别适合restful api场景下的post request data binding process.[^1] 5. **@PathVariable** - 获取uri template variables inside url path segments such as {id} part of '/users/{id}' style urls. - 方便实现RESTFUL URL设计原则的同时也增强了程序健壮性因为避免了手动split string带来的潜在风险. ```java @RestController public class UserController { @GetMapping("/user/{userId}") public User getUserById(@PathVariable String userId){ // business logic here... return new User(); } } ``` --- #### 四、事务管理相关注解 1. **@Transactional** - 应用于service layer methods ensuring that all database operations performed during execution are treated atomically together forming one single unit work either succeed completely fail entirely rollback changes made so far if any exception occurs. --- ### 总结 上述列举了一些常用的ssm framework annotations along with their typical usages covering aspects ranging from dependency injection over persistence access up until web development best practices using spring mvc features effectively.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值