Mybatis、Springmvc练习
CRM系统
eclipse:mars2
Jdk:1.7.0_80
数据库:mysql
框架:mybatis(3.2.7)、spring4.2.4、springmvc(4.2.4)
数据库使用mysql 数据库。
1、创建crm数据库
2、将参考资料中的sql脚本导入到数据库中
工程使用Springmvc、spring、mybatis框架整合完成。
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> |
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:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<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 |
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> |
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> |
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> |
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-crmdisplay-name> <welcome-file-list> <welcome-file>index.htmlwelcome-file> <welcome-file>index.htmwelcome-file> <welcome-file>index.jspwelcome-file> <welcome-file>default.htmlwelcome-file> <welcome-file>default.htmwelcome-file> <welcome-file>default.jspwelcome-file> welcome-file-list>
<filter> <filter-name>CharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>utf-8param-value> init-param> filter> <filter-mapping> <filter-name>CharacterEncodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping>
<context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring/applicationContext-*.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener>
<servlet> <servlet-name>springmvcservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring/springmvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet>
<servlet-mapping> <servlet-name>springmvcservlet-name> <url-pattern>*.actionurl-pattern> servlet-mapping> web-app> |
第一个步:拷贝如图所示的tld文件到工程的WEB-INF (容器会自动加载此目录文件。)
第二步骤:copy 代码至util中
第三步骤:在tld/commons.tld 文件中配置好对应的类和配置项。
第四步:如图:配置
copy jsp文件到
spring 是父容器
srpingmvc 是子容器
父容器不可以访问子容器中的bean(对象),
子容器可以访问父容器中的bean
展示客户列表页面
URL:/customer/list.action
参数:暂无
返回值:jsp页面
初始化查询条件下拉列表。
Ø dao开发:先创建POJO ,以mapper代理的方式开发接口。
dao接口:
publicinterface BaseDictDao { /** * 根据typeCode 查询字典表中的数据(客户信息来源。。) * @param typeCode * @return */ public List } |
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 List List //3.设置模型数据 model.addAttribute("fromType", custResourcelist); model.addAttribute("industryType", custIndustrylist); model.addAttribute("levelType", custLevellist); //4.返回即可 return"customer"; } |
展示商品列表,并且可以根据查询条件过滤查询结果,并且实现分页处理。
Ø 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方法 } |
查询的客户列表的字段需要可读性好,需要关联字典表查询。如下:
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 |
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 } |
分析需要分页查询,
Ø 改造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> |
接口:
实现类:
在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的实现类设置总记录数修改如下:
debug测试:页面查询条件无法回显,并且客户名称有乱码问题。
乱码问题:
第一种方式:在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中设置:
分页时也没问题:
推荐使用第二种。
回显问题:添加如下代码:
model.addAttribute("custName", vo.getCustName()); model.addAttribute("custSource", vo.getCustSource()); model.addAttribute("custIndustry", vo.getCustIndustry()); model.addAttribute("custLevel", vo.getCustLevel()); |
1、点击客户列表中的“修改”按钮弹出客户信息修改对话框,并初始化客户信息
2、点击“保存修改”按钮将修改后的结果保存到数据库中
点击按钮,通过客户ID查询客户信息展示在页面中。
URL: /customer/edit
参数:id 客户id
返回值:json数据
<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> |
点击客户列表中的删除按钮,提示“警告信息”
点击确定后删除用户信息,并刷新页面。