带你逐步深入了解SSM框架——淘淘商城项目之框架整合,后台系统搭建

1.  今日计划

1、  SSM框架整合

2、  mybatis逆向工程

3、  商品列表

4、  商品列表分页处理


2.  SSM框架整合

2.1. 后台系统所用的技术

框架:Spring + SpringMVC +Mybatis

前端:EasyUI

数据库:mysql

2.2. 创建数据库

1、安装mysql数据库

2、在mysql中创建一个taotao数据库

3、导入数据库脚本。

2.3. Mybatis逆向工程

执行逆向工程

使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件。

2.4. 整合思路

1、Dao层:

mybatis整合spring,通过spring管理SqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包。

整合内容

对应工程

Pojo

Taotao-mangaer-pojo

Mapper映射文件

Taotao-mangaer-mapper

Mapper接口

Taotao-mangaer-mapper

sqlmapConfig.xml

Taotao-manager-web

applicationContext-dao.xml

Taotao-manager-web


2、Service层:

所有的实现类都放到spring容器中管理。由spring创建数据库连接池,并有spring管理实务。

整合内容

对应工程

Service接口及实现类

Taotao-mangaer-service

applicationContext-service.xml

Taotao-manager-web

applicationContext-trans.xml

Taotao-manager-web


3、表现层:

Springmvc整合spring框架,由springmvc管理controller。

整合内容

对应工程

springmvc.xml

Taotao-manager-web

Controller

Taotao-manager-web

 

2.5. Dao整合

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


2.5.2.  Spring整合mybatis

创建applicationContext-dao.xml

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

 

     

     

      <context:property-placeholder location="classpath:properties/*.properties" />

     

      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

           destroy-method="close">

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

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

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

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

           <property name="maxActive" value="10" />

           <property name="minIdle" value="5" />

      bean>

     

      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

          

           <property name="dataSource" ref="dataSource" />

          

           <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />

      bean>

      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

           <property name="basePackage" value="com.taotao.mapper" />

      bean>

beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

 


备注:

Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。

Druid已经在阿里巴巴部署了超过600个应用,经过多年多生产环境大规模部署的严苛考验。

 

2.6. Service整合

2.6.1.  管理Service实现类

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

 

           <context:component-scan base-package="com.taotao.service"/>

 

beans>


2.6.2.  事务管理

创建applicationContext-trans.xml

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

     

      <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(* com.taotao.service.*.*(..))" />

      aop:config>

beans>


2.7. 表现层整合

2.7.1.  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.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.xsd">

 

      <context:component-scan base-package="com.taotao.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>


2.7.2.  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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

      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>taotao-manager-webdisplay-name>

      <welcome-file-list>

           <welcome-file>login.htmlwelcome-file>

      welcome-file-list>

     

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

 

     

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

 

 

     

      <servlet>

           <servlet-name>taotao-managerservlet-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>taotao-managerservlet-name>

           <url-pattern>/url-pattern>

      servlet-mapping>

web-app>


2.7.3.  整合静态页面

静态页面位置:02.第二天(三大框架整合,后台系统搭建)\01.参考资料\后台管理系统静态页面

带你逐步深入了解SSM框架——淘淘商城项目之框架整合,后台系统搭建_第1张图片

使用方法:

把静态页面添加到taotao-manager-web工程中的WEB-INF下:

带你逐步深入了解SSM框架——淘淘商城项目之框架整合,后台系统搭建_第2张图片

由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,包括静态资源例如css、js等。所以需要在springmvc.xml中添加资源映射标签:

      <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>

      <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>


2.8. 修改taotao-manager-mapper的pom文件

在pom文件中添加如下内容:

      <build>

           <resources>

            <resource>

                <directory>src/main/javadirectory>

                <includes>

                    <include>**/*.propertiesinclude>

                    <include>**/*.xmlinclude>

                includes>

                <filtering>falsefiltering>

            resource>

        resources>

      build>


2.9. 整合测试

根据商品id查询商品信息。

 

3.  商品列表查询

3.1. 商品列表页面

