亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Mybatis中配置Mapper的方法

系統(tǒng) 1781 0

Mybatis中 配置Mapper的方法

?

?????? 在這篇文章中我主要想講一下Mybatis配置文件中mappers元素的配置。關(guān)于基礎(chǔ)部分的內(nèi)容可以參考 http://haohaoxuexi.iteye.com/blog/1333271

?????? 我們知道在Mybatis中定義Mapper信息有兩種方式,一種是利用xml寫一個(gè)對應(yīng)的包含Mapper信息的配置文件;另一種就是定義一個(gè)Mapper接口,然后定義一些相應(yīng)的操作方法,再輔以相應(yīng)的操作注解。

?????? 現(xiàn)假設(shè)我有這樣一個(gè)實(shí)體類:

      package com.tiantian.mybatis.model;
 
public class User {
 
    private int id;
    private String name;
    private int age;
    public int getId() {
       return id;
    }
    public void setId(int id) {
       this.id = id;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }
   
}
    

?

?????? 它對應(yīng)的數(shù)據(jù)庫表結(jié)構(gòu)是這樣的:

Mybatis中配置Mapper的方法
?

然后我要利用Mybatis對它做一個(gè)簡單的增刪改查操作,那么如果利用xml配置Mapper的方式來定義的話,我對應(yīng)的UserMapper.xml文件會是這樣:

      <?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.tiantian.mybatis.mapper.UserMapper">
    <insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyColumn="id">
       insert into t_user(name, age) values(#{name}, #{age})
    </insert>
   
    <update id="updateUser" parameterType="User">
       update t_user set name=#{name}, age=#{age} where id=#{id}
    </update>
   
    <select id="findById" parameterType="int" resultType="User">
       select * from t_user where id=#{id}
    </select>
   
    <delete id="deleteUser" parameterType="int">
       delete from t_user where id=#{id}
    </delete>
</mapper>
    

?

?????? 如果使用接口加注解的方式,那么我們的UserMapper接口應(yīng)該這樣定義:

      package com.tiantian.mybatis.mapperinterface;
 
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
 
import com.tiantian.mybatis.model.User;
 
public interface UserMapper {
 
    @Insert("insert into t_user(name, age) values(#{name}, #{age})")
    public void insertUser(User user);
   
    @Update("update t_user set name=#{name}, age=#{age} where id=#{id}")
    public void updateUser(User user);
   
    @Select("select * from t_user where id=#{id}")
    public User findById(int id);
   
    @Delete("delete from t_user where id=#{id}")
    public void deleteUser(int id);
   
}
    

?

?????? 注意看這里我故意把UserMapper接口的namespace也就是它的包名置為與UserMapper.xml的namespace屬性不一樣。這主要是為了要更好的講以下的內(nèi)容。

?????? 接下來要做的就是把Mapper信息注冊到Mybatis的配置中,告訴Mybatis我們定義了哪些Mapper信息。這主要是在Mybatis的配置文件中通過mappers元素來進(jìn)行的。在以前版本的Mybatis中我們在Mybatis的配置文件中需要這樣定義Mapper信息資源的位置。

          <mappers>
       <mapper resource="com/tiantian/mybatis/mapper/UserMapper1.xml"/>
       <mapper url="file:///E:/UserMapper.xml"/>
    </mappers>
    

?

?????? 這主要是通過mapper元素的resource和url屬性來指定的,resource屬性指定的是相對于跟類路徑下的資源,url屬性指定的是通過URL可以獲取到的資源。這有一點(diǎn)不好的地方,當(dāng)我們使用Mapper接口加注解來定義當(dāng)前Mapper的操作信息時(shí),我們還需要定義一個(gè)與它對應(yīng)的Mapper.xml文件。如:

      <?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.tiantian.mybatis.mapperinterface.UserMapper">
 
</mapper>
    

?

?

      package com.tiantian.mybatis.mapperinterface;
 
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
 
import com.tiantian.mybatis.model.User;
 
public interface UserMapper {
 
    @Insert("insert into t_user(name, age) values(#{name}, #{age})")
    public void insertUser(User user);
   
    @Update("update t_user set name=#{name}, age=#{age} where id=#{id}")
    public void updateUser(User user);
   
    @Select("select * from t_user where id=#{id}")
    public User findById(int id);
   
    @Delete("delete from t_user where id=#{id}")
    public void deleteUser(int id);
   
}
    

?

?

?????? 我發(fā)現(xiàn)在現(xiàn)在使用的這個(gè)Mybatis3.2.1中這個(gè)問題已經(jīng)得到了改善,現(xiàn)在我們在定義基于接口的定義的Mapper時(shí)可以通過一個(gè)class屬性來指定接口。

          <mappers>
       <mapper class="com.tiantian.mybatis.mapperinterface.UserMapper"/>
    </mappers>
    

?

?????? 除了通過class屬性指定Mapper接口外,Mybatis還為我們提供了一個(gè)可以同時(shí)指定多個(gè)Mapper接口的方法。在現(xiàn)在的Mybatis版本中我們可以在mappers元素下面定義一個(gè)package子元素,用以指定Mapper接口所在的包,這樣Mybatis就會把這個(gè)包下面的所有Mapper接口都進(jìn)行注冊。

          <mappers>
       <package name="com.tiantian.mybatis.mapperinterface"/>
    </mappers>
    

?

?????? 這里的一個(gè)package只針對于一個(gè)包。當(dāng)在多個(gè)包里面定義有Mapper接口時(shí),我們需要定義對應(yīng)的多個(gè)package元素。

這四種注冊Mapper的方式就是我想在這篇文章中表達(dá)的。總結(jié)一下:

          <mappers>
       <!-- 通過package元素將會把指定包下面的所有Mapper接口進(jìn)行注冊 -->
       <package name="com.tiantian.mybatis.mapperinterface"/>
       <!-- 通過mapper元素的resource屬性可以指定一個(gè)相對于類路徑的Mapper.xml文件 -->
       <mapper resource="com/tiantian/mybatis/mapper/UserMapper.xml"/>
       <!-- 通過mapper元素的url屬性可以指定一個(gè)通過URL請求道的Mapper.xml文件 -->
       <mapper url="file:///E:/UserMapper.xml"/>
       <!-- 通過mapper元素的class屬性可以指定一個(gè)Mapper接口進(jìn)行注冊 -->
       <mapper class="com.tiantian.mybatis.mapperinterface.UserMapper"/>
    </mappers>
    

?

?????? 當(dāng)使用mapper元素進(jìn)行Mapper定義的時(shí)候需要注意:mapper的三個(gè)屬性resource、url和class對于每個(gè)mapper元素只能指定一個(gè),要么指定resource屬性,要么指定url屬性,要么指定class屬性,不能都指定,也不能都不指定。

下面將對上面的代碼給出一些對應(yīng)的測試代碼。先貼出測試對應(yīng)的Mybatis的配置文件:

?

      <?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="config/jdbc.properties"></properties>
    <typeAliases>
       <package name="com.tiantian.mybatis.model"/>
    </typeAliases>
    <environments default="development">
       <environment id="development">
           <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>
    <mappers>
       <mapper resource="com/tiantian/mybatis/mapper/UserMapper.xml"/>
       <package name="com.tiantian.mybatis.mapperinterface"/>
    </mappers>
</configuration>
    

?

?????? 1.對于使用xml方式定義的UserMapper.xml,然后直接使用SqlSession訪問定義在其中的statement的測試:

      package com.tiantian.mybatis.test;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Before;
import org.junit.Test;
 
import com.tiantian.mybatis.model.User;
import com.tiantian.mybatis.util.Util;
 
/**
 *
 * 這個(gè)類主要用來測試直接使用SqlSession訪問定義在UserMapper.xml文件中的statement
 *
 */
public class UserMapperTest {
 
    SqlSessionFactory sqlSessionFactory = null;
   
    @Before
    public void before() {
       sqlSessionFactory = Util.getSqlSessionFactory();
    }
   
    @Test
    public void testInsert() {
       SqlSession sqlSession = sqlSessionFactory.openSession();
       try {
           User user = new User();
           user.setName("張三");
           user.setAge(30);
           sqlSession.insert("com.tiantian.mybatis.mapper.UserMapper.insertUser", user);
           sqlSession.commit();
       } finally {
           sqlSession.close();
       }
    }
   
    @Test
    public void testUpdate() {
       SqlSession sqlSession = sqlSessionFactory.openSession();
       try {
           User user = new User();
           user.setId(1);
           user.setName("李四");
           user.setAge(34);
           sqlSession.update("com.tiantian.mybatis.mapper.UserMapper.updateUser", user);
           sqlSession.commit();
       } finally {
           sqlSession.close();
       }
    }
   
    @Test
    public void testFind() {
       SqlSession sqlSession = sqlSessionFactory.openSession();
       try {
           User user = sqlSession.selectOne("com.tiantian.mybatis.mapper.UserMapper.findById", 1);
           System.out.println(user.getId() + "--" + user.getName() + "--" + user.getAge());
       } finally {
           sqlSession.close();
       }
    }
   
    @Test
    public void testDelele() {
       SqlSession sqlSession = sqlSessionFactory.openSession();
       try {
           sqlSession.delete("com.tiantian.mybatis.mapper.UserMapper.deleteUser", 2);
           sqlSession.commit();
       } finally {
           sqlSession.close();
       }
    }
   
}
    

?

?

?????? 2.對于使用Mapper接口加對應(yīng)的注解來定義的Mapper信息直接使用SqlSession訪問Mapper接口中使用注解定義好的statement的測試:

      package com.tiantian.mybatis.test;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Before;
import org.junit.Test;
 
import com.tiantian.mybatis.model.User;
import com.tiantian.mybatis.util.Util;
 
/**
 *
 *這個(gè)類是測試直接使用SqlSession訪問UserMapper接口中使用注解定義好的statement
 *
 */
public class UserMapperTest2 {
 
       SqlSessionFactory sqlSessionFactory = null;
      
       @Before
       public void before() {
              sqlSessionFactory = Util.getSqlSessionFactory();
       }
      
       @Test
       public void testInsert() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     User user = new User();
                     user.setName("張三");
                     user.setAge(30);
                     sqlSession.insert("com.tiantian.mybatis.mapperinterface.UserMapper.insertUser", user);
                     sqlSession.commit();
              } finally {
                     sqlSession.close();
              }
       }
      
       @Test
       public void testUpdate() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     User user = new User();
                     user.setId(1);
                     user.setName("李四");
                     user.setAge(34);
                     sqlSession.update("com.tiantian.mybatis.mapperinterface.UserMapper.updateUser", user);
                     sqlSession.commit();
              } finally {
                     sqlSession.close();
              }
       }
      
       @Test
       public void testFind() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     User user = sqlSession.selectOne("com.tiantian.mybatis.mapperinterface.UserMapper.findById", 1);
                     System.out.println(user.getId() + "--" + user.getName() + "--" + user.getAge());
              } finally {
                     sqlSession.close();
              }
       }
      
       @Test
       public void testDelele() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     sqlSession.delete("com.tiantian.mybatis.mapperinterface.UserMapper.deleteUser", 3);
                     sqlSession.commit();
              } finally {
                     sqlSession.close();
              }
       }
      
}
    

