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方式)进行管理。
- 整合持久层 mapper ,包括 数据源 、 SqlSessionFactory 及 mapper 代理对象的整合;
- 整合业务层 Service ,包括 事务Bean 及 service 的 bean 的配置;
- 整合表现层 Controller ,直接使用 springmvc 的配置。
- 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}" />-->
<!-- <!– 注意:HikariCP 使用 jdbcUrl 而不是 url –>-->
<!-- <property name="jdbcUrl" value="${jdbc.url}" />-->
<!-- <property name="username" value="${jdbc.username}" />-->
<!-- <property name="password" value="${jdbc.password}" />-->
<!-- <!– HikariCP 的连接池大小配置 –>-->
<!-- <property name="maximumPoolSize" value="30" />-->
<!-- <!– HikariCP 没有直接的 maxIdle 属性,但可以通过 minimumIdle 控制空闲连接数 –>-->
<!-- <property name="minimumIdle" value="5" />-->
<!-- <!– 其他可选配置,如连接超时、空闲连接超时等 –>-->
<!-- <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 打开环境,首先的提示信息就是告诉我们,可以用post传参的方式来传入参数data 首先考虑的还是rce,但是这里发现,不管输入那种命令,它都会直接显示在中间的那一小行里面,而实际的命令…...
【单片机毕业设计选题24037】-基于STM32的电力系统电力参数无线监控系统
系统功能: 系统上电后,OLED显示“欢迎使用电力监控系统请稍后”,两秒后显示“Waiting..”等待ESP8266初始化完成, ESP8266初始化成功后进入正常页面显示, 第一行显示电压值(单位V) 第二行显示电流值&am…...
Python使用彩虹表来尝试对MD5哈希进行破解
MD5是一种散列算法,它是不可逆的,无法直接解密。它的主要作用是将输入数据进行散列,生成一个固定长度的唯一哈希值。 然而,可以使用预先计算好的MD5哈希值的彩虹表(Rainbow Table)来尝试对MD5进行破解。彩…...
数据恢复篇: 如何在数据丢失后恢复照片
数据丢失的情况并不少见。如果您曾经遇到过图像丢失的情况,您可能想过照片恢复工具是如何工作的?可能会丢失多少数据图像?即使是断电也可能导致照片和媒体文件丢失。 话虽如此,如果你认为删除的照片无法恢复,那你就错…...
c++ 引用第三方库
文章目录 背景编写cmake代码里引用测试 背景 遇到一个c项目,想跑一些示例。了解下如何直接引用第三方库。 编写cmake 项目结构 myprojectincludexx.hmain.cppCMakeLists.txt CMakeLists.txt cmake_minimum_required(VERSION 3.28) project(velox_demo)set(CM…...
[数据集][目标检测]猪只状态吃喝睡站检测数据集VOC+YOLO格式530张4类别
数据集格式:Pascal VOC格式YOLO格式(不包含分割路径的txt文件,仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数):530 标注数量(xml文件个数):530 标注数量(txt文件个数):530 标注类别…...
Redis中设置验证码
限制一分钟内最多发送5次,且每次有效时间是5分钟! String 发送验证码(phoneNumber) {key "shortMsg:limit:" phoneNumber;// 设置过期时间为 1 分钟(60 秒)// 使⽤ NX,只在不存在 key 时才能设置成功bool…...
使用hadoop进行数据分析
Hadoop是一个开源框架,它允许分布式处理大数据集群上的大量数据。Hadoop由两个主要部分组成:HDFS(Hadoop分布式文件系统)和MapReduce。以下是使用Hadoop进行数据分析的基本步骤: 数据准备: 将数据存储在HDF…...
架构师篇-7、企业安全架构设计及实践
摘要: 认识企业安全架构企业安全案例分析及实践 内容: 为什么做企业安全架构怎么做好安全架构设计案例实践分析&随堂练 为什么要做企业安全架构 安全是麻烦制造者? 整天提安全需求增加开发工作增加运维要求增加不确定性延后业务上线…...
递归算法~快速排序、归并排序
递归排序是一种基于分治法的排序算法,最典型的例子就是快速排序和归并排序。这两种算法都利用递归将问题分解成更小的子问题,然后将子问题的解合并以得到原始问题的解。 1、快速排序(Quick Sort) 快速排序的基本思想是选择一个基…...
DarkGPT:基于GPT-4-200k设计的人工智能OSINT助手
关于DarkGPT DarkGPT是一款功能强大的人工智能安全助手,该工具基于GPT-4-200k设计并实现其功能,可以帮助广大研究人员针对泄露数据库进行安全分析和数据查询相关的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 有一堆石头,用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。 每一回合,从中选出任意两块石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 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是一种强大而灵活的编程语言,具有广泛的应用范围,从桌面应用程序到企业级应用程序都能够使用Java进行开发。在Java的编程过程中,使用标准类库是非常重要的,因为标准类库提供了丰富的类和API,可以简化开发过…...
【股指期权投教】一手股指期权大概多少钱?
一手股指期权的权利金大概在几千人民币左右,如果是作为期权卖方还需要另外缴纳保证金的。国内的股指期权有三种,沪深300、上证50、中证1000股指期权,每点合约人民币100 元。 期权合约的价值计算可以通过此公式得出:权利金的支付或…...
mmap()函数和munmap()函数的例子
代码: #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)
计算神经网络中梯度的核心机制 - 反向传播(backpropagation)算法(1) flyfish 链式法则在深度学习中的主要应用是在反向传播(backpropagation)算法中。 从简单的开始 ,文本说的就是链式法则 R …...
VUE实现简易购物车
主要是对基础的指令的使用,直接上代码: <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0">&l…...
AtCoder 第409场初级竞赛 A~E题解
A Conflict 【题目链接】 原题链接:A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串,只有在同时为 o 时输出 Yes 并结束程序,否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...
【算法训练营Day07】字符串part1
文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接:344. 反转字符串 双指针法,两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一)
宇树机器人多姿态起立控制强化学习框架论文解析 论文解读:交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一) 论文解读:交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化…...
华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建
华为云FlexusDeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色,华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型,能助力我们轻松驾驭 DeepSeek-V3/R1,本文中将分享如何…...
【开发技术】.Net使用FFmpeg视频特定帧上绘制内容
目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法,当前调用一个医疗行业的AI识别算法后返回…...
关键领域软件测试的突围之路:如何破解安全与效率的平衡难题
在数字化浪潮席卷全球的今天,软件系统已成为国家关键领域的核心战斗力。不同于普通商业软件,这些承载着国家安全使命的软件系统面临着前所未有的质量挑战——如何在确保绝对安全的前提下,实现高效测试与快速迭代?这一命题正考验着…...
【Java学习笔记】BigInteger 和 BigDecimal 类
BigInteger 和 BigDecimal 类 二者共有的常见方法 方法功能add加subtract减multiply乘divide除 注意点:传参类型必须是类对象 一、BigInteger 1. 作用:适合保存比较大的整型数 2. 使用说明 创建BigInteger对象 传入字符串 3. 代码示例 import j…...
Mysql中select查询语句的执行过程
目录 1、介绍 1.1、组件介绍 1.2、Sql执行顺序 2、执行流程 2.1. 连接与认证 2.2. 查询缓存 2.3. 语法解析(Parser) 2.4、执行sql 1. 预处理(Preprocessor) 2. 查询优化器(Optimizer) 3. 执行器…...
Chromium 136 编译指南 Windows篇:depot_tools 配置与源码获取(二)
引言 工欲善其事,必先利其器。在完成了 Visual Studio 2022 和 Windows SDK 的安装后,我们即将接触到 Chromium 开发生态中最核心的工具——depot_tools。这个由 Google 精心打造的工具集,就像是连接开发者与 Chromium 庞大代码库的智能桥梁…...
【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信 BLE Mesh协议的拓扑结构 定向转发机制
目录 节点的功能承载层(GATT/Adv)局限性: 拓扑关系定向转发机制定向转发意义 CG 节点的功能 节点的功能由节点支持的特性和功能决定。所有节点都能够发送和接收网格消息。节点还可以选择支持一个或多个附加功能,如 Configuration …...
