前面学习了很多在SSM中要使用的东西,Mybatis,Freemarker等,今天终于到了要整合一整个可用框架的时候了
项目源码:源码下载
~~
这篇东西有点多,不过如果你看了前面的几篇文章的话,这篇文章应该很简单,没有看也没有关系,只要按照这篇文章里的做绝对可以搭出来并成功运行,前提我代码里的注释很重要,一定要看注释
~~
<packaging>warpackaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jettygroupId>
<artifactId>jetty-maven-pluginartifactId>
<version>9.4.14.v20181114version>
<configuration>
<webApp>
<defaultsDescriptor>src/main/resources/webdefault.xmldefaultsDescriptor>
webApp>
<webAppSourceDirectory>src/main/webappwebAppSourceDirectory>
<httpConnector>
<port>80port>
httpConnector>
configuration>
plugin>
plugins>
build>
此文件不导入的话,修改页面后可能会报错,此文件一般在.m2里的如下目录,直接复制到项目的resources下
修改其中的180行,值改为false,如果版本不同也可能不是180行,那就找useFileMappedBuffer
光导入此文件还不行,还需要在pom.xml中进行引用才可以,上面引入jetty插件的时候已经引用好了
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.28version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>5.1.6.RELEASEversion>
dependency>
如果有下载不下来的包可以设置阿里云仓库,也可以不设置
<repositories>
<repository>
<id>aliyunid>
<name>aliyunname>
<url>https://maven.aliyun.com/repository/publicurl>
repository>
repositories>
<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">
web-app>
<servlet>
<servlet-name>ssmservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
init-param>
<load-on-startup>0load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>ssmservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
如果上面的有关Servlet的东西都设置好后,还有红线的话,有可能是没有引入Servlet相关包引起的,pom.xml中引入相关包
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>compilescope>
dependency>
<filter>
<filter-name>characterFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<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: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">
beans>
<context:component-scan base-package="com.zhangyx">context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8value>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
<mvc:default-servlet-handler>mvc:default-servlet-handler>
<bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html;charset=utf-8">property>
<property name="suffix" value=".ftl">property>
bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl">property>
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8prop>
props>
property>
bean>
到这里Spring与SpringMVC的框架算是搭建完成了,下面继续Spring 与Mybatis框架
大概四步
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.0version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.13version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.46version>
dependency>
<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/babytun?useUnicode=true&characterEncodeing=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhangyx.ssm"/>
bean>
resources下创建对应文件与文件夹
mappers文件夹
mybatis-config.xml
<configuration>
configuration>
测试时发现,这是因为mappers文件夹是空的,下面必须有文件才行,我这里随便创建了一个README.text,运行成功
@Controller
public class HelloController {
@GetMapping("/hello")
public ModelAndView hello(){
ModelAndView mv = new ModelAndView("/hello");
return mv;
}
}
其中遇到的坑 1.driverClassName不要写错了 2.引入数据源dataSource时要用ref,不要用value
mapper接口开发要素
实体类包:com.zhangyx.ssm.entity-存放与数据库对应的实体类
接口类包:com.zhangyx.ssm.mapper-存放与SQL xml 文件对应的接口类
mybatis-config.xml设置
驼峰命名转换
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
注意:看注释,此代码里的注释与下面的SQL.xml里的注释结合来看
public interface GoodsMapper {
/*SQL.xml中的SQL ID 要与此方法名保持一致
SQL.xml中的取值key也要与这里的参数名保持一致
*/
Goods findById(Integer goodsId);
}
mappers下创建goods_mapper.xml
注意:看注释,此代码里的注释与上面的mapper接口里的注释结合来看
<?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">
<!--spring整合了Mybatis后,此处的namespace要指向我们的接口类-->
<mapper namespace="com.zhangyx.ssm.mapper.GoodsMapper">
<!--id:跟上面的namespace一样,要与接口中的方法名保持一致,指向接口中的findById方法名
参数类型也要与接口方法中的保持一致,返回数据类型一样
上层(service)调用时会直接调用到这里去请求数据库
此xml可以看作是我们之前的dao的实现类,我是这样理解的-->
<select id="findById" parameterType="Integer" resultType="com.zhangyx.ssm.entity.Goods">
select * from t_goods where goods_id = #{value }
</select>
</mapper>
@Resource
private GoodsService goodsService;
@GetMapping("/goods")
public ModelAndView goodsInfo(){
ModelAndView mv = new ModelAndView("/goods");
Goods goods = goodsService.findById(739);
mv.addObject("goods",goods);
return mv;
}
多参数多条件查询时,有两种方式
sql.xml
注意parameterType,resultType
<select id="list" parameterType="java.util.Map" resultType="com.zhangyx.ssm.entity.Goods">
select * from t_goods where category_id = #{categoryId} and current_price < #{currentPrice}
select>
mapper 接口
List<Goods> list(Map params);
controller
@GetMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView("/list");
/*利用Map传参*/
Map param = new HashMap();
param.put("categoryId",40);
param.put("currentPrice",800);
List<Goods> goodsList = goodsService.list(param);
mv.addObject("goodsList",goodsList);
return mv;
}
SQL.xml不变
mapper接口如下
/*@Param("参数名"),将参数封装到Map里,参数名作为Map里的key,后面的参数值category作为对应map的值*/
List<Goods> list(@Param("categoryId") Integer category,@Param("currentPrice") Float currentPrice);
controller类
@GetMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView("/list");
/*利用Map传参*/
// Map param = new HashMap();
// param.put("categoryId",40);
// param.put("currentPrice",800);
/*直接传*/
List<Goods> goodsList = goodsService.list(40,800f);
mv.addObject("goodsList",goodsList);
return mv;
}