pom.xml文件(ssm相关依赖)
<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>springmvc_006_ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.12</junit.version>
<spring.version>5.2.5.RELEASE</spring.version>
<mybatis.version>3.5.1</mybatis.version>
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<!-- mybatis支持的分页插件的依赖-->
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<!-- mysql的依赖-->
<mysql.version>5.1.32</mysql.version>
<!-- slf4j日志依赖-->
<slf4j.version>1.6.4</slf4j.version>
<!-- druid连接池-->
<druid.version>1.1.12</druid.version>
<!-- 分页插件的依赖-->
<pagehelper.version>5.1.2</pagehelper.version>
<!-- jstl的依赖(jsp的标准标签库)-->
<jstl.version>1.2</jstl.version>
<!-- servlet依赖-->
<servlet-api.version>3.0.1</servlet-api.version>
<!-- jsp的依赖-->
<jsp-api.version>2.0</jsp-api.version>
<!-- jackson的依赖,springmvc框架默认进行json转换的依赖工具-->
<jackson.version>2.9.6</jackson.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>com.bjpowernode.test</scope>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
<version>${jsp-api.version}</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<!-- 文件异步上传使用的依赖-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<!-- 插件配置 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<!--识别所有的配置文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
ssm项目的大概目录结构
mybatis
主要工作区域在mapper文件夹(接口区域)
这里只需要书写<setting>相关的配置,其他的都写在spring的配置文件中,需要引入数据源与数据库连接(在spring配置文件中)
SqlMapConfig.xml(mybatis相关配置文件)
<?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="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
mybatis概述
mybatis的核心是SqlSessionFactoryBean,它可以获得SqlSessionFactory,从而获得SqlSession,SqlSession就可以调用在内存中的动态代理对象的方法,实现对持久层的开发。
我们只需要在mapper中写好需要的方法,mybais会自动在内存中通过动态代理的方式制造一个对象,也就是mybatis里的Mapper mapper = sqlSession.getMapper(接口名.class),这里的mapper对象就是动态代理生成的对象,通过这个mapper可直接调用接口中的方法。
对于每个mapper接口,都需要一个对应的mapper.xml文件,这两个需要放在同一个文件夹下,当然还有另一种书写方式放在resources文件夹下。
在ssm整合项目中,直接使用@Autowired自动注入到service中,不需要手动创建对象,这里属于spring的内容
mybatis中文官网:MyBatis中文网
mapper.xml相关配置
这个需要在spring的配置文件中注册
<?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.powernode.mapper.UserMapper">
<resultMap id="usermap" type="user">
<id property="userId" column="user_id"/>
<result property="cardType" column="card_type"/>
<result property="cardNo" column="card_no"/>
<result property="userName" column="user_name"/>
<result property="userSex" column="user_sex"/>
<result property="userAge" column="user_age"/>
<result property="userRole" column="user_role"/>
</resultMap>
<sql id="allColumns">
user_id,card_type,card_no,user_name,user_sex,user_age,user_role
</sql>
<insert id="insert" parameterType="user">
insert into user values (#{user.userId},#{user.cardType},#{user.cardNo},#{user.userName},#{user.userSex},#{user.userAge},#{user.userRole})
</insert>
<delete id="delete" parameterType="string">
delete from user
where user_id = #{userId}
</delete>
<select id="selectUserPage" resultMap="usermap">
select <include refid="allColumns"></include>
from user
<where>
<if test="userName != null and userName != ''">
and user_name like concat('%',#{userName},'%')
</if>
<if test="userSex != null and userSex != ''">
and user_sex = #{userSex}
</if>
</where>
limit #{startRow},5
</select>
<select id="getRowCount" resultType="int">
select count(*)
from user
<where>
<if test="userName!=null and userName!=''">
and user_name like concat('%',#{userName},'%')
</if>
<if test="userSex!=null and userSex!=''">
and user_name = #{userSex}
</if>
</where>
</select>
</mapper>
动态sql标签有:
<if test=""></if>
<choose>
<when test=""></when>
<otherwise></otherwise>
</choose>
<where></where>
<trim></trim>
<foreach></foreach>
<set></set>
<sql></sql> 配合 <include></include>使用
spring
spring活跃在整个项目中,主要使用区域在service(业务包)
核心是控制反转(ioc)和面向切面编程(aop)
注意:每个包下的核心类都需要加入@Service、@Resource、@Controller、@Component其中一个注解交给spring框架管理,这四个注解含义相同,用不同注解表示不同包下的类结构清晰
mapper接口不用配,因为那是由mybatis动态创建的对象,自动交给spring管理
控制反转(ioc)
通过xml文件和注解,由第三方去生产或获取特定对象的方式。在spring中实现控制反转的是ios容器,实现方式是依赖注入。
applicationContext_mapper.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--读取属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
<!--配置SqlSessionFactoryBean,mybatis相关配置-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 配置SqlMapperConfig-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<!-- 注册实体类-->
<property name="typeAliasesPackage" value="com.powernode.pojo"/>
</bean>
<!--注册mapper.xml-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.powernode.mapper"/>
</bean>
</beans>
applicationContext_service.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置业务逻辑层,给spring扫描识别该包下的所有类,使用注解方式-->
<context:component-scan base-package="com.powernode.service.impl"></context:component-scan>
<!-- 添加事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--配置数据源,事务需要和数据库打交道-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 添加事务切面-->
<tx:advice id="myadvice" transaction-manager="transactionManager">
</tx:advice>
<!-- 配置切入点+绑定-->
<aop:config>
<aop:pointcut id="mycut" expression="execution(* com.powernode.service.impl.*.*(..))"/>
<aop:advisor advice-ref="myadvice" pointcut-ref="mycut"/>
</aop:config>
</beans>
两个spring配置文件都需要对相关文件夹(包)进行包扫描,注解是
<context:component-scan base-package=""></context:component-scan> //可用com简写
读取外部文件:例如properties文件
<comtext:property-placeholder location=""></comtext:property-placeholder>
配置数据源:druid连接池,通过properties文件传参数,用于连接数据库
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
</bean>
注册mybatis核心对象SqlSessionFactoryBean:需要引入数据源,引入mybatis配置文件SqlMapConfig.xml
也就是在这里将mybatis和spring连接在一起
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 配置SqlMapperConfig-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<!-- 起别名-->
<property name="typeAliasesPackage" value="com.powernode.pojo"/>
</bean>
注册mapper.xml文件:在这里引入mapper.xml文件(在单独的mybatis中写在mybatis-config.xml核心配置文件中)
采用包扫描的方式(MapperScannerConfigurer)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.powernode.mapper"/>
</bean>
注入方式
@Autowired(根据类型注入),可配合@Qualifier(根据名字注入)使用
可使用@Resources注入,需要引入相关依赖
面向切面编程(aop)
注册事务管理器:需要引入数据源(告诉事务管理器作用哪个数据库)
DataSourceTransactionManager
需要添加事务管理的注解@Transactional
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--配置数据源,事务需要和数据库打交道-->
<property name="dataSource" ref="dataSource"></property>
</bean>
使用注解实现aop
1.编写一个注解实现的增强类,需要用@Aspect标记这个增强类,声明是切面,切面=通知+切点,记得添加@Component
2.使用注解标记增强类中的方法
如:括号中的表达式指的是要切入的方法
@After(execution(* com.powernode.service.impl.*.*(..)))
public void after(){
System.out.println("方法执行后")
}
@Before、@Around
3.在spring配置文件中,注册增强类bean,并添加支持注解的配置
<bean id="" class="增强类的全限定类名"/>
<aop:aspectj-autoproxy/>//使用注解声明自动为spring容器中哪些配置@aspectJ切面的bean创建代理.织入切面
springmvc
springmvc作用于controller包下
需要在web.xml中注册springmvc框架,引入DispatcherServlet,添加springmvc.xml
可以添加中文过滤CharacterEncodingFilter
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/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">
<!--扫描根包 -->
<context:component-scan base-package="com.powernode.controller"></context:component-scan>
<!--视图解析器,本项目全为ajax请求,不需要配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<!--文件上传插件-->
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
记得添加<mvc:annotation-driven/>注解驱动,这样就可以使用注解配置springmvc
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_4_0.xsd"
version="4.0">
<filter>
<filter-name>encode</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 注册SpringMVC框架-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 注册Spring框架,目的是启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext_*.xml</param-value>
</context-param>
</web-app>
springmvc常用注解
@Controller:注册bean到spring中
@RequestMapping:为处理器指定可以处理哪些url请求
@RequestBody:主要作用就是用于接收前端的参数,当我们使用post请求的时候,我们会将参数放在request body中,此时我们就需要在Controller的方法的参数前面加上@RequestBody用来接受到前端传过来的request body中的值,一般用于处理非Content-Type:application/x-www-form-urlencoded编码格式的数据,比如application/json、application/xml等类型的数据;放在参数前面,注意与requestParam的区别
@ResponseBody:将方法返回的对象通过适当的HttpMessageConverter转换为指定格式(json)写入到response对象的body区
@ModelAttribute:执行其他方法时,先执行该注解标注的方法。 如果有将属性放入model的操作,model中的数据也会共享 方法的返回值会自动装入model中,key值如果没有指定的话为返回对象类型的首字母小写。指定key的话给注解的value属性赋值即可。方法返回的值可能会覆盖同key的值。 model中的属性的属性名为方法中model.addAttribute的key
@RequestParam:取请求参数
@PathVariable:取路径上的参数
@ExceptionHandler:注解到方法上,出现异常时执行该方法
@ControllerAdvice:作用是给Controller控制器添加统一的操作或处理