userDao-mapping.xml相当于是UserDao的实现, 同时也将User实体类与数据表User成功关联起来。
再编写一个userDao-mapping.xml (可随便命名):
userDao-mapping.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.dy.dao.UserDao"> <select id="findUserById" resultType="com.dy.entity.User" > select * from user where id = #{id} </select> </mapper>
package com.dy.entity; public class User { private int id; private String name; private String password; private int age; private int deleteFlag; 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 String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getDeleteFlag() { return deleteFlag; } public void setDeleteFlag(int deleteFlag) { this.deleteFlag = deleteFlag; } }
再编写一个UserDao 接口:
UserDao:
package com.dy.dao; import java.util.List; import com.dy.entity.User; public interface UserDao { public void insert(User user); public User findUserById (int userId); public List<User> findAllUsers(); }
http://www.cnblogs.com/dongying/p/4031382.html
//外部调用此方法对mybatis配置文件进行解析 19 public Configuration parse() { 20 if (parsed) { 21 throw new BuilderException("Each XMLConfigBuilder can only be used once."); 22 } 23 parsed = true; 24 //从根节点configuration 25 parseConfiguration(parser.evalNode("/configuration")); 26 return configuration; 27 } 28 29 //此方法就是解析configuration节点下的子节点 30 //由此也可看出,我们在configuration下面能配置的节点为以下10个节点 31 private void parseConfiguration(XNode root) { 32 try { 33 propertiesElement(root.evalNode("properties")); //issue #117 read properties first 34 typeAliasesElement(root.evalNode("typeAliases")); 35 pluginElement(root.evalNode("plugins")); 36 objectFactoryElement(root.evalNode("objectFactory")); 37 objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); 38 settingsElement(root.evalNode("settings")); 39 environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631 40 databaseIdProviderElement(root.evalNode("databaseIdProvider")); 41 typeHandlerElement(root.evalNode("typeHandlers")); 42 mapperElement(root.evalNode("mappers")); 43 } catch (Exception e) { 44 throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); 45 } 46 } 47 }
通过以上源码,我们就能看出,在mybatis的配置文件中:
1. configuration节点为根节点。
2. 在configuration节点之下,我们可以配置10个子节点, 分别为:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。
http://www.cnblogs.com/dongying/p/4031613.html
时间: 2024-10-13 22:44:55