可能大家会说有了Hibernate为什么引入Mybatis呢!这个主要就是hibernate和mybatis各有自己的优势劣势。

首先,通过项目的经验,Hibernate在项目整套架构中,在某些地方,确实有它的优势!

如:在设计model时候,只需要做一次工作,就是在项目的创建model对象和各个属性,当<prop key="hibernate.hbm2ddl.auto">create</prop>时,启动项目,hibernate会在数据库中自动创建对应表和表的相关字段。

但是hibernate的不足就是在一些复杂查询的时候,如果对HQL不是很属性,那就陷入了迷茫,所以这里引起Mybatis来弥补这个问题!

1、需要的jar包

 

2、新建spring-mybatis,配置文件

  1)、spring-mybatis.xml

<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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

<!--下面是mybatis 的配置 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:sql-mapping/*.xml" />
        <property name="configLocation" value="classpath:sql-map-config.xml" />
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.iobs.manager.repo" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

  <!-- 这里因为是只查询,所以不配事物管理bean,如果要的话,可以把事物交给spring来管理 --> </beans>

  2)、web.xml加载对应xml文件

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
 /WEB-INF/web-context.xml,
 /WEB-INF/spring-hibernate.xml
 /WEB-INF/spring-mybatis.xml
</param-value>
</context-param>

 

3、新建sql-map-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>
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="multipleResultSetsEnabled" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <setting name="defaultExecutorType" value="REUSE"/>
        <setting name="defaultStatementTimeout" value="25000"/>
    </settings>

</configuration>

 

4、测试,需要新建一个sqlmap进行测试,这里新建一个

<?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.iobs.manager.repo.UserRepo">

	<resultMap id="user" type="com.iobs.manager.entity.User">
		<result column="id" property="id" />
		<result column="jid" property="jid" />
		<result column="user_name" property="user_name" />
		<result column="env_used" property="env_used" />
		<result column="create_date" property="create_date" />
	</resultMap>



	<select id="queryListUsers" resultMap="user">
		SELECT * FROM st_user_info
	</select>


</mapper>

 红色部分为对应的接口类,相当于这个sqlmap是UserRepo接口的实现类

定义UserRepo接口

package com.iobs.manager.repo;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.iobs.manager.entity.User;

@Repository
public interface UserRepo {
	public List<User> queryListUsers();
}

 

测试,可以用action进行测试,也可以junit进行测试!看是否输出了st_user_info表的信息

 

posted on 2016-11-11 17:37  直到没朋友  阅读(180)  评论(0)    收藏  举报