Mybatis层
Mybatis
mybatis-config.xml
1.配置数据源 交给spring去做
2.TypeAliases
6.注册 Mapper
pojo
3.对应数据库写实体类
dao.xxxMapper
4.Mapper接口
dao.xxxMapper.xml
5.SQL语句
service.xxxService
7.编写xxxService接口
service.xxxServiceImpl
8.在xxxService实现类里面调用dao层接口,并写下dao层接口的set方法,方便spring注入
db.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ooms?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.userName = root
jdbc.passWord = 123456
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>
<typeAliases>
<package name="com.zyy.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.zyy.dao.BooksMapper"/>
</mappers>
</configuration>
Spring层
dt.properties
1.将数据库配置写入文件
applicationContext.xml
2.创建文件
7.import导入spring-dao.xml
12.import导入spring-service.xml
spring-dao.xml
3.关联数据库配置文件<context:property-placeholder location=""/>
4.连接池/DriverManageDataSource
5.sqlSessionFactory(导入数据源、绑定Mybatis配置文件)
6.配置dao接口扫描包MapperScannerConfigurer(引入上一步的sqlSessionFactory、选择要扫描的包) 动态的将dao接口注入到了spring容器中
spring-service.xml
8.扫描service下面的包(为了注解注入)<context:component-scan base-package=""/>
9.将业务类注入到spring,properties属性为dao层接口(联系Mybatis的第8步和Spring的第6步)
10.声明式事务DataSourceTransactionManager(注入数据源)
11.AOP事务支持
spring-dao.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 关联mybatis配置文件-->
<context:property-placeholder location="classpath:dataBase.properties" />
<!-- 连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.userName}"/>
<property name="password" value="${jdbc.passWord}"/>
</bean>
<!-- sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 导入数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 配置Dao接口扫描包 动态的将dao接口注入到spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 引入上一步注入了的sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 选择要扫描的包-->
<property name="basePackage" value="com.zyy.dao"/>
</bean>
</beans>
spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描service下面的包 方便使用注解注入-->
<context:component-scan base-package="com.zyy.service"/>
<!-- 注入service实现类xxxServiceImpl-->
<bean id="booksServiceImpl" class="com.zyy.service.BooksServiceImpl">
<!-- 注入Dao接口 这里可以直接使用ref引用是因为在spring-dao.xml里面
配置了Dao配置扫描包 已经动态将bookMapper注入了 -->
<property name="booksMapper" ref="booksMapper"/>
</bean>
<!-- 声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Aop事务支持-->
</beans>
SpringMVC层
SpringMVC
1.添加web支持
spring-mvc.xml
2.创建文件
6.注解驱动mvc:annotation-drivern/
7.静态资源过滤mvc:default-service-handler/
8.扫描包:
9.视图解析器
web.xml
3.DispatcherServlet(初始化参数contextConfigLocation、设置优先级)
5.乱码过滤
applicationContext.xml
4.import导入spring-mvc.xml
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 添加注解驱动-->
<mvc:annotation-driven/>
<!-- 静态资源过滤-->
<mvc:default-servlet-handler/>
<!-- 扫描包-->
<context:component-scan base-package="com.zyy.controller"/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
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">
<!-- DispatcherServlet-->
<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:applicationContext.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>
<!-- 乱码过滤-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>