带你逐步深入了解SSM框架——淘淘商城项目之框架整合,后台系统搭建_第3张图片

对应的jsp为

item-list.jsp

 

请求的url:

/item/list

请求的参数:

page=1&rows=30

响应的json数据格式:

Easyui中datagrid控件要求的数据格式为:

{total:”2”,rows:[{“id”:”1”,”name”,”张三”},{“id”:”2”,”name”,”李四”}]}


3.2. 响应的json数据格式EasyUIResult

public class EasyUIResult {

 

    private Integer total;

 

    private List rows;

 

    public EasyUIResult(Integer total, List rows) {

        this.total = total;

        this.rows = rows;

    }

 

    public EasyUIResult(Long total, List rows) {

        this.total = total.intValue();

        this.rows = rows;

    }

 

    public Integer getTotal() {

        return total;

    }

 

    public void setTotal(Integer total) {

        this.total = total;

    }

 

    public List getRows() {

        return rows;

    }

 

    public void setRows(List rows) {

        this.rows = rows;

    }

 

}

 

3.3. 分页处理

3.3.1.  Mybatis分页插件 - PageHelper说明

如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。

该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

3.3.2.  使用方法

第一步:在Mybatis配置xml中配置拦截器插件:

<plugins>

   

    <plugininterceptor="com.github.pagehelper.PageHelper">

               

        <propertyname="dialect"value="mysql"/>

    plugin>

plugins>

第二步:在代码中使用

1、设置分页信息:

    //获取第1页,10条内容,默认查询总数count

    PageHelper.startPage(1, 10);

 

    //紧跟着的第一个select方法会被分页

List<Country> list = countryMapper.selectIf(1);

2、取分页信息

//分页后,实际返回的结果list类型是Page,如果想取出分页信息,需要强制转换为Page

Page<Country> listCountry = (Page<Country>)list;

listCountry.getTotal();

3、取分页信息的第二种方法

//获取第1页,10条内容,默认查询总数count

PageHelper.startPage(1, 10);

List<Country> list = countryMapper.selectAll();

//PageInfo对结果进行包装

PageInfo page=new PageInfo(list);

//测试PageInfo全部属性

//PageInfo包含了非常全面的分页属性

assertEquals(1, page.getPageNum());

assertEquals(10, page.getPageSize());

assertEquals(1, page.getStartRow());

assertEquals(10, page.getEndRow());

assertEquals(183, page.getTotal());

assertEquals(19, page.getPages());

assertEquals(1, page.getFirstPage());

assertEquals(8, page.getLastPage());

assertEquals(true, page.isFirstPage());

assertEquals(false, page.isLastPage());

assertEquals(false, page.isHasPreviousPage());

assertEquals(true, page.isHasNextPage());

 

3.4. Mapper

使用逆向工程生成的mapper文件。

 

3.5. Service

@Service

public class ItemServiceImpl implements ItemService {

 

      @Autowired

      private TbItemMapper itemMapper;

      @Override

      public EasyUIResult getItemList(Integer page, Integer rows) throws Exception {

           TbItemExample example = new TbItemExample();

           //设置分页

           PageHelper.startPage(page, rows);

           List list = itemMapper.selectByExample(example);

           //取分页信息

           PageInfo pageInfo = new PageInfo<>(list);

           long total = pageInfo.getTotal();

           EasyUIResult result = new EasyUIResult(total, list);

          

           returnresult;

      }

 

}

 

3.6. Controller

@Controller

@RequestMapping("/item")

public class ItemController {

     

      @Autowired

      private ItemService itemService;

 

      @RequestMapping("/list")

      //设置相应的内容为json数据

      @ResponseBody

      public EasyUIResult getItemlist(@RequestParam(defaultValue="1")Integer page,

                 @RequestParam(defaultValue="30")Integer rows) throws Exception {

           //查询商品列表

           EasyUIResult result = itemService.getItemList(page, rows);

          

           return result;

      }

}

 带你逐步深入了解SSM框架——淘淘商城项目之框架整合,后台系统搭建_第4张图片

 

你可能感兴趣的:(SSM框架,JAVA,商城,框架,电商,技术)