一 通过逆向工程生成po与mapper
MyBatis官方提供了强大的逆向工程代码,即可以通过数据库生成po与mapper,这里不去做过多的讨论,争取写完这一系列后更新一篇关于mybatis逆向工程的博文。
生成后目录如下(注:ItemsMapperCustom,ItemsCustom,ItemsQueryVo为自定义po与mapper,下一部分将讲解):
二 手工定义po与mapper
随着项目的进展和改进,往往需要开发人员自己定义po与mapper,实现综合查询等功能。
1 po下的ItemsCustom与ItemsQueryVo
ItemsCustom是为了满足商品信息扩展而实现的类。我们知道,通过逆向工程生成的po来源的属性其实都是表中的字段,当我们想对商品增加属性时,如果还是通过逆向工程来再次生成po,那么就需要修改数据库,再执行逆向工程,这个工作量是很大的。
package com.lpc.po;
/**
* 商品信息的扩展类
*/
public class ItemsCustom extends Items{
}
对于ItemsQueryVo,它是为了商品的包装类。
package com.lpc.po;
/**
* 商品的包装类
*/
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统的可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom;
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
}
2 ItemsMapperCustom与ItemsMapperCustom.xml
ItemsMapperCustom
这里只实现查询商品的简单功能。
/**
* 商品自定义mapper
*/
public interface ItemsMapperCustom {
// 商品查询列表
List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
throws Exception;
}
在ItemsMapperCustom.xml中使用了sql片段,这在之前与mybatis相关的博文中介绍过。
<?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.lpc.mapper.ItemsMapperCustom">
<!-- 商品查询的sql片段
建议是以单表为单位定义查询条件
建议将常用的查询条件都写出来
-->
<sql id="query_items_where">
<if test="itemsCustom!=null">
<if test="itemsCustom.name!=null and itemsCustom.name!=''">
and name like '%${itemsCustom.name}%'
</if>
<if test="itemsCustom.id!=null">
and id = #{itemsCustom.id}
</if>
</if>
</sql>
<!-- 商品查询
parameterType:输入 查询条件
-->
<select id="findItemsList" parameterType="com.lpc.po.ItemsQueryVo"
resultType="com.lpc.po.ItemsCustom">
SELECT * FROM items
<where>
<include refid="query_items_where"/>
</where>
</select>
</mapper>
三 整合mybatis与spring
在resources中,新建mybatis与spring子包,同时新建数据库的配置文件。
在mybatis包下,新建SqlMapperConfig,xml这个mybatis配置文件。
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 定义 别名 -->
<typeAliases>
<!-- 批量定义别名 指定包路径,自动扫描包下的POJO 别名默认为类名-->
<package name="com.lpc.po"/>
</typeAliases>
<!-- 配置数据源 事务等 因为与Spring整合 这里不再配置 -->
<!-- 扫描 Mapper 因为配置了Spring和mybatis的整合包,所以不再配置-->
</configuration>
在spring包下,新建applicationContext-dao.xml配置文件。
<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"
xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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 ">
<!-- 扫描配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<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"/>
<property name="configLocation" value="classpath:mybatis/SqlMapperConfig.xml"/>
</bean>
<!--
MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象,
自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/>
<property name="basePackage" value="com.lpc.mapper"/>
</bean>
</beans>
至此,整合完成。
四 测试
在Test包下,测试整合是否成功。
package com.lpc.mapper;
import com.lpc.po.Items;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class daoTest {
@Test
public void daoTest(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) ctx.getBean("SqlSessionFactory");
SqlSession session = sqlSessionFactory.openSession();
ItemsMapper itemsMapper = (ItemsMapper) ctx.getBean("itemsMapper");
Items items = itemsMapper.selectByPrimaryKey(1);
System.out.println(items);
}
}
输出结果