认识SSM整合
1.Spring来整合MyBatis和SpringMVC,并提供业务层事务管理 2.SpringMVC本身就是Spring的一部分,所以整合相对简单 3.整合MyBatis需要使用由MyBatis社区来提供的整合插件mybatis-spring,而不是由Spring来提供的整合插件,主要是因为MyBatis出现的比Spring晚 4.整合插件官网:http://mybatis.org/spring/zh/index.html 5.SSM整合的结果是: 1)MyBatis的使用更加简单 2)MyBatis中的Bean,比如SqlSessionFactory、Mapper接口的动态代理类都交给Spring的IoC容器管理 3)对业务层应用Spring的声明式事务功能 4)整合SpringMVC(主要是ContextLoaderListener的使用,其他无差别) |
Spring整合MyBatis
1.创建项目,创建包
2.添加依赖pom.xml
<dependencies> |
3.日志文件logback.xml
直接复制: <?xml version="1.0" encoding="UTF-8"?> |
4.配置数据源
1)属性文件jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver |
2)配置Druid
创建spring配置文件:spring-persist.xml <!--加载属性文件--> |
3)测试Druid
@SpringJUnitConfig(locations = "classpath:spring-persist.xml") |
5.整合MyBatis
1)MyBatis配置文件(可以全部省略)
2)配置SqlSessionFactoryBean
<!--配置SqlSessionFactory--> |
配置SqlSessionFactory,这是一个工厂Bean,返回的不是SqlSessionFactoryBean,而是该类的getObject()方法的返回值SqlSessionFactory |
3)配置Mapper接口扫描器
<!--配置Mapper接口扫描,属于myBatis-spring整合包--> |
1.配置MapperScannerConfigurer:其实整合MyBatis还有其他方法,这个算是最给力的,这是一个强大的功能,让它扫描特定的包,自动帮我们成批地创建映射器Mapper,这样一来,就能大大减少配置的工作量 2.底层采用了Spring针对自动侦测到的组件的默认命名策略,即把类/接口名字的首字母小写,其他不变,作为映射器的名字,例如映射器接口UserMapper被扫描后创建的映射器bean名为userMapper |
4)开发MyBatis业务
实体类Employee: @Data |
Mapper接口EmployeeMapper public interface EmployeeMapper { |
映射文件EmployeeMapper.xml <mapper namespace="com.atguigu.mapper.EmployeeMapper"> |
5)测试整合MyBatis
@SpringJUnitConfig(locations = "classpath:spring-persist.xml") |
Spring管理业务层事务
1.增加业务层
1)增加业务层接口
public interface EmployeeService { |
2)增加业务层实现类
@Service |
3)扫描业务层注解
<context:component-scan base-package="com.atguigu.service"></context:component-scan> |
4)测试:发现没有事务
@SpringJUnitConfig(locations = "classpath:spring-persist.xml") |
2.添加业务层事务
1)配置事务管理器+启动事务注解
<!--配置事务管理器--> |
2)给业务层方法添加事务功能
@Service |
3)测试事务功能
<logger name="org.springframework.jdbc.datasource.DataSourceTransactionManager" level="DEBUG"/> |
Spring整合SpringMVC
1.环境准备
1)将Java项目变为Web项目
可以使用JBLJAVATOWEB插件:<packaging>war</packaging> |
2)web.xml
<!--配置ContextLoaderListener--> |
3)spring-mvc.xml
<!--组件扫描:扫描@Controller--> |
4)部署项目并运行
可以使用idea绑定的tomcat,也可以使用tomcat7-maven-plugin <build> |
2.查询所有员工
1)开发控制器
@Controller |
2)开发页面
<a th:href="@{/employee}">查询员工列表</a><br> |
<!DOCTYPE html> |
3)测试
3.添加员工
1)配置视图控制器
<mvc:view-controller path="/employee/page/toAdd" view-name="empAdd"></mvc:view-controller> |
2)开发页面
<h3>添加员工</h3> |
3)开发控制器
@PostMapping |
SSM整合下的分页功能
1.理解分页
1)分页的好处: 用户体验较好 服务器端每次只查询一部分数据,内存压力减小 对冷数据减少查询的次数,据此对系统性能进行优化 2)分页的SQL语句: SELECT emp_id, emp_name, emp_salary FROM t_emp LIMIT 0, 5; #查询第一页数据 SELECT emp_id ,emp_name ,emp_salary FROM t_emp LIMIT 5, 5; #查询第二页数据 SELECT emp_id ,emp_name ,emp_salary FROM t_emp LIMIT 10 ,5; #查询第三页数据 limit (pageNum-1)*pageSize, pageSize 3)分页的三个基本要素: 当前页号:pageNum pageIndex 每页记录数:pageSize 总的记录数:totalCount 4)分页的其他要素:由其他要素计算而来 总页数:pages,100/5=20,101/5=21 前一页:prePage,2--1,1--1 后一页:nextPage,2—3,n--n 是否为第一页:isFirstPage,1 是否为最后一页:isLastPage,pages 是否有前一页:hasPreviousPage 是否有下一页:hasNextPage 所有导航页号:navigatepageNums |
2.分页插件
PageHelper是MyBatis框架的一个插件,用于支持在MyBatis执行分页操作,使用非常方便 GitHub: https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md 码云: |
添加依赖 <!--MyBatis分页插件--> |
给SqlSessionFactoryBean注入插件属性 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> |
3.分页操作
1)开发控制器
@GetMapping("/get/page/{pageNum}") |
2)开发业务层
public PageInfo<Employee> findByPage(Integer pageNum, int pageSize); |
@Override |
3)开发视图层
<a th:href="@{/employee/get/page/1}">查询员工列表(分页)</a> |
<tr th:each="emp,status:${pageInfo.list}" th:class="${status.index}/2==0?'white':'beige'"> |
4.PageHelper的更多细节
问题1:调用findAll()方法返回是empList,并作为参数传给PageInfo对象,而PageInfo类的其他分页属性是如何计算出来的 List<Employee> empList = this.employeeMapper.findAll(); return new PageInfo<Employee>(empList, 8); 返回值其实是Page,是ArrayList的一个子类,不仅包含emp数据,也包括各种分页属性,也就是emp数据以及各种分页属性都是包含在findAll()的返回值中的,进入PageInfo只是进行了结构的转换 |
问题2:既然Page中已经有了分页的员工数据,以及分页的各个属性,为什么还要封装到PageInfo中,直接返回Page不可以吗? 可以,但是没有navigatepageNums、navigatePages、prePage、nextPage、startRow、endRow的相关信息,所以功能受限 |