当前位置: 首页 > news >正文

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>

相关文章:

003 SSM框架整合

文章目录 整合web.xmlapplicationContext-dao.xmlapplicationContext-service.xmlspringmvc.xmldb.propertieslog4j.propertiespom.xml 测试sqlItemController.javaItemMapper.javaItem.javaItemExample.javaItemService.javaItemServiceImpl.javaItemMapper.xml 整合 将工程的…...

web刷题记录(7)

[HDCTF 2023]SearchMaster 打开环境&#xff0c;首先的提示信息就是告诉我们&#xff0c;可以用post传参的方式来传入参数data 首先考虑的还是rce&#xff0c;但是这里发现&#xff0c;不管输入那种命令&#xff0c;它都会直接显示在中间的那一小行里面&#xff0c;而实际的命令…...

【单片机毕业设计选题24037】-基于STM32的电力系统电力参数无线监控系统

系统功能: 系统上电后&#xff0c;OLED显示“欢迎使用电力监控系统请稍后”&#xff0c;两秒后显示“Waiting..”等待ESP8266初始化完成&#xff0c; ESP8266初始化成功后进入正常页面显示&#xff0c; 第一行显示电压值&#xff08;单位V&#xff09; 第二行显示电流值&am…...

Python使用彩虹表来尝试对MD5哈希进行破解

MD5是一种散列算法&#xff0c;它是不可逆的&#xff0c;无法直接解密。它的主要作用是将输入数据进行散列&#xff0c;生成一个固定长度的唯一哈希值。 然而&#xff0c;可以使用预先计算好的MD5哈希值的彩虹表&#xff08;Rainbow Table&#xff09;来尝试对MD5进行破解。彩…...

数据恢复篇: 如何在数据丢失后恢复照片

数据丢失的情况并不少见。如果您曾经遇到过图像丢失的情况&#xff0c;您可能想过照片恢复工具是如何工作的&#xff1f;可能会丢失多少数据图像&#xff1f;即使是断电也可能导致照片和媒体文件丢失。 话虽如此&#xff0c;如果你认为删除的照片无法恢复&#xff0c;那你就错…...

c++ 引用第三方库

文章目录 背景编写cmake代码里引用测试 背景 遇到一个c项目&#xff0c;想跑一些示例。了解下如何直接引用第三方库。 编写cmake 项目结构 myprojectincludexx.hmain.cppCMakeLists.txt CMakeLists.txt cmake_minimum_required(VERSION 3.28) project(velox_demo)set(CM…...

[数据集][目标检测]猪只状态吃喝睡站检测数据集VOC+YOLO格式530张4类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;530 标注数量(xml文件个数)&#xff1a;530 标注数量(txt文件个数)&#xff1a;530 标注类别…...

Redis中设置验证码

限制一分钟内最多发送5次&#xff0c;且每次有效时间是5分钟&#xff01; String 发送验证码(phoneNumber) {key "shortMsg:limit:" phoneNumber;// 设置过期时间为 1 分钟&#xff08;60 秒&#xff09;// 使⽤ NX&#xff0c;只在不存在 key 时才能设置成功bool…...

使用hadoop进行数据分析

Hadoop是一个开源框架&#xff0c;它允许分布式处理大数据集群上的大量数据。Hadoop由两个主要部分组成&#xff1a;HDFS&#xff08;Hadoop分布式文件系统&#xff09;和MapReduce。以下是使用Hadoop进行数据分析的基本步骤&#xff1a; 数据准备&#xff1a; 将数据存储在HDF…...

架构师篇-7、企业安全架构设计及实践

摘要&#xff1a; 认识企业安全架构企业安全案例分析及实践 内容&#xff1a; 为什么做企业安全架构怎么做好安全架构设计案例实践分析&随堂练 为什么要做企业安全架构 安全是麻烦制造者&#xff1f; 整天提安全需求增加开发工作增加运维要求增加不确定性延后业务上线…...

递归算法~快速排序、归并排序

递归排序是一种基于分治法的排序算法&#xff0c;最典型的例子就是快速排序和归并排序。这两种算法都利用递归将问题分解成更小的子问题&#xff0c;然后将子问题的解合并以得到原始问题的解。 1、快速排序&#xff08;Quick Sort&#xff09; 快速排序的基本思想是选择一个基…...

DarkGPT:基于GPT-4-200k设计的人工智能OSINT助手

关于DarkGPT DarkGPT是一款功能强大的人工智能安全助手&#xff0c;该工具基于GPT-4-200k设计并实现其功能&#xff0c;可以帮助广大研究人员针对泄露数据库进行安全分析和数据查询相关的OSINT操作。 工具要求 openai1.13.3 requests python-dotenv pydantic1.10.12 工具安装 …...

RAG 检索增强生成有效评估

我们将介绍RAG(检索增强生成)的评估工作流程 RAG工作流程的部分 数据集 这里是我们将要使用的LCEL (LangChain Expression Language)相关问题的数据集。 这个数据集是在LangSmith UI中使用csv上传创建的: https://smith.langchain.com/public/730d833b-74da-43e2-a614-4e2ca…...

Day38:LeedCode 1049. 最后一块石头的重量 II 494. 目标和 474.一和零

1049. 最后一块石头的重量 II 有一堆石头&#xff0c;用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。 每一回合&#xff0c;从中选出任意两块石头&#xff0c;然后将它们一起粉碎。假设石头的重量分别为 x 和 y&#xff0c;且 x < y。那么粉碎的可能结果…...

