注意:
mapper也是一个java包,用于放xml映射
他与dao要同一级
<?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>
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>
创建service接口,并且创建一个impl包,用于实现接口,这个包要与service接口同一级下。
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>
在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";
}
}
<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>
因篇幅问题不能全部显示,请点此查看更多更全内容