利用mybatis的逆向工程生成mapper和model后,下一步就是springmvc+mybatis对dao层的整合。
如果是简单的单体查询,直接用mybatis逆向生成的mapper和model就可以了。但是在实际项目中,对数据库的查询一般都是级联的,因此,使用逆向工程所生成的mapper和model就不能满足实际开发的需要,这就需要我们对生成mapper和model进行封装和扩展。
一、对model的封装和扩展
package com.sky.ssm.po;
/**
* 商品扩展类
* 作用:扩展mybatis逆向生成的基础类,减少耦合性
*
*/
public class ItemsCustom extends Items{
//在这里添加表现层需要用到的扩展的item字段
}
ItemsCustom继承了生成的数据库封装类Items,在拥有基础字段的基础上,扩展表现层所需要的字段,这样做的好处就是,当数据库文件发生字段变更时,只需要将Items基础类重新生成即可,而不需要改动其他地方。
package com.sky.ssm.po;
/**
* 商品查询类 作用:为商品的查询提供相应的查询条件
*/
public class QueryItemsCustomVo {
private Items items;
private ItemsCustom itemsCustom;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
}
商品查询类封装代理了商品的基础类和扩展类,为商品的查询提供丰富的查询条件。
二、mapper的扩展
<?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" >
<mapper namespace="com.sky.ssm.mapper.ItemsCustomMapper" >
<!-- sql片段 -->
<sql id="query-items-list-where">
<if test="itemsCustom != null">
<if test="itemsCustom.name != null and itemsCustom.name != ''">
ITEMS.NAME LIKE '%${itemsCustom.name}%'
</if>
</if>
</sql>
<!-- 用实体类的查询类组作为查询条件参数,可以更好的对查询语句进行级联查询和扩展查询字段,
从而不需要改动基础类,从而避免了数据库更改时,造成基础类由于扩展字段太多而无法进行逆向工程的生成
一般在开发中,由逆向工程生成的基础类都不要改动,只要扩展即可。 -->
<!-- ${}符号为数据库动态拼接符号;#{}符号为数据库变量 -->
<select id="queryItemsList"
parameterType="com.sky.ssm.po.QueryItemsCustomVo"
resultType="com.sky.ssm.po.ItemsCustom">
SELECT * FROM ITEMS
<where>
<include refid="query-items-list-where"></include>
</where>
</select>
</mapper>
使用商品的代理类作为查询条件,返回商品的扩展类,方便业务层和表现层的使用。
三、在spring中配置dao
applicationContext-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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 加载db.properties文件的内容,db.properties文件中的key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源,dbcp 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="30"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
</bean>
<!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描的包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
<property name="basePackage" value="com.sky.ssm.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
用到的参数文件:
db.properties
jdbc.username = root
jdbc.password = 123456
jdbc.url = jdbc:mysql://localhost:3306/mybatis
jdbc.driver = com.mysql.jdbc.Driver
项目目录: