SSM整合
1.创建web工程
2.导入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- 配置全局spring版本 -->
<spring.version>5.2.6.RELEASE</spring.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<!-- 配置版本-->
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.19</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!-- springmvc -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
3.使用逆向工程生成pojo,mapper以及mapper.xml文件
( 注意:生成的代码拷贝至工程的指定位置,并在Mapper上加@Repository注解 )
4. 编写 Service
package com.qf.service;
import com.qf.pojo.TbMusic;
import java.util.List;
public interface MusicService {
List<TbMusic> findAll();
}
5.编写ServiceImpl
package com.qf.service.impl;
import com.qf.dao.TbMusicMapper;
import com.qf.pojo.TbMusic;
import com.qf.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MusicServiceImpl implements MusicService {
@Autowired
private TbMusicMapper tbMusicMapper;
@Override
public List<TbMusic> findAll() {
return tbMusicMapper.selectByExample(null);
}
}
6. 编写Controller
package com.qf.controller;
import com.qf.pojo.TbMusic;
import com.qf.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("music")
public class MusicController {
@Autowired
private MusicService musicService;
@RequestMapping("findAll")
public List<TbMusic> findAll(){
return musicService.findAll();
}
}
7.编写 jdbc.properties
jdbc.username = root
jdbc.password= root
jdbc.url = jdbc:mysql:///wyy_music?serverTimezone=Asia/Shanghai
jdbc.driverClassName = com.mysql.cj.jdbc.Driver
8.编写 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>
</configuration>
9.编写 applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!-- 扫描指定包下的注解 -->
<context:component-scan base-package="com.qf">
<!-- 不扫描指定的注解 -->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />-->
</context:component-scan>
<!-- 引入外包properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
</bean>
<!-- 配置sqlSessionFactoryBean -->
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 引入mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 扫描mapper.xml配置文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.dao"></property>
</bean>
<!-- 配置事务平台管理器 -->
<bean name="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="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.qf.service.impl.*ServiceImpl.*(..))"></aop:advisor>
</aop:config>
</beans>
10. 编写 springmvc.xml
<?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: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/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">
<!-- 扫描指定包下的注解 -->
<context:component-scan base-package="com.qf.controller"/>
<!-- 设置静态资源可以访问 -->
<mvc:default-servlet-handler/>
<!-- 注解配置 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀和后缀 -->
<!-- <property name="prefix" value=""></property>-->
<!-- <property name="suffix" value=""></property>-->
</bean>
</beans>
11.编写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" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 通过监听的方式去加载applicationContext.xml-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端过滤器 -->
<servlet>
<servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
12.测试
添加分页操作
1.导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
2.配置分页插件(两种方式)
2.1 第一种:在 applicationContext.xml 中添加配置
<!-- 配置sqlSessionFactoryBean -->
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 引入mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="plugins">
<set>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!-- 数据库方言,可选择:oracle,mysql,mariadb 等-->
<prop key="helperDialect">mysql</prop>
<!--reasonable:分页合理化参数,默认值:false。
当该参数设置为true时,pageNum<=0时会查询第一页,
pageNum>pages(超过总数时),会查询最后一页-->
<prop key="reasonable">true</prop>
<!--supportMethodsArguments:
是否支持通过 Mapper 接口参数来传递分页参数,默认值:false-->
<prop key="supportMethodsArguments">true</prop>
</props>
</property>
</bean>
</set>
</property>
</bean>
2.2 第二种:在 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>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
<property name="reasonable" value="true"/>
<property name="supportMtehodsArguments" value="true"/>
</plugin>
</plugins>
</configuration>
3.在 controller 中添加方法
@RequestMapping("findPage")
public PageInfo findPage(
@RequestParam(required = false,defaultValue = "1") Integer pageNum,
@RequestParam(required = false,defaultValue = "2") Integer pageSize){
PageHelper.startPage(pageNum,pageSize);
List<TbMusic> list = musicService.findAll();
PageInfo pageInfo = new PageInfo(list);
return pageInfo;
}