我们在使用Mybatis的时候,获取需要执行的SQL语句的时候,都是通过调用xml文件来获取,例如:User user = (User) sqlSession.selectOne("cn.ddnd.www.Entity.User.getUser", "xue8@qq.com");
。这种方式是通过字符串去调用标签定义的SQL语句,第一容易出错,第二是当xml当中的id修改过后你不知道在程序当中有多少个地方使用了这个id,需要手动一一修改。后来Mybatis推出了Mapper动态代理方式,只需要编写Mapper接口(相当于Dao层),由Mybatis框架根据接口定义创建接口的动态代理对象。
<mapper namespace="cn.ddnd.www.Dao.User">
对应的是cn.ddnd.www.Dao
包下的User
类。select
ID要和Mapper.java接口中的类方法名相同,即<select id="getUser" parameterType="String" resultType="User">
的getUser
和public User getUser(String email);
的getUser
方法名对应。parameterType
的类型要和Mapper接口中方法的传入参数类型相同。resultType
的类型要和Mapper接口中方法的返回参数类型相同。IUser.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="cn.ddnd.www.Dao.IUser">
<select id="getUser" parameterType="String" resultType="User">
select * from user where email = #{email}
</select>
</mapper>
IUser.java:复制代码
package cn.ddnd.www.Dao;
import cn.ddnd.www.Entity.User;
public interface IUser {
public User getUser(String email);
}
Mybatis-config.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>
<typeAliases>
<typeAlias type="cn.ddnd.www.Entity.User" alias="User"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring?serverTimezone=GMT%2B8" />
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/ddnd/www/Dao/IUser.xml"></mapper>
</mappers>
</configuration>
test.java:复制代码
import cn.ddnd.www.Dao.IUser;
import cn.ddnd.www.Entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.apache.ibatis.io.Resources;
import java.io.Reader;
import java.io.IOException;
public class test {
private static Reader reader;
private static SqlSessionFactory sqlSessionFactory;
static{
try{
reader = Resources.getResourceAsReader("Mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch (IOException e){
e.printStackTrace();
}
}
@Test
public void a() throws IOException {
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
IUser IUser = (IUser) sqlSession.getMapper(IUser.class);
User user = IUser.getUser("xue8@qq.com");
System.out.println("用户的邮箱是:" + user.getEmail() + ",用户的名称是:" + user.getName() + ",用户的密码是:" + user.getPassword());
}finally {
sqlSession.close();
}
}
}
IUser IUser = (IUser) sqlSession.getMapper(IUser.class);
sqlSession会帮我们生成一个实现类(给IUser接口),这样即可获取IUser接口的代理对象。User user = IUser.getUser("xue8@qq.com");
代理对象方法。