Mybatis、Springmvc练习
CRM系统
1 开发环境
eclipse:mars2
Jdk:1.7.0_80
数据库:mysql
框架:mybatis(3.2.7)、spring4.2.4、springmvc(4.2.4)
2 数据库
数据库使用mysql 数据库。
1、创建crm数据库
2、将参考资料中的sql脚本导入到数据库中
3 工程搭建
工程使用Springmvc、spring、mybatis框架整合完成。
3.1 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> |
3.2 applicationContext-dao.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">
<!-- 配置读取properties文件 jdbc.properties --> <context:property-placeholder location="classpath:db.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:mybatis/SqlMapConfig.xml" /> <!-- 配置pojo别名 --> <property name="typeAliasesPackage" value="com.itheima.ssm.pojo"></property> </bean>
<!--接口开发、并支持扫描 写在此包下即可被扫描到 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itheima.ssm.dao" /> </bean> </beans> |
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8 jdbc.username=root jdbc.password=itcast |
3.3 applicationContext-service.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"> <context:component-scan base-package="com.itheima.ssm.service" /> </beans> |
3.4 applicationContext-transaction.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="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(* com.itheima.ssm.service.*.*(..))"/> </aop:config> </beans> |
3.5 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:mvc="http://www.springframework.org/schema/mvc" 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"> <context:component-scan base-package="com.itheima.ssm.controller" /> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans> |
3.6 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>boot-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> <!-- 解决POST乱码 --> <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> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/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:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置URL拦截形式 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app> |
3.7 加入jsp及分页标签
第一个步:拷贝如图所示的tld文件到工程的WEB-INF (容器会自动加载此目录文件。)
第二步骤:copy 代码至util中
第三步骤:在tld/commons.tld 文件中配置好对应的类和配置项。
第四步:如图:配置
copy jsp文件到
4 工程搭建后的结构
5 父子容器
spring 是父容器
srpingmvc 是子容器
父容器不可以访问子容器中的bean(对象),
子容器可以访问父容器中的bean
6 查询条件初始化
展示客户列表页面
URL:/customer/list.action
参数:暂无
返回值:jsp页面
6.1 需求
初始化查询条件下拉列表。
Ø dao开发:先创建POJO ,以mapper代理的方式开发接口。
dao接口:
publicinterface BaseDictDao { /** * 根据typeCode 查询字典表中的数据(客户信息来源。。) * @param typeCode * @return */ public List<BaseDict> getBaseDictListByTypeCode(String typeCode); } |
sql映射文件:
<?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.itheima.ssm.dao.BaseDictDao"> <select id="getBaseDictListByTypeCode" parameterType="string" resultType="com.itheima.ssm.po.BaseDict"> SELECT * FROM base_dict d WHERE d.dict_type_code = #{typeCode} </select> </mapper> |
Ø service及实现类
Ø controller
@RequestMapping("/customer/list") public String listCustomer(Model model){ //获取下拉列表的值,展示在页面 //1.注入service //2.调用service的方法获取值
List<BaseDict> custResourcelist = service.getBaseDictListByTypeCode(Constants.CON_cust_source); List<BaseDict> custIndustrylist = service.getBaseDictListByTypeCode(Constants.CON_cust_industry); List<BaseDict> custLevellist = service.getBaseDictListByTypeCode(Constants.CON_cust_level); //3.设置模型数据 model.addAttribute("fromType", custResourcelist); model.addAttribute("industryType", custIndustrylist); model.addAttribute("levelType", custLevellist); //4.返回即可 return"customer"; } |
7 客户列表展示
7.1 需求
展示商品列表,并且可以根据查询条件过滤查询结果,并且实现分页处理。
7.2 分析
Ø URL的分析:
首次访问该页面的请求的URL应该和点击查询时提交表单时的URL是一样的。只是一个有查询条件,一个没有. 后台可以使用动态sql判断,有值则查询。
Ø 参数的分析
有四个查询条件,可以封装到QueryVo中
Ø 返回值的分析
返回为JSP页面,需要传递模型数据的值到页面中(分页的信息)
URL:/customer/list
参数 QueryVo
返回值:页面
QueryVo:类中的属性和页面中的名字一致
publicclass QueryVo { private String custName; private String custSource; private String custIndustry; private String custLevel; //getter setter方法 } |
7.3 dao的开发
7.3.1 示例sql语句:
查询的客户列表的字段需要可读性好,需要关联字典表查询。如下:
SELECT cust_id, cust_name, a.dict_item_name as cust_source, b.dict_item_name as cust_industry, d.dict_item_name as cust_level, cust_phone, cust_mobile FROM customer c LEFT JOIN base_dict a ON c.cust_source = a.dict_id LEFT JOIN base_dict b ON c.cust_industry = b.dict_id LEFT JOIN base_dict d ON c.cust_level = d.dict_id WHERE cust_name LIKE '%马云%' AND cust_source = 6 AND cust_industry = 2 AND cust_level = 22 |
7.3.2 POJO
publicclass Customer { private Long cust_id; private String cust_name; private Long cust_user_id; private Long cust_create_id; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; private String cust_zipcode; private String cust_address; private Date cust_createtime; //getter setter } |
7.3.3 Mapper接口
7.3.4 mapper映射文件:
分析需要分页查询,
Ø 改造queryVo
注意:一定是page 字段,因为分页时传递过来的参数的名称就是page,如果名称不一致接收不到数据。
mapper映射文件中:limit #{start},#{size}
<?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.itheima.ssm.mapper.CustomerMapper"> <select id="getCustomerListByQueryVo" parameterType="com.itheima.ssm.pojo.QueryVo" resultType="com.itheima.ssm.pojo.Customer"> SELECT cust_id, cust_name, a.dict_item_name as cust_source, b.dict_item_name as cust_industry, d.dict_item_name as cust_level, cust_phone, cust_mobile FROM customer c LEFT JOIN base_dict a ON c.cust_source = a.dict_id LEFT JOIN base_dict b ON c.cust_industry = b.dict_id LEFT JOIN base_dict d ON c.cust_level = d.dict_id <where> <if test="custName!=null and custName!=''"> and c.cust_name LIKE '%${custName}%' </if> <if test="custSource!=null and custSource!=''"> AND c.cust_source = #{custSource} </if> <if test="custIndustry!=null and custIndustry!=''"> AND c.cust_industry = #{custIndustry} </if> <if test="custLevel!=null and custLevel!=''"> AND c.cust_level = #{custLevel} </if> </where> limit #{start},#{size} </select> </mapper> |
7.4 Service的开发
接口:
实现类:
在mapper接口中添加统计总记录数的方法
mapper映射文件改造:
<?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.itheima.ssm.mapper.CustomerMapper"> <sql id="where_case"> <where> <if test="custName!=null and custName!=''"> and c.cust_name LIKE '%${custName}%' </if> <if test="custSource!=null and custSource!=''"> AND c.cust_source = #{custSource} </if> <if test="custIndustry!=null and custIndustry!=''"> AND c.cust_industry = #{custIndustry} </if> <if test="custLevel!=null and custLevel!=''"> AND c.cust_level = #{custLevel} </if> </where> </sql>
<select id="getCustomerListByQueryVo" parameterType="com.itheima.ssm.pojo.QueryVo" resultType="com.itheima.ssm.pojo.Customer"> SELECT cust_id, cust_name, a.dict_item_name as cust_source, b.dict_item_name as cust_industry, d.dict_item_name as cust_level, cust_phone, cust_mobile FROM customer c LEFT JOIN base_dict a ON c.cust_source = a.dict_id LEFT JOIN base_dict b ON c.cust_industry = b.dict_id LEFT JOIN base_dict d ON c.cust_level = d.dict_id <include refid="where_case"/> limit #{start},#{size} </select>
<select id="getCountCustomer" parameterType="com.itheima.ssm.pojo.QueryVo" resultType="int"> SELECT count(*) FROM customer c LEFT JOIN base_dict a ON c.cust_source = a.dict_id LEFT JOIN base_dict b ON c.cust_industry = b.dict_id LEFT JOIN base_dict d ON c.cust_level = d.dict_id <include refid="where_case"/> </select> </mapper> |
service的实现类设置总记录数修改如下:
7.5 controller
debug测试:页面查询条件无法回显,并且客户名称有乱码问题。
7.5.1 解决乱码和回显问题
乱码问题:
第一种方式:在controller方法中添加如下代码,但是分页时会有问题。
try { if(StringUtils.isNotBlank(vo.getCustName())) vo.setCustName(new String(vo.getCustName().getBytes("iso-8859-1"),"utf-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } |
第二种方式:在tomcat的conf/server.xml中设置:
<Connector URIEncoding="UTF-8"connectionTimeout="20000" port="8080"protocol="HTTP/1.1" redirectPort="8443"/>
分页时也没问题:
推荐使用第二种。
回显问题:添加如下代码:
model.addAttribute("custName", vo.getCustName()); model.addAttribute("custSource", vo.getCustSource()); model.addAttribute("custIndustry", vo.getCustIndustry()); model.addAttribute("custLevel", vo.getCustLevel()); |
8 修改客户信息
8.1 需求
1、点击客户列表中的“修改”按钮弹出客户信息修改对话框,并初始化客户信息
2、点击“保存修改”按钮将修改后的结果保存到数据库中
8.2 页面分析
点击按钮,通过客户ID查询客户信息展示在页面中。
URL: /customer/edit
参数:id 客户id
返回值:json数据
8.3 dao的开发
8.3.1 接口
8.3.2 mapper映射文件
<update id="updateCustomer" parameterType="com.itheima.crm.pojo.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> |
8.4 service的开发
8.4.1 接口
8.4.2 实现类
9 删除客户
9.1 需求
点击客户列表中的删除按钮,提示“警告信息”
点击确定后删除用户信息,并刷新页面。