①Spring是一个开源框架 ②Spring为简化企业级开发而生,使用Spring开发可以将Bean对象,Dao组件对象,Service组件对象等交给Spring容器来管理,这样使得很多复杂的代码在Spring中开发却变得非常的优雅和简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。 ③Spring是一个IOC(DI)和AOP容器框架。 ④Spring的优良特性 [1]非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API [2]控制反转:IOC——Inversion of Control,指的是将对象的创建权交给Spring去创建。使用Spring之前,对象的创建都是由我们自己在代码中new创建。而使用Spring之后。对象的创建都是由给了Spring框架。 [3]依赖注入:DI——Dependency Injection,是指依赖的对象不需要手动调用setXX方法去设置,而是通过配置赋值。 [4]面向切面编程:Aspect Oriented Programming——AOP面向切面编程 [5]容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期 [6]组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。 [7]一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)。
Spring MVC框架是一个开源的Java平台,为开发强大的基于JavaWeb应用程序提供全面的基础架构支持,并且使用起来非常简单容易。 Spring web MVC框架提供了MVC(模型 - 视图 - 控制器)架构,用于开发灵活和松散耦合的Web应用程序的组件。 MVC模式使应用程序的不同组件(输入逻辑,业务逻辑和UI逻辑)合理有效的分离,同时又有效的将各组件组合一起完成功能。 模型(Model) 封装了应用程序数据,通常它们将由POJO类组成。 视图(View) 负责渲染模型数据,一般来说它负责生成客户端浏览器可以解释HTML输出。 控制器(Controller) 负责处理用户请求并构建适当的模型,并将其传递给视图进行渲染。
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录.
G:\AshareDay\share\SSM01\02_spring\day09\springmvc第四天下午完结 ssm整合\代码\ssmPro\web\WEB-INF\lib
mybatis核心配置文件 由spring.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"> <!--mybatis的核心配置文件--> <configuration> <!--支持 pagehelper分页工具--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> </plugin> </plugins> </configuration>spring.xml 整合了mybatis 和springmvc的核心配置文件
1.开启注解扫描 (在spring容器中排除对控制器的管理)
2.引入数据库连接的属性文件(classpath:jdbc.properties)
3.在spring容器中管理数据源
4.整合mybatis 连接数据源 dataSource 引用mybatis-config.xml
5.sqlsession.getMapper(BookMapper.class)代理对象的创建
6.事务管理器 、事务注解驱动 <?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:mybatis="http://mybatis.org/schema/mybatis-spring" 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 https://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--spring 的配置文件--> <!--开启注解扫描 mapper service--> <context:component-scan base-package="com.atguigu"> <!--在spring容器中排除对控制器的管理--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--引入数据库连接的属性文件--> <context:property-placeholder location="classpath:jdbc.properties" /> <!--spring容器中管理数据源--> <bean id="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.driver}"></property> </bean> <!--API sqlsession sqlsessionFactory sqlsessionFactoryBuilder spring 整合mybatis --> <bean 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="mapperLocations" value="classpath:com/atguigu/mapper/*.xml"></property> <!--实体类别名(直接注册包的地址)--> <property name="typeAliasesPackage" value="com.atguigu.pojo"></property> </bean> <!--sqlsession.getMapper(BookMapper.class) 代理对象的创建--> <mybatis:scan base-package="com.atguigu.mapper"></mybatis:scan> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务注解驱动--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>1.springmvc容器中管理控制器 2.视图解析器 3.mvc默认servlet处理器 4.mvc默认注解驱动
<?xml version="1.0" encoding="UTF-8"?> -<beans 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 http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd" 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" xmlns="http://www.springframework.org/schema/beans"> -<context:component-scan use-default-filters="false" base-package="com.atguigu"> <!--springmvc 的容器中管理控制器--> <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan> <!--视图解析器--> -<bean class="org.springframework.web.servlet.view.InternalResourceViewResolve"> <property value="/WEB-INF/views/" name="prefix"/> <property value=".jsp" name="suffix"/> </bean> <!--springmvc 默认servlet处理器处理 静态资源 --> <mvc:default-servlet-handler/> <!--springmvc 处理 动态请求 (去controller找方法) --> <context:include-filter <mvc:annotation-driven/> </beans>导入 jar包 : pagehelper-5.0.0.jar 此jra包依赖于jsqlparser-0.9.5.jar
@Autowired private BookService bookService; @RequestMapping(value = "/queryAll",method= RequestMethod.GET) public String queryAll( @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize" ,required = false,defaultValue = "2") Integer pageSize, Map<String,Object> map){ PageHelper.startPage(pageNum,pageSize); List<Book> list = bookService.queryAll(); PageInfo<Book> pageInfo = new PageInfo<>(list,5); map.put("pageInfo",pageInfo); return "bookList"; }