Mapper 接口开发方法只需要程序员编写Mapper 接口(相当于Dao 接口),由Mybatis 框架根据接口定义创建接 口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。 Mapper 接口开发需要遵循以下规范:
1、 Mapper.xml文件中的namespace与mapper接口的全限定名相同
2、 Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、 Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
4、 Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
即:
最后我们要获取Mybatis给我们生成的代理对象 ,是用 sqlSession.getMapper 这个方法获取的!!!!
实操:
这是结构
package com.bihu.Dao; import com.bihu.Bean.User; import java.util.List; public interface UserMapper { public List<User> findAll(); }
<?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.bihu.Dao.UserMapper"> <select id="findAll" resultType="User" > <!--这里的User前面有别名--> select * from user </select> </mapper>
<?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="datasourceInfo.properties"></properties> <typeAliases> <typeAlias type="com.bihu.Bean.User" alias="User"></typeAlias> </typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <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/bihu/mapper/UserMapper.xml"></mapper> </mappers> </configuration>
package com.bihu.Service; import com.bihu.Bean.User; import com.bihu.Dao.UserMapper; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class UserService { public static void main(String[] args) throws IOException { InputStream sqlMapConfig = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(sqlMapConfig); SqlSession sqlSession = build.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); //获取动态对象 List<User> all = mapper.findAll(); //执行查询所有方法 System.out.println(all); //打印 } }
下面来个按条件查询操作 带参数的:
其实就是准守最上面4条即可 非常的简单:
首先写接口:
然后对应的namespace确保对应接口,而且参数类型和结果类型(结果类型只算泛型)对应!
确保那几个下面就可以获取动态对象了:
package com.bihu.Service; import com.bihu.Bean.User; import com.bihu.Dao.UserMapper; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class UserService { public static void main(String[] args) throws IOException { InputStream sqlMapConfig = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(sqlMapConfig); SqlSession sqlSession = build.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); //获取动态对象 User user = new User();//模拟有User数据【其实单单查一个ID推荐用Integer即可 但这里没有。】 user.setId(5); List<User> userList = mapper.findId(user); System.out.println(userList); } }
允许:
说白了 其实单单一个Id 直接用Integer 但这里我为了演示参数就没那样做