003 SSM框架整合

文章目录

  • 整合
    • web.xml
    • applicationContext-dao.xml
    • applicationContext-service.xml
    • springmvc.xml
    • db.properties
    • log4j.properties
    • pom.xml
  • 测试
    • sql
    • ItemController.java
    • ItemMapper.java
    • Item.java
    • ItemExample.java
    • ItemService.java
    • ItemServiceImpl.java
    • ItemMapper.xml

整合

将工程的三层结构中的 JavaBean 分别使用 Spring容器 (通过XML方式)进行管理。

  1. 整合持久层 mapper ,包括 数据源 、 SqlSessionFactory 及 mapper 代理对象的整合;
  2. 整合业务层 Service ,包括 事务Bean 及 service 的 bean 的配置;
  3. 整合表现层 Controller ,直接使用 springmvc 的配置。
  4. Web.xml 加载 spring 容器(包含多个XML文件,还分为 父子容器 )

核心配置文件:
applicationContext-dao.xml
applicationContext-service.xml
springmvc.xml
web.xml

web.xml


<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><!--        <?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>Archetype Created Web Application</display-name><!-- 配置springmvc的前端控制器 --><servlet><servlet-name>ssm</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><servlet-mapping><servlet-name>ssm</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--  在web.xml中,使用监听器来对spring的配置文件进行加载:--><!-- 指定持久层和业务层的spring配置文件路径--><!-- 加载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></web-app>

applicationContext-dao.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"><!-- 加载db.properties --><context:property-placeholder location="classpath:db.properties" /><!-- 配置数据源 -->
<!--    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"-->
<!--          destroy-method="close">-->
<!--        <property name="driverClassName" value="${jdbc.driver}" />-->
<!--        <property name="url" value="${jdbc.url}" />-->
<!--        <property name="username" value="${jdbc.username}" />-->
<!--        <property name="password" value="${jdbc.password}" />-->
<!--        <property name="maxActive" value="30" />-->
<!--        <property name="maxIdle" value="5" />-->
<!--    </bean>--><!-- 配置数据源为HikariCP -->
<!--    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"-->
<!--          destroy-method="close">-->
<!--        <property name="driverClassName" value="${jdbc.driver}" />-->
<!--        &lt;!&ndash; 注意:HikariCP 使用 jdbcUrl 而不是 url &ndash;&gt;-->
<!--        <property name="jdbcUrl" value="${jdbc.url}" />-->
<!--        <property name="username" value="${jdbc.username}" />-->
<!--        <property name="password" value="${jdbc.password}" />-->
<!--        &lt;!&ndash; HikariCP 的连接池大小配置 &ndash;&gt;-->
<!--        <property name="maximumPoolSize" value="30" />-->
<!--        &lt;!&ndash; HikariCP 没有直接的 maxIdle 属性,但可以通过 minimumIdle 控制空闲连接数 &ndash;&gt;-->
<!--        <property name="minimumIdle" value="5" />-->
<!--        &lt;!&ndash; 其他可选配置,如连接超时、空闲连接超时等 &ndash;&gt;-->
<!--        <property name="connectionTimeout" value="30000" />-->
<!--        <property name="idleTimeout" value="600000" />-->
<!--        <property name="maxLifetime" value="1800000" />-->
<!--    </bean>--><bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxTotal" value="30" /> <!-- DBCP2 中使用 maxTotal 替代 maxActive --><property name="maxIdle" value="5" /><property name="minIdle" value="0" /> <!-- 可选,设置最小空闲连接数 --><!-- 可选的其他配置,如连接验证、超时设置等 --><property name="validationQuery" value="SELECT 1" /> <!-- 用于验证从池中取出的连接是否仍然有效 --><property name="testOnBorrow" value="true" /> <!-- 从池中取出连接时是否进行验证 --><property name="testOnReturn" value="false" /> <!-- 连接归还到池中时是否进行验证 --><property name="testWhileIdle" value="true" /> <!-- 连接空闲时是否进行验证,如果为true,还需要设置timeBetweenEvictionRunsMillis属性 --><property name="timeBetweenEvictionRunsMillis" value="30000" /> <!-- 连接空闲时检测空闲连接的时间间隔 --><property name="numTestsPerEvictionRun" value="3" /> <!-- 每次检测空闲连接时检测的连接数 --><property name="minEvictableIdleTimeMillis" value="1800000" /> <!-- 连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值 --></bean><!-- 配置SqlSessionFacotory --><bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 加载mybatis的配置文件(如果配置文件中没有配置项,可以忽略该文件)-->
<!--        <property name="configLocation"-->
<!--                  value="classpath:mybatis/SqlMapConfig.xml" />--><!-- 配置数据源 --><property name="dataSource" ref="dataSource" /><property name="typeAliasesPackage" value="com.ssm.po"></property></bean><!-- 配置mapper扫描器,SqlSessionConfig.xml中的mapper配置去掉 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 指定扫描的包 --><property name="basePackage" value="com.ssm.mapper" /></bean>
</beans>

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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 扫描Service --><context:component-scan base-package="com.ssm.service" /><!-- 配置事务 --><!-- 事务管理器,对mybatis操作数据库进行事务控制,此处使用jdbc的事务控制 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 指定要进行事务管理的数据源 --><property name="dataSource" ref="dataSource"></property></bean><!-- 通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 传播行为 --><tx:method name="save*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="del*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="modify*" propagation="REQUIRED" /><tx:method name="find*" read-only="true" /><tx:method name="query*" read-only="true" /><tx:method name="select*" read-only="true" /><tx:method name="get*" read-only="true" /></tx:attributes></tx:advice><!-- aop -->
<!--    <aop:config>-->
<!--        <aop:advisor advice-ref="txAdvice"-->
<!--                     pointcut="execution(* com.ssm.service.impl.*.*(..))" />-->
<!--    </aop:config>--><aop:config><aop:advisor advice-ref="txAdvice"pointcut="execution(* *..*.*ServiceImpl.*(..))" /></aop:config>
</beans>

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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置三大组件之处理器映射器和处理器适配器 --><mvc:annotation-driven /><!-- 配置三大组件之视图解析器 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 配置处理器Bean的读取 --><!-- 扫描controller注解,多个包中间使用半角逗号分隔--><!-- 使用注解的handler可以使用组件扫描器,加载handler --><context:component-scan base-package="com.ssm.controller" />
</beans>