?

?

?????? 3.對于使用Mapper接口加注解定義好的Mapper信息通過SqlSession獲取其對應(yīng)的Mapper接口來操作其中定義好的statement的測試:

      package com.tiantian.mybatis.test;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Before;
import org.junit.Test;
 
import com.tiantian.mybatis.mapperinterface.UserMapper;
import com.tiantian.mybatis.model.User;
import com.tiantian.mybatis.util.Util;
 
/**
 *
 *這個(gè)類是測試使用SqlSession獲取UserMapper接口來執(zhí)行使用注解定義在UserMapper接口中的statement
 *
 */
public class UserMapperTest3 {
 
       SqlSessionFactory sqlSessionFactory = null;
      
       @Before
       public void before() {
              sqlSessionFactory = Util.getSqlSessionFactory();
       }
      
       @Test
       public void testInsert() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     User user = new User();
                     user.setName("張三");
                     user.setAge(30);
                     UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                     userMapper.insertUser(user);
                     sqlSession.commit();
              } finally {
                     sqlSession.close();
              }
       }
      
       @Test
       public void testUpdate() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     User user = new User();
                     user.setId(1);
                     user.setName("李四");
                     user.setAge(34);
                     UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                     userMapper.updateUser(user);
                     sqlSession.commit();
              } finally {
                     sqlSession.close();
              }
       }
      
       @Test
       public void testFind() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                     User user = userMapper.findById(1);
                     System.out.println(user.getId() + "--" + user.getName() + "--" + user.getAge());
              } finally {
                     sqlSession.close();
              }
       }
      
       @Test
       public void testDelele() {
              SqlSession sqlSession = sqlSessionFactory.openSession();
              try {
                     UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                     userMapper.deleteUser(5);
                     sqlSession.commit();
              } finally {
                     sqlSession.close();
              }
       }
      
}
    

