SSM+Maven商城项目——3 查询所有商品种类

1. 将sql数据导入数据库

2. 创建pojo

3. 创建dao接口与mapper映射

注意:
mapper也是一个java包,用于放xml映射
他与dao要同一级
在这里插入图片描述
ProductTypeMapper.xml如下:

<?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.itrucheng.zshop.dao.ProductTypeDao">		<!--指定对应的dao-->

    <sql id="productTypeColumn"> 	<!--这是一个sql片段-->
        id, name, status
    </sql>

    <select id="findAll" resultType="ProductType">
        select <include refid="productTypeColumn"></include>
        from t_product_type;
    </select>
</mapper>

因为dao项目下有了mapper.xml,所有也要在dao的pom.xml进行配置,同昨天一样:

<build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>

      <resource>
        <directory>src/main/resource</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>

4 配置mybatis

首先先配置数据源dataSource.properties,它的创建与spring-dao同一目录下。

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/zshop?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=***********
jdbc.initialSize=5

在backend下的spring-dao下配置

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置数据源-->
    <context:property-placeholder location="classpath:dataSource.properties" />

    <!-- 配置数据连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="initialSize" value="${jdbc.initialSize}"></property>
     </bean>

    <!--配置mybatis相关信息的-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>        <!--连接池-->
        <property name="mapperLocations" value="classpath:com/itrucheng/zshop/mapper/*.xml"></property>    <!--mapper的映射路径-->
        <!--起别名的-->
        <property name="typeAliasesPackage" value="com.itrucheng.zshop.pojo"></property> 
    </bean>
	<!--扫描器配置-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="com.itrucheng.zshop.dao"></property>
    </bean>
</beans>

5 配置service层

创建service接口,并且创建一个impl包,用于实现接口,这个包要与service接口同一级下。
在这里插入图片描述
ServiceImp如下:

package com.itrucheng.zshop.service.impl;/*
 * auth:熊汝成
 * date:2019/11/23
 * description:
 */

import com.itrucheng.zshop.dao.ProductTypeDao;
import com.itrucheng.zshop.pojo.ProductType;
import com.itrucheng.zshop.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeDao productTypeDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<ProductType> findAll() {
        return productTypeDao.findAll();
    }
}

再配置spring-service.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--扫描事务实现层组件-->
    <context:component-scan base-package="com.itrucheng.zshop.service.impl" />

    <!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--事务注解驱动打开-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

</beans>

6 书写控制器

在backend下的控制器层,调用service

package com.itrucheng.zshop.backend.controller;/*
 * auth:熊汝成
 * date:2019/11/22
 * description:
 */

import com.itrucheng.zshop.pojo.ProductType;
import com.itrucheng.zshop.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/backend/productType")
public class ProductTypeManager {

    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        //返回所有的商品类型
        List<ProductType> productTypes = productTypeService.findAll();
        model.addAttribute("productTypes", productTypes);
        return "productTypeManager";
    }
}

7 最后再到前端页面遍历数据便可

 <c:forEach items="${productTypes}" var="productType">
                    <tr>
                        <td>${productType.id}</td>
                        <td>${productType.name}</td>
                        <td>
                            <c:if test="${productType.status == 1}">启用</c:if>
                            <c:if test="${productType.status == 0}">禁用</c:if>
                        </td>
                        <td class="text-center">
                            <input type="button" class="btn btn-warning btn-sm doProTypeModify" value="修改">
                            <input type="button" class="btn btn-warning btn-sm doProTypeDelete" value="删除">
                            <input type="button" class="btn btn-danger btn-sm doProTypeDisable" value="禁用">
                        </td>
                    </tr>


                </c:forEach>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值