db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=root

log4j.properties


#dev env [debug] product env [info]
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>ssm</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>ssm Maven Webapp</name><url>http://maven.apache.org</url><dependencies>
<!--    <dependency>-->
<!--      <groupId>junit</groupId>-->
<!--      <artifactId>junit</artifactId>-->
<!--      <version>3.8.1</version>-->
<!--      <scope>test</scope>-->
<!--    </dependency>--><!-- 持久层依赖 开始 --><!-- spring ioc组件需要的依赖包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>5.0.7.RELEASE</version></dependency><!-- spring 事务管理和JDBC依赖包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.7.RELEASE</version></dependency><!-- mysql数据库驱动包 --><!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.3.0</version></dependency><!-- dbcp连接池的依赖包 -->
<!--    <dependency>-->
<!--      <groupId>commons-dbcp</groupId>-->
<!--      <artifactId>commons-dbcp</artifactId>-->
<!--      <version>1.4</version>-->
<!--    </dependency>--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-dbcp2</artifactId><version>2.12.0</version></dependency><!--      <dependency>-->
<!--          <groupId>com.zaxxer</groupId>-->
<!--          <artifactId>HikariCP</artifactId>-->
<!--          <version>4.0.3</version>-->
<!--      </dependency>--><!-- mybatis依赖 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.16</version></dependency><!-- mybatis和spring的整合依赖 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><!-- 持久层依赖 结束 --><!-- 业务层依赖 开始 --><!-- 基于AspectJ的aop依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><!-- 业务层依赖 结束 --><!-- 表现层依赖 开始 --><!-- spring MVC依赖包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.7.RELEASE</version></dependency><!-- jstl 取决于视图对象是否是JSP --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-oxm</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.10</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.6</version></dependency><!-- 表现层依赖 结束 --><!-- spring 单元测试组件包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.7.RELEASE</version></dependency><!-- 单元测试Junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!-- Mock测试使用的json-path依赖 --><dependency><groupId>com.jayway.jsonpath</groupId><artifactId>json-path</artifactId><version>2.2.0</version></dependency><!-- 文件上传 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency></dependencies><build><finalName>ssm</finalName><plugins><!-- 配置Maven的JDK编译级别 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><path>/</path><port>80</port></configuration></plugin></plugins></build>
</project>

