SqlMapConfig.xml: mybatis的核心配置文件, 创建mybtias的运行环境.
SqlSessionFactory对象: 用来创建sqlSession会话对象, 故只需要创建一次此工厂即可(单例).
SqlSession对象: 封装了对数据库的CRUD操作.
Mapped Statement: 是mybatis底层封装的一个对象,包装了mybatis配置信息以及sql映射信息. Mapper.xml中,一个sql对应一个MappedStatement对象, sql的id即是MappedStatement的id.示例:
<?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> <properties resource="db.properties"/> <!-- 和spring整合后 environments配置将废除--> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理--> <transactionManager type="JDBC" /> <!-- 数据库连接池--> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> </configuration>需求: 查询订单orders, 以及订单对应的客户信息.
思路: 定义一个OrdersVo, 继承Orders, 且多了一个username字段. 代码如下:
<!-- 查询所有订单信息 --> <select id="findOrdersList" resultType="onlien.bendou.mybatis.po.OrdersVo"> SELECT orders.*, user.username, user.address FROM orders, user WHERE orders.user_id = user.id </select>思路: Orders实体类中新增一个user对象. 则Mapper.xml中查询方式如下:
<!-- 查询订单关联用户信息使用resultmap --> <resultMap type="online.bendou.po.Orders" id="orderUserResultMap"> <id column="id" property="id"/> <result column="user_id" property="userId"/> <result column="number" property="number"/> <result column="createtime" property="createtime"/> <result column="note" property="note"/> <!-- 一对一关联映射 --> <!-- property:Orders对象的user属性 javaType:user属性对应 的类型 -->Ty <association property="user" javaType="online.bendou.po.User"> <!-- column:user表的主键对应的列 property:user对象中id属性--> <id column="user_id" property="id"/> <result column="username" property="username"/> </association> </resultMap> <select id="findOrdersWithUserResultMap" resultMap="orderUserResultMap"> SELECT o.id, o.user_id, o.number, o.createtime, o.note, u.username FROM orders o JOIN `user` u ON u.id = o.user_id </select>注意: 如果封装的是多个User, 及List< User >, 则需要将< association> 标签换为< collection >
示例: SqlMapConfig.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="online.bendou.mybatis.pojo"/> </typeAliases> </configuration>applicationContext-mybatis :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据库连接池 --> <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="10" /> <property name="maxIdle" value="5" /> </bean> <!-- mapper配置 --> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <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="online.bendou.mybatis.mapper"></property> </bean> </beans>