mybatis集成在spring中使用及理解

很多项目中使用到了mybatis框架,一般也都是把mybatis集成在spring中。为什么要使用mybatis,以及使用mybatis优势在哪,这个往往是我们要关心以及要弄明白的地方。

       mybatis官网给出的定义:MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

      也许官网给出的定义太过抽象,下面以我自己的理解来说明下。(以例子来说明)

     一、配置集成到spring的mybatis相关配置(applicationContext.xml)

            配置文件如下:

 1 <!-- 配置数据源 -->
 2     <bean id="dataSource"
 3         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 4         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 5         <property name="url" value="jdbc:mysql://localhost:3306/db_crm"/>
 6         <property name="username" value="root"/>
 7         <property name="password" value="passwd"/>
 8     </bean>
 9 
10     <!-- 配置mybatis的sqlSessionFactory -->
11     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
12         <property name="dataSource" ref="dataSource" />
13         <!-- 自动扫描mappers.xml文件 -->
14         <property name="mapperLocations" value="classpath:com/hik/mappers/*.xml"></property>
15         <!-- mybatis配置文件 -->
16         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
17     </bean>
18 
19     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
20     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
21         <property name="basePackage" value="com.hik.dao" />
22         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
23     </bean>
applicationContext.xml

mybatis框架使用实际上是优化了dao实现层,我们都知道dao层是与数据库打交道,dao层定义接口,dao实现层相关代码实现数据的增删改查操作。

而mybatis的使用,直接省去了dao实现层的代码,通过配置文件实现映射。框架内部会自动把mybatis配置文件转为dao实现层代码,实现数据的增删改查操作。

其本质都是一样的。所以,为什么我们使用mybatis,是因为它代码实现起来,更加简洁,通过配置文件映射更加直观。我们使用时,只需遵循它的配置规则即可。

如下:

    dao接口层:

 1 /**
 2  * 
 3  */
 4 package com.hik.dao;
 5 
 6 import java.util.List;
 7 import java.util.Map;
 8 
 9 import com.hik.entity.User;
10 
11 /**
12  * @ClassName: UserDao
13  * @Description: 用户Dao接口
14  * @author jed
15  * @date 2017年8月13日上午11:47:48
16  *
17  */
18 public interface UserDao {
19 
20     /**
21      * @MethodName: login
22      * @Description: 用户登录 
23      * @author jed
24      * @date 2017年8月13日下午4:35:42
25      * @param @param user
26      * @param @return    
27      * @return User    返回类型
28      * @param user
29      * @return
30      *
31      */
32     public User login(User user);
33     
34     /**
35      * 
36      * @MethodName: find
37      * @Description: 查询所有用户
38      * @author jed
39      * @date 2017年8月19日上午9:50:46
40      * @param @return    
41      * @return List<User>    返回类型
42      * @return
43      *
44      */
45     public List<User> find(Map<String, Object> map);
46     
47     /**
48      * 
49      * @MethodName: getTotal
50      * @Description: 获取总记录数
51      * @author jed
52      * @date 2017年8月19日上午9:52:52
53      * @param @param map
54      * @param @return    
55      * @return Long    返回类型
56      * @param map
57      * @return
58      *
59      */
60     public Long getTotal(Map<String, Object> map);
61 }
View Code

使用mybatis,我们不需要写daoImpl代码

只需在mapper配置文件中写数据库语句即可。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.hik.dao.UserDao">
 6 
 7     <resultMap type="User" id="UserResult">
 8         <result property="id" column="id"/>
 9         <result property="userName" column="userName"/>
10         <result property="password" column="password"/>
11         <result property="trueName" column="trueName"/>
12         <result property="email" column="email"/>
13         <result property="phone" column="phone"/>
14         <result property="roleName" column="roleName"/>
15     </resultMap>
16     
17     <select id="login" parameterType="User" resultMap="UserResult">
18         select * from t_user where userName=#{userName} and password=#{password} and roleName=#{roleName}
19     </select>
20     
21     <select id="find" parameterType="Map" resultMap="UserResult">
22         select * from t_user
23         <where>
24             <if test="userName!=null and userName!=''">
25                 and userName like #{userName}
26             </if>
27         </where>
28         <if test="start!=null and size!=null">
29             limit #{start},#{size}
30         </if>
31     </select>
32     
33     <select id="getTotal" parameterType="Map" resultType="Long">
34         select count(*) from t_user
35         <where>
36             <if test="userName!=null and userName!=''">
37                 and userName like #{userName}
38             </if>
39         </where>
40     </select>
41 </mapper>
View Code

命名空间正好是dao,对应dao层,其实mapper的配置文件,我们就可以看作是daoImpl。

这里的实体对象,配置成resultMap,作为sql查询语句的返回值(resultType)。这里可省去resultType

where标签中if标签用于条件判断,里面的and会自动去掉。保证sql语句的正确性。

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- 别名 -->
 7     <typeAliases>
 8         <package name="com.hik.entity"/>
 9     </typeAliases>
10 </configuration>
View Code

而mybatis-config.xml配置中的别名,便于我们找到javaBean实体。

使用mybatis框架,确实方便不少。

猜你喜欢

转载自www.cnblogs.com/TomJay/p/9587888.html