整合思路:首先,先划分大层次,分为三层:表现层->业务层->数据访问层,再加上web.xml。
spring和mybatis整合
1.spring帮mybatis管理数据库信息
2.spring帮mybatis创建会话工厂
3.spring自动帮mybatis创建接口文件的实现类
整合步骤:
1.编写web.xml,
前端控制器(spring-mvc)
<!--spring-mvc-->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定springmvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--标记是否在tomcat启动时加载servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
监听器(spring)
<!--spring监听器
spring监听器的作用-ContextLoaderListener
在启动web容器时,自动装配springapplicationContext.xml的配置信息
因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器。启动容器时,就会默认执行它实现的方法。
在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载过程配置由ContextLoader来完成
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
监听器在运行时,需要配置参数,读取sring配置信息
<!--spring参数,name固定-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
2.编写controller层
创建springmvc.xml
<!--注解驱动-->
<mvc:annotation-driven />
<!--组件扫描,此处应该只扫描controller,但考虑到该模板每层都需要扫描,就这么写了-->
<context:component-scan base-package="com.ssm"/>
<!--jsp视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/templates/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--静态资源过滤-->
<mvc:resources mapping="static/asserts/**" location="static/asserts/"/>
<!-- 不处理静态资源 -->
<mvc:default-servlet-handler/>
3.编写service层(较为核心操作)
将数据库信息外部化,写入外部文件datasource.properties
#数据库信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=jf
jdbc.password=121
编写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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--组件扫描,在springmvc.xml中将ssm包下内容全部扫描了,此处就不写了-->
<!--<context:component-scan base-package="com.ssm.service,com.ssm.dao,com.ssm.domain"/>-->
<!--加载外部资源-->
<context:property-placeholder location="classpath:datasource.properties"/>
<!--数据源,使用连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--四大参数,用"${}"获取外部文件中的key-->
<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>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--sqlSessionFactory会话工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引用数据源-->
<property name="dataSource" ref="dataSource"/>
<!--扫描Mybatis/mapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置自动扫描MapperScannerConfigurer-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入需要扫描的包,它会自动扫描这个包下的接口,并实现这些接口,并且会实例化这些接口的实现类-->
<property name="basePackage" value="com.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
那么对于Mybatis的配置文件,清空<configuration>标签内全部内容,保留一个空的配置文件,以便日后对mybatis的扩展。(日志功能等)
4.编写dao层
编写mapper.xml,注意id对应类中方法名
<?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">
<!--namespace为对应的接口全路径-->
<mapper namespace="com.ssm.dao.StuDao">
<!--type为对应的实体类-->
<resultMap id="stuMap" type="com.ssm.domain.Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="sbirth" property="sbirth"/>
</resultMap>
<!--List<Student> getAll()-->
<select id="getAll" resultMap="stuMap">
select * from student
<where>
<if test="name != null and name != ''">
and name like "%"#{name}"%"
</if>
<if test="classid != null and classid != '-1'">
and classid = #{classid}
</if>
</where>
order by classid limit #{start},#{rows}
</select>
<!--void addStu()-->
<insert id="addStu">
insert into student values(0,#{name},#{sbirth},#{classid})
</insert>
</mapper>
整合的基本操作就完成了。
对于分页操作,之前只是定义一个分页的工具类,至于页数等操作还是选择到了不同的层去计算分析,那么可以考虑,既然它是一个工具类,即使在对于的层拿到对应需要的数据,它仍然是要去计算的,那么可以直接在分页工具类中去计算,不需要将操作分的太细。