一、项目结构示意图
项目结构
二、pom文件配置
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.caiweiwei</groupId> <artifactId>Jetty</artifactId> <version>1.0-SNAPSHOT</version> <!--必须改成war--> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.6.RELEASE</version> </dependency> <!-- SpringMVC支持的JSON序列化工具包Jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <!-- freemarker核心jar --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> </dependency> <!--SpringFramework整合三方组件的支持包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.1.6.RELEASE</version> </dependency> <!--SM整合依赖--> <!--Spring的JDBC封装组件--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.6.RELEASE</version> </dependency> <!--引入mybatisplus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.1.0</version> </dependency> <!--Mybatis核心组件若使用mybatis则这样配置--> <!--<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency>--> <!--Mybatis与Spring整合组件--> <!--<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency>--> <!--Druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.13</version> </dependency> <!--MySql 5.xJDBC驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--引入日志包--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.14.v20181114</version> <configuration> <webApp> <!--找到修改后的xml文件--> <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor> </webApp> <!--启动后去加载webapp网页与资源--> <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory> <httpConnector> <port>80</port> </httpConnector> </configuration> </plugin> </plugins> </build> </project>
三、application配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 启用Spring注解形式扫描对象 --> <context:component-scan base-package="com.caiweiwei"></context:component-scan> <!-- 启用Spring MVC的注解模式 --> <mvc:annotation-driven> <mvc:message-converters> <!--StringHttpMessageConverter 用于设置文本类型http响应的设置--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <!--配置支持的媒体类型(MIME)--> <property name="supportedMediaTypes"> <list> <!-- 响应输出的文本被浏览器作为html进行解释,使用字符集为UTF-8 --> <value>text/html;charset=utf-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--将静态资源排除在外--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!-- FreeMarkerViewResolver是Spring-Context-Support提供的整合类, 在IOC容器初始化时通知SpringMVC默认使用Freemarker进行数据展现 --> <bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <!-- 设置响应输出,并解决中文乱码 --> <property name="contentType" value="text/html;charset=utf-8"></property> <!-- 指定Freemarker模板文件扩展名 --> <property name="suffix" value=".ftl"/> </bean> <!--Freemarker设置类--> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <!--设置模板保存的目录--> <property name="templateLoaderPath" value="/WEB-INF/ftl"/> <!--其他模板引擎设置--> <property name="freemarkerSettings"> <props> <!--设置Freemarker脚本与数据渲染时使用的字符集--> <prop key="defaultEncoding">UTF-8</prop> </props> </property> </bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 定义数据库 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/data?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> <!--初始化连接池的时候默认创建5个连接--> <property name="initialSize" value="5"/> <!--最大连接数量--> <property name="maxActive" value="20"/> <!-- 设置空闲连接数最少为5个 --> <property name="minIdle" value="5"/> <!-- 数据库连接最大等待时间60秒 --> <property name="maxWait" value="60000"/> <!--是否在分配连接时测试连接有效性--> <property name="testOnBorrow" value="false"/> <!--是否在连接空闲状态时测试连接有效性--> <property name="testWhileIdle" value="true"/> <!--是否在归还连接时测试有效性--> <property name="testOnReturn" value="false"/> <!--测试有效性的SQL语句--> <property name="validationQuery" value="SELECT 1"/> <!--filters用于开启监控过滤器 stat 是SQL及Web监控 wall 是预防SQL注入攻击 slf4j 是日志输出--> <property name="filters" value="stat,wall,slf4j"/> <!--mergeSQL对相同SQL进行合并 slowSqlMillis定义超过500毫秒执行时间的SQL为慢查询--> <property name="connectionProperties" value="druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500"/> </bean> <!--定义sqlSessionFactory连接工厂--> <!-- MybatisSqlSessionFactoryBean对Mybatis-Spring整合插件进行了重写与扩展.提供自动生成SQL的功能 --> <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <!--关联数据源--> <property name="dataSource" ref="dataSource"/> <!--配置文件地址--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--mapper XML文件保存目录--> <property name="mapperLocations" value="classpath:mappers/*.xml"/> <!-- mapper resource="goods.xml" --> </bean> <!--Spring为Mybatis提供了一种新的开发方式“Mapper接口”, 这个配置用于扫描com.itlaoqi.ssm包下所有有效的Mapper接口类--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.caiweiwei.ssm"/> </bean> <!-- 事务管理器transactionManager提供了声明式事务的支持,在程序成功提交,运行时异常回滚 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启注解形式的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
四、mybatis-config.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--驼峰命名转换--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!--开启Mybatis-Plus分页功能--> <plugins> <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"> </plugin> </plugins> </configuration>
五、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>springmvc</servlet-name> <!-- 用于拦截请求,创建对应的Controller进行处理 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 指明在DispatcherServlet初始化时加载的配置文件 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <!-- 在启动时对Servlet进行加载,0代表被先被加载 --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>characterFilter</filter-name> <!-- CharacterEncodingFilter 将Post请求中的参数字符集设置为UTF-8--> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Druid连接池监控界面--> <!--DruidWebStateFilter过滤器用于数据收集--> <filter> <filter-name>DruidWebStateFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置Druid监控页面--> <servlet> <servlet-name>DruidServlet</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <param-name>loginUsername</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>loginPassword</param-name> <param-value>123456</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidServlet</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> </web-app>
六、具体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.caiweiwei.ssm.mapper.GoodsDetailMapper"> <!--使用mybatis-plus分页插件后,必须在引用参数时增加#{前缀.xxx}--> <select id="selectMap" parameterType="java.util.Map" resultType="java.util.LinkedHashMap"> select d.* , g.title from t_goods_detail d , t_goods g where d.goods_id = g.goods_id and d.gd_order = #{p.order} and d.goods_id = #{p.goodsId} </select> </mapper>
七、对应接口编写
package com.caiweiwei.ssm.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.caiweiwei.ssm.entity.GoodsDetail; import org.apache.ibatis.annotations.Param; import java.util.Map; //BaseMapper定义了CRUD的方法声明,在运行时会自动根据实体的注解生成对应的SQL语句 public interface GoodsDetailMapper extends BaseMapper<GoodsDetail> { /** * 自定义SQL分页查询 * @param page 第一个对象固定为分页对象 * @param param 第二个参数Map向SQL传递多参数,但要使用@Param()定义前缀 * @return 返回分页结果 */ public IPage<Map> selectMap(IPage page ,@Param("p") Map param); }
八、对应实体类
package com.caiweiwei.ssm.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; //@TableName设置实体与表的对应关系 @TableName("t_goods_detail") public class GoodsDetail { //@TableId用于设置主键属性 //IdType.AUTO是指自动利用Mysql底层的自动生成功能产生主键 //主键生成后会自动回填到gdId属性 @TableId(type = IdType.AUTO) private Integer gdId; private Integer goodsId;//goods_id //如果属性与字段名对应不上,则需要使用@TableField进行说明 @TableField("gd_pic_url") private String picUrl;//gd_pic_url // @TableField("gd_order") private Integer gdOrder; public Integer getGdId() { return gdId; } public void setGdId(Integer gdId) { this.gdId = gdId; } public Integer getGoodsId() { return goodsId; } public void setGoodsId(Integer goodsId) { this.goodsId = goodsId; } public String getPicUrl() { return picUrl; } public void setPicUrl(String picUrl) { this.picUrl = picUrl; } public Integer getGdOrder() { return gdOrder; } public void setGdOrder(Integer gdOrder) { this.gdOrder = gdOrder; } }