测试

表现层

  • 请求URL:/queryItem
  • 请求参数:无
  • 请求返回值:json格式数据

业务层

  • 业务处理逻辑(需求分析):实现商品列表的查询

持久层

  • 只针对表进行增删改查操作

sql


CREATE TABLE `item` (  `id` INT NOT NULL AUTO_INCREMENT,  `name` VARCHAR(255) DEFAULT NULL,  `price` FLOAT DEFAULT NULL,  `pic` VARCHAR(255) DEFAULT NULL,  `createtime` DATETIME DEFAULT NULL,  PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ItemController.java


package com.ssm.controller;import com.ssm.po.Item;
import com.ssm.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller
public class ItemController {@Autowiredprivate ItemService service;@RequestMapping("/queryItem")@ResponseBodypublic List<Item> queryItem() {
// 根据查询条件去数据库中查询商品列表List<Item> itemList = service.queryItemList();return itemList;}
}

ItemMapper.java


package com.ssm.mapper;import com.ssm.po.Item;
import com.ssm.po.ItemExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;public interface ItemMapper {int countByExample(ItemExample example);int deleteByExample(ItemExample example);int deleteByPrimaryKey(Integer id);int insert(Item record);int insertSelective(Item record);List<Item> selectByExample(ItemExample example);Item selectByPrimaryKey(Integer id);int updateByExampleSelective(@Param("record") Item record, @Param("example") ItemExample example);int updateByExample(@Param("record") Item record, @Param("example") ItemExample example);int updateByPrimaryKeySelective(Item record);int updateByPrimaryKey(Item record);
}

Item.java


package com.ssm.po;import java.util.Date;public class Item {private Integer id;private String name;private Float price;private String pic;private Date createtime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name == null ? null : name.trim();}public Float getPrice() {return price;}public void setPrice(Float price) {this.price = price;}public String getPic() {return pic;}public void setPic(String pic) {this.pic = pic == null ? null : pic.trim();}public Date getCreatetime() {return createtime;}public void setCreatetime(Date createtime) {this.createtime = createtime;}
}

ItemExample.java


package com.ssm.po;import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class ItemExample {protected String orderByClause;protected boolean distinct;protected List<Criteria> oredCriteria;public ItemExample() {oredCriteria = new ArrayList<Criteria>();}public void setOrderByClause(String orderByClause) {this.orderByClause = orderByClause;}public String getOrderByClause() {return orderByClause;}public void setDistinct(boolean distinct) {this.distinct = distinct;}public boolean isDistinct() {return distinct;}public List<Criteria> getOredCriteria() {return oredCriteria;}public void or(Criteria criteria) {oredCriteria.add(criteria);}public Criteria or() {Criteria criteria = createCriteriaInternal();oredCriteria.add(criteria);return criteria;}public Criteria createCriteria() {Criteria criteria = createCriteriaInternal();if (oredCriteria.size() == 0) {oredCriteria.add(criteria);}return criteria;}protected Criteria createCriteriaInternal() {Criteria criteria = new Criteria();return criteria;}public void clear() {oredCriteria.clear();orderByClause = null;distinct = false;}protected abstract static class GeneratedCriteria {protected List<Criterion> criteria;protected GeneratedCriteria() {super();criteria = new ArrayList<Criterion>();}public boolean isValid() {return criteria.size() > 0;}public List<Criterion> getAllCriteria() {return criteria;}public List<Criterion> getCriteria() {return criteria;}protected void addCriterion(String condition) {if (condition == null) {throw new RuntimeException("Value for condition cannot be null");}criteria.add(new Criterion(condition));}protected void addCriterion(String condition, Object value, String property) {if (value == null) {throw new RuntimeException("Value for " + property + " cannot be null");}criteria.add(new Criterion(condition, value));}protected void addCriterion(String condition, Object value1, Object value2, String property) {if (value1 == null || value2 == null) {throw new RuntimeException("Between values for " + property + " cannot be null");}criteria.add(new Criterion(condition, value1, value2));}public Criteria andIdIsNull() {addCriterion("id is null");return (Criteria) this;}public Criteria andIdIsNotNull() {addCriterion("id is not null");return (Criteria) this;}public Criteria andIdEqualTo(Integer value) {addCriterion("id =", value, "id");return (Criteria) this;}public Criteria andIdNotEqualTo(Integer value) {addCriterion("id <>", value, "id");return (Criteria) this;}public Criteria andIdGreaterThan(Integer value) {addCriterion("id >", value, "id");return (Criteria) this;}public Criteria andIdGreaterThanOrEqualTo(Integer value) {addCriterion("id >=", value, "id");return (Criteria) this;}public Criteria andIdLessThan(Integer value) {addCriterion("id <", value, "id");return (Criteria) this;}public Criteria andIdLessThanOrEqualTo(Integer value) {addCriterion("id <=", value, "id");return (Criteria) this;}public Criteria andIdIn(List<Integer> values) {addCriterion("id in", values, "id");return (Criteria) this;}public Criteria andIdNotIn(List<Integer> values) {addCriterion("id not in", values, "id");return (Criteria) this;}public Criteria andIdBetween(Integer value1, Integer value2) {addCriterion("id between", value1, value2, "id");return (Criteria) this;}public Criteria andIdNotBetween(Integer value1, Integer value2) {addCriterion("id not between", value1, value2, "id");return (Criteria) this;}public Criteria andNameIsNull() {addCriterion("name is null");return (Criteria) this;}public Criteria andNameIsNotNull() {addCriterion("name is not null");return (Criteria) this;}public Criteria andNameEqualTo(String value) {addCriterion("name =", value, "name");return (Criteria) this;}public Criteria andNameNotEqualTo(String value) {addCriterion("name <>", value, "name");return (Criteria) this;}public Criteria andNameGreaterThan(String value) {addCriterion("name >", value, "name");return (Criteria) this;}public Criteria andNameGreaterThanOrEqualTo(String value) {addCriterion("name >=", value, "name");return (Criteria) this;}public Criteria andNameLessThan(String value) {addCriterion("name <", value, "name");return (Criteria) this;}public Criteria andNameLessThanOrEqualTo(String value) {addCriterion("name <=", value, "name");return (Criteria) this;}public Criteria andNameLike(String value) {addCriterion("name like", value, "name");return (Criteria) this;}public Criteria andNameNotLike(String value) {addCriterion("name not like", value, "name");return (Criteria) this;}public Criteria andNameIn(List<String> values) {addCriterion("name in", values, "name");return (Criteria) this;}public Criteria andNameNotIn(List<String> values) {addCriterion("name not in", values, "name");return (Criteria) this;}public Criteria andNameBetween(String value1, String value2) {addCriterion("name between", value1, value2, "name");return (Criteria) this;}public Criteria andNameNotBetween(String value1, String value2) {addCriterion("name not between", value1, value2, "name");return (Criteria) this;}public Criteria andPriceIsNull() {addCriterion("price is null");return (Criteria) this;}public Criteria andPriceIsNotNull() {addCriterion("price is not null");return (Criteria) this;}public Criteria andPriceEqualTo(Float value) {addCriterion("price =", value, "price");return (Criteria) this;}public Criteria andPriceNotEqualTo(Float value) {addCriterion("price <>", value, "price");return (Criteria) this;}public Criteria andPriceGreaterThan(Float value) {addCriterion("price >", value, "price");return (Criteria) this;}public Criteria andPriceGreaterThanOrEqualTo(Float value) {addCriterion("price >=", value, "price");return (Criteria) this;}public Criteria andPriceLessThan(Float value) {addCriterion("price <", value, "price");return (Criteria) this;}public Criteria andPriceLessThanOrEqualTo(Float value) {addCriterion("price <=", value, "price");return (Criteria) this;}public Criteria andPriceIn(List<Float> values) {addCriterion("price in", values, "price");return (Criteria) this;}public Criteria andPriceNotIn(List<Float> values) {addCriterion("price not in", values, "price");return (Criteria) this;}public Criteria andPriceBetween(Float value1, Float value2) {addCriterion("price between", value1, value2, "price");return (Criteria) this;}public Criteria andPriceNotBetween(Float value1, Float value2) {addCriterion("price not between", value1, value2, "price");return (Criteria) this;}public Criteria andPicIsNull() {addCriterion("pic is null");return (Criteria) this;}public Criteria andPicIsNotNull() {addCriterion("pic is not null");return (Criteria) this;}public Criteria andPicEqualTo(String value) {addCriterion("pic =", value, "pic");return (Criteria) this;}public Criteria andPicNotEqualTo(String value) {addCriterion("pic <>", value, "pic");return (Criteria) this;}public Criteria andPicGreaterThan(String value) {addCriterion("pic >", value, "pic");return (Criteria) this;}public Criteria andPicGreaterThanOrEqualTo(String value) {addCriterion("pic >=", value, "pic");return (Criteria) this;}public Criteria andPicLessThan(String value) {addCriterion("pic <", value, "pic");return (Criteria) this;}public Criteria andPicLessThanOrEqualTo(String value) {addCriterion("pic <=", value, "pic");return (Criteria) this;}public Criteria andPicLike(String value) {addCriterion("pic like", value, "pic");return (Criteria) this;}public Criteria andPicNotLike(String value) {addCriterion("pic not like", value, "pic");return (Criteria) this;}public Criteria andPicIn(List<String> values) {addCriterion("pic in", values, "pic");return (Criteria) this;}public Criteria andPicNotIn(List<String> values) {addCriterion("pic not in", values, "pic");return (Criteria) this;}public Criteria andPicBetween(String value1, String value2) {addCriterion("pic between", value1, value2, "pic");return (Criteria) this;}public Criteria andPicNotBetween(String value1, String value2) {addCriterion("pic not between", value1, value2, "pic");return (Criteria) this;}public Criteria andCreatetimeIsNull() {addCriterion("createtime is null");return (Criteria) this;}public Criteria andCreatetimeIsNotNull() {addCriterion("createtime is not null");return (Criteria) this;}public Criteria andCreatetimeEqualTo(Date value) {addCriterion("createtime =", value, "createtime");return (Criteria) this;}public Criteria andCreatetimeNotEqualTo(Date value) {addCriterion("createtime <>", value, "createtime");return (Criteria) this;}public Criteria andCreatetimeGreaterThan(Date value) {addCriterion("createtime >", value, "createtime");return (Criteria) this;}public Criteria andCreatetimeGreaterThanOrEqualTo(Date value) {addCriterion("createtime >=", value, "createtime");return (Criteria) this;}public Criteria andCreatetimeLessThan(Date value) {addCriterion("createtime <", value, "createtime");return (Criteria) this;}public Criteria andCreatetimeLessThanOrEqualTo(Date value) {addCriterion("createtime <=", value, "createtime");return (Criteria) this;}public Criteria andCreatetimeIn(List<Date> values) {addCriterion("createtime in", values, "createtime");return (Criteria) this;}public Criteria andCreatetimeNotIn(List<Date> values) {addCriterion("createtime not in", values, "createtime");return (Criteria) this;}public Criteria andCreatetimeBetween(Date value1, Date value2) {addCriterion("createtime between", value1, value2, "createtime");return (Criteria) this;}public Criteria andCreatetimeNotBetween(Date value1, Date value2) {addCriterion("createtime not between", value1, value2, "createtime");return (Criteria) this;}}public static class Criteria extends GeneratedCriteria {protected Criteria() {super();}}public static class Criterion {private String condition;private Object value;private Object secondValue;private boolean noValue;private boolean singleValue;private boolean betweenValue;private boolean listValue;private String typeHandler;public String getCondition() {return condition;}public Object getValue() {return value;}public Object getSecondValue() {return secondValue;}public boolean isNoValue() {return noValue;}public boolean isSingleValue() {return singleValue;}public boolean isBetweenValue() {return betweenValue;}public boolean isListValue() {return listValue;}public String getTypeHandler() {return typeHandler;}protected Criterion(String condition) {super();this.condition = condition;this.typeHandler = null;this.noValue = true;}protected Criterion(String condition, Object value, String typeHandler) {super();this.condition = condition;this.value = value;this.typeHandler = typeHandler;if (value instanceof List<?>) {this.listValue = true;} else {this.singleValue = true;}}protected Criterion(String condition, Object value) {this(condition, value, null);}protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {super();this.condition = condition;this.value = value;this.secondValue = secondValue;this.typeHandler = typeHandler;this.betweenValue = true;}protected Criterion(String condition, Object value, Object secondValue) {this(condition, value, secondValue, null);}}
}

ItemService.java


package com.ssm.service;import com.ssm.po.Item;import java.util.List;public interface ItemService {List<Item> queryItemList();
}

ItemServiceImpl.java


package com.ssm.service;import com.ssm.mapper.ItemMapper;
import com.ssm.po.Item;
import com.ssm.po.ItemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class ItemServiceImpl implements ItemService {@Autowiredprivate ItemMapper mapper;public List<Item> queryItemList() {ItemExample example = new ItemExample();return mapper.selectByExample(example);}
}

ItemMapper.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.ssm.mapper.ItemMapper" ><resultMap id="BaseResultMap" type="com.ssm.po.Item" ><id column="id" property="id" jdbcType="INTEGER" /><result column="name" property="name" jdbcType="VARCHAR" /><result column="price" property="price" jdbcType="REAL" /><result column="pic" property="pic" jdbcType="VARCHAR" /><result column="createtime" property="createtime" jdbcType="TIMESTAMP" /></resultMap><sql id="Example_Where_Clause" ><where ><foreach collection="oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Update_By_Example_Where_Clause" ><where ><foreach collection="example.oredCriteria" item="criteria" separator="or" ><if test="criteria.valid" ><trim prefix="(" suffix=")" prefixOverrides="and" ><foreach collection="criteria.criteria" item="criterion" ><choose ><when test="criterion.noValue" >and ${criterion.condition}</when><when test="criterion.singleValue" >and ${criterion.condition} #{criterion.value}</when><when test="criterion.betweenValue" >and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}</when><when test="criterion.listValue" >and ${criterion.condition}<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >#{listItem}</foreach></when></choose></foreach></trim></if></foreach></where></sql><sql id="Base_Column_List" >id, name, price, pic, createtime</sql><select id="selectByExample" resultMap="BaseResultMap" parameterType="com.ssm.po.ItemExample" >select<if test="distinct" >distinct</if><include refid="Base_Column_List" />from item<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if><if test="orderByClause != null" >order by ${orderByClause}</if></select><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select<include refid="Base_Column_List" />from itemwhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from itemwhere id = #{id,jdbcType=INTEGER}</delete><delete id="deleteByExample" parameterType="com.ssm.po.ItemExample" >delete from item<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></delete><insert id="insert" parameterType="com.ssm.po.Item" >insert into item (id, name, price,pic, createtime)values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL},#{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP})</insert><insert id="insertSelective" parameterType="com.ssm.po.Item" >insert into item<trim prefix="(" suffix=")" suffixOverrides="," ><if test="id != null" >id,</if><if test="name != null" >name,</if><if test="price != null" >price,</if><if test="pic != null" >pic,</if><if test="createtime != null" >createtime,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="id != null" >#{id,jdbcType=INTEGER},</if><if test="name != null" >#{name,jdbcType=VARCHAR},</if><if test="price != null" >#{price,jdbcType=REAL},</if><if test="pic != null" >#{pic,jdbcType=VARCHAR},</if><if test="createtime != null" >#{createtime,jdbcType=TIMESTAMP},</if></trim></insert><select id="countByExample" parameterType="com.ssm.po.ItemExample" resultType="java.lang.Integer" >select count(*) from item<if test="_parameter != null" ><include refid="Example_Where_Clause" /></if></select><update id="updateByExampleSelective" parameterType="map" >update item<set ><if test="record.id != null" >id = #{record.id,jdbcType=INTEGER},</if><if test="record.name != null" >name = #{record.name,jdbcType=VARCHAR},</if><if test="record.price != null" >price = #{record.price,jdbcType=REAL},</if><if test="record.pic != null" >pic = #{record.pic,jdbcType=VARCHAR},</if><if test="record.createtime != null" >createtime = #{record.createtime,jdbcType=TIMESTAMP},</if></set><if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByExample" parameterType="map" >update itemset id = #{record.id,jdbcType=INTEGER},name = #{record.name,jdbcType=VARCHAR},price = #{record.price,jdbcType=REAL},pic = #{record.pic,jdbcType=VARCHAR},createtime = #{record.createtime,jdbcType=TIMESTAMP}<if test="_parameter != null" ><include refid="Update_By_Example_Where_Clause" /></if></update><update id="updateByPrimaryKeySelective" parameterType="com.ssm.po.Item" >update item<set ><if test="name != null" >name = #{name,jdbcType=VARCHAR},</if><if test="price != null" >price = #{price,jdbcType=REAL},</if><if test="pic != null" >pic = #{pic,jdbcType=VARCHAR},</if><if test="createtime != null" >createtime = #{createtime,jdbcType=TIMESTAMP},</if></set>where id = #{id,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.ssm.po.Item" >update itemset name = #{name,jdbcType=VARCHAR},price = #{price,jdbcType=REAL},pic = #{pic,jdbcType=VARCHAR},createtime = #{createtime,jdbcType=TIMESTAMP}where id = #{id,jdbcType=INTEGER}</update>
</mapper>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/36772.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux 生产消费者模型

&#x1f493;博主CSDN主页:麻辣韭菜&#x1f493;   ⏩专栏分类&#xff1a;Linux初窥门径⏪   &#x1f69a;代码仓库:Linux代码练习&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学习更多Linux知识   &#x1f51d; 前言 1. 生产消费者模型 1.1 什么是生产消…

二分查找:自定义 upper_bound、lower_bound

二分查找详细介绍可以看这篇文章&#xff0c;此篇文章介绍返回索引的 upper_bound 和 lower_bound 的 C 实现。 lower_bound 实现代码 #include <vector>int lower_bound_index(const std::vector<int>& vec, const int& target) {int left 0;int right…

AI复活亲人市场分析:技术、成本与伦理挑战

“起死回生”这种事&#xff0c;过去只存在于科幻电影里&#xff0c;但今年&#xff0c;被“复活”的案例却越来越多。 2月底&#xff0c;知名音乐人包晓柏利用AI“复活”了她的女儿&#xff0c;让她在妈妈生日时唱了一首生日歌&#xff1b;3月初&#xff0c;商汤科技的年会上…

grpc编译

1、cmake下载 Download CMakehttps://cmake.org/download/cmake老版本下载 Index of /fileshttps://cmake.org/files/2、gprc源码下载&#xff0c;发现CMAKE报错 3、使用git下载 1&#xff09;通过git打开一个目录&#xff1a;如下grpc将放在D盘src目录下 cd d: cd src2&am…

深入解析内容趋势:使用YouTube API获取视频数据信息

一、引言 YouTube&#xff0c;作为全球最大的视频分享平台之一&#xff0c;汇聚了无数优质的内容创作者和观众。从个人分享到专业制作&#xff0c;从教育科普到娱乐休闲&#xff0c;YouTube上的视频内容丰富多彩&#xff0c;满足了不同用户的需求。对于内容创作者、品牌以及希…

查看linux中libc库的位置

因为在linux中libc库是非常基础的库&#xff0c;因此随便编译一个简单的hello world程序&#xff0c;一般也会链接上这个库。可以通过ldd命令来查看编译成功的可执行程序&#xff0c;来查看链接哪些库&#xff0c;以及链接库的位置。 $ ldd hello | grep libc显示结果 libc.so…

第八节:学习@Bean和@ComponentScan以及@Autowired的区别(自学Spring boot 3.x的第二天)

大家好&#xff0c;我是网创有方&#xff0c;上篇学习了依赖注入。加上上节学习的Autowired和之前的Bean以及ComponentScan&#xff0c;目前已经有三种方式。那么该如何选择用哪一种方式呢&#xff1f;咱们这节来学习它们的区别在哪里&#xff1f; 第七节&#xff1a;如何浅显…

欧姆龙NJ/NX使用科伺伺服的PDO一般配置

选择单击左侧“配置和设置”&#xff0c;双击“EtherCAT”&#xff0c;选择从设备&#xff0c;单击“编辑PDO映射设置” 配置伺服所需PDO 选择单击左侧“配置和设置”下的“运动控制设置”&#xff0c;然后右键“轴设置”&#xff0c;添加“运动控制轴/单轴位置控制轴”&#x…

LeetCode:经典题之144、94、145、102题解及延伸|二叉树的遍历|前中后层序遍历|Morris算法

系列目录 88.合并两个有序数组 52.螺旋数组 567.字符串的排列 643.子数组最大平均数 150.逆波兰表达式 61.旋转链表 160.相交链表 83.删除排序链表中的重复元素 389.找不同 1491.去掉最低工资和最高工资后的工资平均值 896.单调序列 206.反转链表 92.反转链表II 141.环形链表 …

Linux 常用命令之 RZ和SZ 简介

一、引言 在Linux系统管理中&#xff0c;尤其是在远程操作时&#xff0c;文件的上传与下载是常见的需求。对于CentOS用户而言&#xff0c;rz和sz这两个命令提供了简单而高效的文件传输方式&#xff0c;尤其在SSH终端环境中更为便利。本文将详细介绍rz和sz命令的基本概念、如何…

【鸿蒙学习笔记】@Styles装饰器:定义组件重用样式

原文链接&#xff1a;Styles装饰器&#xff1a;定义组件重用样式 目录标题 [Q&A] Styles装饰器作用 [Q&A] Styles装饰器特点使用场景 [Q&A] Styles装饰器作用 Styles 表示公共样式&#xff0c;每个组件都可以复用。如果相同样式在各个组件里复制一遍&#xff0c;…

高效管理:在Postman中处理大型响应数据的策略与技巧

Postman是一款功能强大的API开发和测试工具&#xff0c;但在处理大型响应数据时&#xff0c;用户可能会遇到性能瓶颈或数据管理上的挑战。本文将详细探讨在Postman中处理大型响应数据的策略和技巧&#xff0c;包括数据查看、分析、预处理和性能优化等。 一、大型响应数据的挑战…

基于SpringBoot漫画网站系统设计和实现(源码+LW+调试文档+讲解等)

&#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN作者、博客专家、全栈领域优质创作者&#xff0c;博客之星、平台优质作者、专注于Java、小程序技术领域和毕业项目实战✌&#x1f497; &#x1f31f;文末获取源码数据库&#x1f31f; 感兴趣的可以先收藏起来&#xff0c;…

Nettyの粘包、半包问题框架解决方案自定义协议

1、Netty框架是如何解决粘包、半包问题 关于粘包&#xff0c;半包问题&#xff0c;在前面几篇中都有提及&#xff0c;我们简单的复习一下。 粘包指的是客户端发出的多条消息&#xff0c;被服务端当做一条进行接收。半包指的是客户端发出一条完整的消息&#xff0c;在传输的过程…

数据安全和隐私保护在大数据领域的重要性

如何理解数据安全和隐私保护在大数据领域的重要性&#xff1f; 在大数据领域&#xff0c;数据安全和隐私保护至关重要。随着技术的发展&#xff0c;海量的数据被收集、存储和分析&#xff0c;这些数据包含了用户的个人信息和敏感信息。其重要性体现在以下几个方面&#xff1a;…

Java IO: 使用 `BufferedReader` 和 `BufferedWriter` 进行高效文件操作

在 Java 中&#xff0c;文件 I/O 操作是常见的任务。BufferedReader 和 BufferedWriter 是用于读取和写入文件的高效类&#xff0c;它们通过缓冲机制显著提高了 I/O 操作的性能。本文将介绍如何使用 BufferedReader 和 BufferedWriter&#xff0c;并提供具体的示例代码。 Buff…

Redisson框架

1. Redisson锁与Redis订阅与发布模式的联系&#xff1a; Redisson锁中&#xff0c;使用订阅发布模式去通知等待锁的客户端&#xff1a;锁已经释放&#xff0c;可以进行抢锁。 publish channel_name message&#xff1a;将消息发送到指定频道 解锁时&#xff0c;在Lua解锁脚本…

如何把项目文文件/文件夹)上传到Gitee(全网最细)

目录 1、首先必须要有一个Gitee官网的账号 2、点击右上角的号&#xff0c;点击新建仓库 3、按照下图步骤&#xff0c;自己起仓库名字&#xff0c;开发语言 4、点击初始化readme文件 5、在自己的电脑上选择姚上传的文件夹&#xff0c;或者文件&#xff0c;这里都是一样的&a…

内网渗透:端口转发(SSH隧道)

SSH&#xff1a;两台设备之间进行远程登录的协议&#xff08;SSH本身就是一个隧道协议&#xff09; 远程文件传输scp命令&#xff08;scp是基于SSH的&#xff09; 拓扑&#xff1a; SSH隧道搭建的条件 1.获取到跳板机权限 2.跳板机中SSH服务启动 SSH端口转发分类&#xff1…

C语言 实现链表的各种功能

C语言实现链表的各种功能 链表的定义 链表是一种数据结构&#xff0c;它是由一系列节点组成的线性结构。每个节点包含两个部分&#xff1a;数据和指针。数据部分存储着实际的数据&#xff0c;指针部分指向下一个节点。 链表的特点是&#xff1a; 每个节点都可以自由地插入或…