上一篇文章總結了一些Dao開發的問題,所以我們這里開始講一種mapper代理的方式去開發。
我先給出mapper代理開發的思路(mapper代理開發的規范):
我們用mapper代理開發時要寫2個:
1.mapper.xml
2.mapper接口
我們寫的mapper接口只要需要遵循一些開發規則,mybatis可以自動生成mapper接口實現類代理對象。(這句話很重要)
我們重點講一下開發規則:
開發規范:
1、在mapper.xml中namespace等於mapper接口地址
2、mapper.java接口中的方法名和mapper.xml中statement的id一致
mapper.xml中是這樣的:
mapper.java接口應該是這樣的:

名字要一模一樣的。
3、mapper.java接口中的方法輸入參數類型和mapper.xml中statement的parameterType指定的類型一致。
4、mapper.java接口中的方法返回值類型和mapper.xml中statement的resultType指定的類型一致。

總結:
以上開發規范主要是對下邊的代碼進行統一生成:
User user = sqlSession.selectOne("test.findUserById", id);
sqlSession.insert("test.insertUser", user);
接下來將案例:
先給出案例的結構:
第一步:編寫: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> <!-- 和spring整合后 environments配置將廢除--> <environments default="development"> <environment id="development"> <!-- 使用jdbc事務管理--> <transactionManager type="JDBC" /> <!-- 數據庫連接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybaits?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- 把映射文件(sqlmap/user.xml)加載進sqlMapConfig.xml--> <mappers> <!-- 把mapper.xml加載進sqlMapConfig.xml--> <mapper resource="mapper/userMapper.xml"/> </mappers> </configuration>
第二步:編寫mapper/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"> <!-- nanmespace:命名空間。 作用就是對sql進行分類話管理,理解Sal分離 注意:使用mapper代理方式,namespace有特殊重要的作用 --> <mapper namespace="cn.itcast.mybatis.Dao.UserMapper"> <!-- 根據id獲取用戶信息 --> <!-- 在映射文件中配置很多sql語句 --> <!-- id:標識映射文件中的sql; 將sql語句封裝到mappedStatement對象中,所以將id稱為statement的id;parmenterType:指定輸入的參數的類型,這里指定的int型 #{}表示一個占位符號; #{id}:其中的id表示接收輸入的參數,參數名稱就是id,如果輸入參數就是簡單類型,#{}中的參數名可以任意,可以value或其它名稱 resultType:指定的sql輸出結果的所映射的java對象類型,select指定resultType表示將單條記錄映射成的java對象; --> <select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User"> select * from user where id = #{id} </select> </mapper>
第三步:UserMapper.java文件
package cn.itcast.mybatis.Dao; import cn.itcast.mybatis.po.User; public interface UserMapper { //這個名字(findUserById)和UserMapper.xml里面的id要一模一樣 public User findUserById(int id); }
第四步:編寫junit測試代碼。編寫Mybatis_mappertest.java
package cn.itcast.mybatis.first; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import cn.itcast.mybatis.Dao.UserMapper; import cn.itcast.mybatis.po.User; public class Mybatis_mappertest { private SqlSessionFactory sqlSessionFactory; @Before public void setup() throws IOException { String resource="SqlMapConfig.xml"; InputStream inputStream= Resources.getResourceAsStream(resource); //主要是生成SqlsessionFactory。 this.sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } @Test public void testMaper() { SqlSession sqlSession=null; sqlSession=sqlSessionFactory.openSession(); //生成代理類 UserMapper userMapper=sqlSession.getMapper(UserMapper.class); User user=userMapper.findUserById(29); System.out.println(user.getUsername()); } }
運行結果:正常。
---------------------------------------------------------------------------------------------------------------------------------------------------------------
擴展一下,我們在查詢時如果查詢到很多條數據,時怎么寫?
1.修改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"> <!-- nanmespace:命名空間。 作用就是對sql進行分類話管理,理解Sal分離 注意:使用mapper代理方式,namespace有特殊重要的作用 --> <mapper namespace="cn.itcast.mybatis.Dao.UserMapper"> <!-- 根據id獲取用戶信息 --> <!-- 在映射文件中配置很多sql語句 --> <!-- id:標識映射文件中的sql; 將sql語句封裝到mappedStatement對象中,所以將id稱為statement的id;parmenterType:指定輸入的參數的類型,這里指定的int型 #{}表示一個占位符號; #{id}:其中的id表示接收輸入的參數,參數名稱就是id,如果輸入參數就是簡單類型,#{}中的參數名可以任意,可以value或其它名稱 resultType:指定的sql輸出結果的所映射的java對象類型,select指定resultType表示將單條記錄映射成的java對象; --> <select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User"> select * from user where id = #{id} </select> <!--where username like '%${value}%' 這句話中必須寫value不然就報錯。因為規則上明確說過 ${}接收輸入參數,類型可以是簡單類型,pojo、hashmap。 如果接收簡單類型,${}中只能寫成value。 --> <select id="findUserByName" parameterType="String" resultType="cn.itcast.mybatis.po.User"> select * from user where username like '%${value}%' </select> </mapper>
2.修改UserMapper.java代碼:
package cn.itcast.mybatis.Dao; import java.util.List; import cn.itcast.mybatis.po.User; public interface UserMapper { //這個名字(findUserById)和UserMapper.xml里面的id要一模一樣 public User findUserById(int id); //注意這里寫的是返回的List.這樣代碼內部就會調用selectList,但是在userMapper.xml中要寫的是 //resultType="cn.itcast.mybatis.po.User" /* * * <select id="findUserByName" parameterType="String" resultType="cn.itcast.mybatis.po.User"> select * from user where username like '%${value}%' </select> * * */ public List<User> findUserByName(String name); }
-------------------------------------------------------------------------------------------------------------------------------------------------------------
最后對mapper開發方式做一個總結:
1. 代理對象內部調用selectOne或selectList
如果mapper方法返回單個pojo對象(非集合對象),代理對象內部通過selectOne查詢數據庫。
如果mapper方法返回集合對象,代理對象內部通過selectList查詢數據庫。
2.mapper接口方法參數只能有一個是否影響系統 開發
mapper接口方法參數只能有一個,系統是否不利於擴展維護。比如
這些方法中只有一個參數,因為:在userMapper.xml中只有一個resultType=“”但是:
系統 框架中,dao層的代碼是被業務層公用的。
即使mapper接口只有一個參數,可以使用包裝類型的pojo滿足不同的業務方法的需求。
注意:持久層方法的參數可以包裝類型、map。。。,service方法中建議不要使用包裝類型(不利於業務層的可擴展)。