sqlalchemy分页查询

sqlalchemy分页查询 在SQLAlchemy中,可以使用limit和offset方法实现分页查询 from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine from models import MyModel # 假设MyModel是你定义的模型# 连接数据库 engine = create_engine(sqlite:///myd…...

Java--常用类APl(复习总结)

前言: Java是一种强大而灵活的编程语言&#xff0c;具有广泛的应用范围&#xff0c;从桌面应用程序到企业级应用程序都能够使用Java进行开发。在Java的编程过程中&#xff0c;使用标准类库是非常重要的&#xff0c;因为标准类库提供了丰富的类和API&#xff0c;可以简化开发过…...

【股指期权投教】一手股指期权大概多少钱?

一手股指期权的权利金大概在几千人民币左右&#xff0c;如果是作为期权卖方还需要另外缴纳保证金的。国内的股指期权有三种&#xff0c;沪深300、上证50、中证1000股指期权&#xff0c;每点合约人民币100 元。 期权合约的价值计算可以通过此公式得出&#xff1a;权利金的支付或…...

mmap()函数和munmap()函数的例子

代码&#xff1a; #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/mman.h> #include <string.h> #include <stdio.h> #include <unistd.h>#define FILELENGTH 80 int main(void) {int fd-1;char …...

计算神经网络中梯度的核心机制 - 反向传播(backpropagation)算法(1)

计算神经网络中梯度的核心机制 - 反向传播&#xff08;backpropagation&#xff09;算法&#xff08;1&#xff09; flyfish 链式法则在深度学习中的主要应用是在反向传播&#xff08;backpropagation&#xff09;算法中。 从简单的开始 &#xff0c;文本说的就是链式法则 R …...

VUE实现简易购物车

主要是对基础的指令的使用&#xff0c;直接上代码&#xff1a; <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0">&l…...

Go 语言接口详解

Go 语言接口详解 核心概念 接口定义 在 Go 语言中&#xff0c;接口是一种抽象类型&#xff0c;它定义了一组方法的集合&#xff1a; // 定义接口 type Shape interface {Area() float64Perimeter() float64 } 接口实现 Go 接口的实现是隐式的&#xff1a; // 矩形结构体…...

关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案

问题描述&#xff1a;iview使用table 中type: "index",分页之后 &#xff0c;索引还是从1开始&#xff0c;试过绑定后台返回数据的id, 这种方法可行&#xff0c;就是后台返回数据的每个页面id都不完全是按照从1开始的升序&#xff0c;因此百度了下&#xff0c;找到了…...

C++ 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

【Nginx】使用 Nginx+Lua 实现基于 IP 的访问频率限制

使用 NginxLua 实现基于 IP 的访问频率限制 在高并发场景下&#xff0c;限制某个 IP 的访问频率是非常重要的&#xff0c;可以有效防止恶意攻击或错误配置导致的服务宕机。以下是一个详细的实现方案&#xff0c;使用 Nginx 和 Lua 脚本结合 Redis 来实现基于 IP 的访问频率限制…...

【Android】Android 开发 ADB 常用指令

查看当前连接的设备 adb devices 连接设备 adb connect 设备IP 断开已连接的设备 adb disconnect 设备IP 安装应用 adb install 安装包的路径 卸载应用 adb uninstall 应用包名 查看已安装的应用包名 adb shell pm list packages 查看已安装的第三方应用包名 adb shell pm list…...

Golang——9、反射和文件操作

反射和文件操作 1、反射1.1、reflect.TypeOf()获取任意值的类型对象1.2、reflect.ValueOf()1.3、结构体反射 2、文件操作2.1、os.Open()打开文件2.2、方式一&#xff1a;使用Read()读取文件2.3、方式二&#xff1a;bufio读取文件2.4、方式三&#xff1a;os.ReadFile读取2.5、写…...

AI语音助手的Python实现

引言 语音助手(如小爱同学、Siri)通过语音识别、自然语言处理(NLP)和语音合成技术,为用户提供直观、高效的交互体验。随着人工智能的普及,Python开发者可以利用开源库和AI模型,快速构建自定义语音助手。本文由浅入深,详细介绍如何使用Python开发AI语音助手,涵盖基础功…...

tomcat指定使用的jdk版本

说明 有时候需要对tomcat配置指定的jdk版本号&#xff0c;此时&#xff0c;我们可以通过以下方式进行配置 设置方式 找到tomcat的bin目录中的setclasspath.bat。如果是linux系统则是setclasspath.sh set JAVA_HOMEC:\Program Files\Java\jdk8 set JRE_HOMEC:\Program Files…...

redis和redission的区别

Redis 和 Redisson 是两个密切相关但又本质不同的技术&#xff0c;它们扮演着完全不同的角色&#xff1a; Redis: 内存数据库/数据结构存储 本质&#xff1a; 它是一个开源的、高性能的、基于内存的 键值存储数据库。它也可以将数据持久化到磁盘。 核心功能&#xff1a; 提供丰…...

华为OD最新机试真题-数组组成的最小数字-OD统一考试(B卷)

题目描述 给定一个整型数组,请从该数组中选择3个元素 组成最小数字并输出 (如果数组长度小于3,则选择数组中所有元素来组成最小数字)。 输入描述 行用半角逗号分割的字符串记录的整型数组,0<数组长度<= 100,0<整数的取值范围<= 10000。 输出描述 由3个元素组成…...