MyBatis理論之DAO與Mapper。本站提示廣大學習愛好者:(MyBatis理論之DAO與Mapper)文章只能為提供參考,不一定能成為您想要的結果。以下是MyBatis理論之DAO與Mapper正文
MyBatis簡介
MyBatis前身是iBatis,是一個基於Java的數據耐久層/對象關系映照(ORM)框架.
MyBatis是對JDBC的封裝,使開辟人員只需存眷SQL自己,而不需消費過量的精神行止理如注冊驅動、設置參數、創立Connection/Statement、解析成果集等JDBC進程性代碼.MyBatis基於XML/注解的方法設置裝備擺設Statement,履行SQL,並將履行成果映照成Java對象, 年夜年夜下降了數據庫開辟的難度.
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
– MyBatis項目地址/在線文檔.
初識MyBatis
應用MyBatis須要在pom.xml中添加以下依附:
<code class="hljs xml"><dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.3.0</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.36</version> </dependency></code>
Select
設置裝備擺設mybatis/mybatis-configuration.xml
作為MyBatis的全局設置裝備擺設文件,其設置裝備擺設了MyBatis的運轉情況信息(如數據源/mapper文件等).
<code class="hljs xml"><code class="hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E--> <configuration> <environments default="development"> <environment id="development"> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEJDBC%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%2D%2D%3E--> <transactionmanager type="JDBC"> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E6%BA%90%2D%2D%3E--> <datasource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"> <property name="url" value="jdbc:mysql://host:port/db?characterEncoding=utf-8"> <property name="username" value="username"> <property name="password" value="password"> </property></property></property></property></datasource> </transactionmanager></environment> </environments> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDmapper%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6%20%2D%2D%3E--> <mappers> <mapper resource="mybatis/mapper/UserDAO.xml"> </mapper></mappers> </configuration></code></code>
書寫UserDAO(mapper映照)
最為MyBatis最焦點的部門,設置裝備擺設了操作數據庫的SQL語句:
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E--> <mapper namespace="namespace"> <select id="selectUserById" parametertype="java.lang.Integer" resulttype="com.fq.domain.User"> SELECT * FROM user WHERE id = #{id}; </select> <select id="selectUserByName" parametertype="java.lang.String" resulttype="com.fq.domain.User"> SELECT * FROM user WHERE name LIKE '%${value}%'; </select> </mapper></code></code></code>
屬性 描寫
namespace 定名空間,用於隔離SQL語句
parameterType 界說SQL輸出映照類型,MyBatis經由過程OGNL從輸出對象中獲得參數傳入SQL語句.
resultType 界說SQL輸入映照類型,MyBatis將SQL查詢成果的一行記載映照為resultType指定的類型.
mapper映照文件名有UserDAO.xml/UserMapper.xml/User.xml等幾種情勢, 其普通寄存在與mybatis-configuration.xml同級的mapper目次下,因為其重要感化為界說SQL語句與映照關系, 是以普通統稱為mapper映照文件.
界說PO類
PO類重要感化為SQL(輸出/輸入)映照,平日與數據庫表對應:
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java">/** * @author jifang * @since 15/12/31 下晝2:27. */ public class User { private Integer id; private String name; private String password; public User() { } public User(Integer id, String name, String password) { this.id = id; this.name = name; this.password = password; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }</code></code></code></code>
UserDAO(Java對象)
取得SqlSession,履行SQL語句, 獲得映照成果:
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java">/** * @author jifang * @since 16/2/24 下晝6:15. */ public class UserDAO { private SqlSessionFactory factory; @Before public void setUp() throws IOException { String resource = "mybatis/mybatis-configuration.xml"; factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource)); } @Test public void selectUserById() { try (SqlSession session = factory.openSession()) { User user = session.selectOne("namespace.selectUserById", 1); System.out.println(user); } } @Test public void selectUserByName() { try (SqlSession session = factory.openSession()) { List<user> users = session.selectList("namespace.selectUserByName", "student"); for (User user : users) { System.out.println(user); } } } }</user></code></code></code></code></code>
Insert
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><insert id="insertUser" parametertype="com.fq.domain.User"> INSERT INTO user(name, password) VALUES(#{name}, #{password}); </insert></code></code></code></code></code></code>
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java">@Test public void insertUser() { try (SqlSession session = factory.openSession()) { User user = new User(); user.setName("new_name1"); user.setPassword("new_password"); session.insert("namespace.insertUser", user); session.commit(); } }</code></code></code></code></code></code></code>
自增主鍵前往
修正mapper文件,添加,可以將MySQL的自增主鍵(即方才拔出數據時生成的ID)前往:
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><insert id="insertUser" parametertype="com.fq.domain.User"> <selectkey keyproperty="id" order="AFTER" resulttype="java.lang.Integer"> SELECT LAST_INSERT_ID(); </selectkey> INSERT INTO user(name, password) VALUES(#{name}, #{password}); </insert></code></code></code></code></code></code></code></code>
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs">@Test public void insertUser() { try (SqlSession session = factory.openSession()) { System.out.println(session); User user = new User(null, "new_name", "new_password"); session.insert("namespace.insertUser", user); // 須要在commit以後能力取得自增主鍵 session.commit(); System.out.println(user.getId()); } }</code></code></code></code></code></code></code></code></code>
該功效還可以經由過程的useGeneratedKeys/keyProperty兩個屬性協作完成, 詳見MyBatis文檔.
Update
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><update id="updateUserById" parametertype="com.fq.domain.User"> UPDATE user SET name = #{name}, password = #{password} WHERE id = #{id}; </update></code></code></code></code></code></code></code></code></code></code>
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java">@Test public void updateUserById() { try (SqlSession session = factory.openSession(true)) { session.update("namespace.updateUserById", new User(1, "feiqing", "ICy5YqxZB1uWSwcVLSNLcA==")); } }</code></code></code></code></code></code></code></code></code></code></code>
Delete
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><delete id="deleteUserById" parametertype="java.lang.Integer"> DELETE FROM user WHERE id = #{id}; </delete></code></code></code></code></code></code></code></code></code></code></code></code>
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java">@Test public void deleteUserById() { try (SqlSession session = factory.openSession(true)) { session.delete("namespace.deleteUserById", 51615); } }</code></code></code></code></code></code></code></code></code></code></code></code></code>
小結
#{}/${}
#{}: 表現一個占位符號,完成向PreparedStatement占位符中設置值(#{}表現一個占位符?),主動停止Java類型到JDBC類型的轉換(是以#{}可以有用避免SQL注入).#{}可以吸收簡略類型或PO屬性值,假如parameterType傳輸的是單個簡略類型值,#{}花括號中可所以value或其它稱號. ${}: 表現拼接SQL串,經由過程${}可將parameterType內容拼接在SQL中而不停止JDBC類型轉換,${}可以吸收簡略類型或PO屬性值,假如parameterType傳輸的是單個簡略類型值,${}花括號中只能是value.
固然${}不克不及避免SQL注入,但有時${}會異常便利(如order by排序,須要將列名經由過程參數傳入SQL,則用ORDER BY ${column},應用#{}則沒法完成此功效(詳見JDBC基本關於PreparedStatement的評論辯論).
SqlSession
供給操作數據庫的辦法(如:selectOne/selectList).但SqlSession是線程不平安的,是以最好將其界說成部分變量應用.
MyBatis長處(與JDBC比擬)
SQL寫在Java代碼中招致不容易保護, 而MyBatis將SQL寫在mapper中,XML與Java代碼分別. 向SQL語句傳參繁瑣(如:SQL的where前提紛歧,SQL數據類型與Java分歧),MyBatis經由過程parameterType主動將Java對象映照至SQL語句. 成果集解析費事(SQL變更招致解析代碼變更,SQL數據類型與Java分歧),MyBatis經由過程resultType主動將SQL履行成果映照成Java對象.
附: 最好在pom.xml中添加一個日記體系完成(logback/log4j), 如許會在調試法式時打印日記信息,便於查錯, 以logback為例:
pom.xml
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><dependency> <groupid>ch.qos.logback</groupid> logback-classic</artifactid> <version>1.1.2</version> </dependency></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
logback.xml
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><configuration> <property name="logRoot" value="/data/logs"> <property name="pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} - %msg%n"> <appender class="ch.qos.logback.core.ConsoleAppender" name="STDOUT"> <encoder> <pattern>${pattern}</pattern> </encoder> </appender> <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="FILE"> <rollingpolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <filenamepattern>${logRoot}/common-server.%d{yyyy-MM-dd}.log</filenamepattern> <maxhistory>7</maxhistory> </rollingpolicy> <encoder> <pattern>${pattern}</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="STDOUT"> <appender-ref ref="FILE"> </appender-ref></appender-ref></root> </property></property></configuration></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
其他關於MyBatis日記的具體信息可參考MyBatis文檔日記部門.
DAO開辟
應用MyBatis開辟DAO有兩個辦法,原始DAO開辟與Mapper映照DAO開辟.
原始DAO開辟
原始DAO開辟須要開辟人員編寫DAO接口與DAO完成,如依據ID查詢用戶信息:
mapper(同前)
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><select id="selectUserById" parametertype="java.lang.Integer" resulttype="com.fq.domain.User"> SELECT * FROM user WHERE id = #{id}; </select></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
UserDAO接口
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java">/** * @author jifang * @since 16/2/22 上午10:20. */ public interface UserDAO { User selectUserById(Integer id) throws Exception; }</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
UserDAO完成
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java">public class UserDAOImpl implements UserDAO { private SqlSessionFactory factory; public UserDAOImpl(SqlSessionFactory factory) { this.factory = factory; } @Override public User selectUserById(Integer id) throws Exception { SqlSession session = factory.openSession(); User user = session.selectOne("namespace.selectUserById", id); session.close(); return user; } }</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
Client
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs">public class MyBatisClient { @Test public void originalClient() throws Exception { UserDAO dao = new UserDAOImpl(new SqlSessionFactoryBuilder(). build(ClassLoader.getSystemResourceAsStream("mybatis/mybatis-configuration.xml"))); User user = dao.selectUserById(1); System.out.println(user); } }</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
原始DAO開辟中存在的成績:
1) DAO完成辦法體中存在許多進程性代碼.
2) 挪用SqlSession的辦法(select/insert/update)須要指定Statement的id,存在硬編碼,晦氣於代碼保護.
Mapper映照開辟
mapper映照開辟辦法只需編寫DAO接口,MyBatis依據接口界說與mapper文件中的SQL語句靜態創立接話柄現.
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><!--?xml version="1.0" encoding="UTF-8" ?--> <mapper namespace="com.fq.mybatis.UserDAO"> <select id="selectUserById" parametertype="java.lang.Integer" resulttype="com.fq.domain.User"> SELECT * FROM user WHERE id = #{id}; </select> </mapper></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
留意: 此時namespace必需與UserDAO接口的全限制名雷同.
UserDAO接口與後面雷同, 但不再應用UserDAOImpl Client
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java">/** * @author jifang * @since 16/2/22 下晝2:57. */ public class MyBatisClient { private SqlSession session; private SqlSessionFactory factory; @Before public void setUp() { factory = new SqlSessionFactoryBuilder(). build(ClassLoader.getSystemResourceAsStream("mybatis/mybatis-configuration.xml")); session = factory.openSession(); } @Test public void mapperClient() throws Exception { UserDAO dao = session.getMapper(UserDAO.class); User user = dao.selectUserById(1); System.out.println(user); } @After public void tearDown() { session.close(); } }</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
mapper映照開辟辦法須要遵守以下標准:
mapper文件中的namespace與DAO接口的全限制名雷同; mapper文件中的Statement的id與DAO接口辦法名雷同; mapper文件中的Statement的parameterType/resultType與DAO辦法的入參/回參類型雷同.
Mapper映照
mapper映照文件(如UserDAO.xml)重要感化是界說SQL語句(每一個SQL是一個Statement),是MyBatis的焦點.
MyBatis官方推舉應用mapper映照的辦法來開辟DAO,是以我們今後就不再過量引見原始DAO的開辟.
輸出映照
多個形參
傳遞簡略類型後面示例曾經應用過,在此就不再贅述.當須要傳遞多個形參時,不再須要設置parameterType參數:
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><update id="updateUserById"> UPDATE user SET name = #{1}, password = #{2} WHERE id = #{0}; </update></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><code class="hljs lasso">void updateUserById(Integer id, String name, String password) throws Exception;</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
傳入PO
MyBatis應用OGNL表達式解析對象屬性值:
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><code class="hljs lasso"><code class="hljs vbnet"><select id="selectUserByNamePassword" parametertype="com.fq.domain.User" resulttype="com.fq.domain.User"> SELECT * FROM user WHERE name = #{name} AND password = #{password}; </select></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><code class="hljs lasso"><code class="hljs vbnet"><code class="hljs java">User selectUserByNamePassword(User user) throws Exception;</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><code class="hljs lasso"><code class="hljs vbnet"><code class="hljs java"><code class="hljs vbnet"><select id="selectUserByMap" parametertype="java.util.Map" resulttype="com.fq.domain.User"> SELECT * FROM user WHERE name = #{name} AND password = #{password}; </select></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
#{}花括號內對應Map的key.
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><code class="hljs lasso"><code class="hljs vbnet"><code class="hljs java"><code class="hljs vbnet"><code class="hljs vhdl">User selectUserByMap(Map<string, object=""> map) throws Exception;</string,></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
輸入映照
輸入簡略類型
mapper
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><code class="hljs lasso"><code class="hljs vbnet"><code class="hljs java"><code class="hljs vbnet"><code class="hljs vhdl"><code class="hljs vbnet"><select id="selectUserCount" parametertype="java.lang.String" resulttype="java.lang.Integer"> SELECT count(*) FROM user WHERE name LIKE '%${value}%'; </select></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
UserDAO
<code class="hljs xml"><code class="hljs xml"><code class="hljs xml"><code class="hljs java"><code class="hljs java"><code class="hljs applescript"><code class="hljs java"><code class="hljs xml"><code class="hljs cs"><code class="hljs haml"><code class="hljs java"><code class="hljs livecodeserver"><code class="hljs java"><code class="hljs xml"><code class="hljs xml"><code class="hljs vbnet"><code class="hljs java"><code class="hljs java"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs haml"><code class="hljs lasso"><code class="hljs vbnet"><code class="hljs java"><code class="hljs vbnet"><code class="hljs vhdl"><code class="hljs vbnet"><code class="hljs mathematica">Integer selectUserCount(String name) throws Exception;</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
前往簡略類型必需包管查詢成果只要一行記載,終究將第一個字段的值轉換為輸入類型.
輸入PO對象/列表
後面曾經演示過輸入兩品種型(selectUserById/selectUserByName固然其時應用的是原始DAO開辟辦法, 但mapper界說情勢年夜同小異),是以在這兒只做簡略總結:
輸入單個PO對象和輸入PO列表在mapper中界說的resultType是一樣的; 輸入單個PO對象要包管SQL查詢成果為單條數據,其外部應用selectOne辦法挪用; 輸入PO列表表現查詢成果能夠為多條,其外部應用selectList辦法挪用,接口前往值可用List/Set承載.
輸入Map
輸入PO對象完整可以改用Map輸入,字段名作key,字段值作value.
mapper
<code class="hljs vbnet"><select id="selectUserLikeName" resulttype="java.util.Map"> SELECT * FROM user WHERE name LIKE '%${value}%'; </select></code>
UserDAO
<code class="hljs vbnet"><code class="hljs mathematica">List<map<string, object="">> selectUserLikeName(String name) throws Exception;</map<string,></code></code>
resultMap
resultType可將查詢成果映照為PO,但條件是PO屬性名與SQL字段名必需分歧,如紛歧致,則可經由過程resultMap尴尬刁難應映照:
mapper
<code class="hljs vbnet"><code class="hljs mathematica"><code class="hljs applescript"><resultmap id="userMap" type="com.fq.domain.User"> <id column="user_id" property="id"> <result column="user_name" property="name"> <result column="user_password" property="password"> </result></result></id></resultmap> <select id="selectUserByName" parametertype="java.lang.String" resultmap="userMap"> SELECT id user_id, name user_name, password user_password FROM user WHERE name = #{name}; </select></code></code></code>
UserDAO接口同前.