?

?

?

?

?

Mybatis中配置Mapper的方法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 97免费视频在线 | 操天天操| 欧美久久网| 在线 | 一区二区三区四区 | 久久久高清国产999尤物 | 四虎永久精品免费网址大全 | 四虎avtom影院| 成人97在线观看免费高清 | 亚洲精品一区二区三区四区手机版 | 亚洲短视频在线观看 | 女人洗澡一级毛片一级毛片 | 日韩三级一区二区 | 亚洲黄色a | 亚洲国产欧美国产综合一区 | 午夜久久久久久 | 涩涩视频观看 | 免费看一级毛片 | 天天插狠狠干 | 日本一级高清片免费 | 99久久精品国产自免费 | 玖玖国产精品视频 | 欧美猛交xxxxx| 杨幂国产精品福利在线观看 | 国产九九热 | 香蕉久久国产 | 伊人久久大香线蕉亚洲 | 五月天亚洲视频 | 不卡一级毛片免费高清 | 91精品中文字幕 | 久久久久综合网久久 | 一级毛片 在线播放 | 久久国产精品岛国搬运工 | 午夜视频网 | 高清二区| 毛片视频免费观看 | 日韩一区二区三区四区 | 久久久久国产精品免费 | 久久精品国产99久久 | 久久99精品热在线观看15 | 添人人躁日日躁夜夜躁夜夜揉 | 777奇米影视久久激情日韩欧美 |