内容简介:我们在使用Mybatis的时候,获取需要执行的SQL语句的时候,都是通过调用xml文件来获取,例如:IUser.xml:IUser.java:
我们在使用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接口规范
- Mapper.xml中的namespace和Mapper.java接口中的类路径相同,即
<mapper namespace="cn.ddnd.www.Dao.User">对应的是cn.ddnd.www.Dao包下的User类。 - Mapper.xml中的
selectID要和Mapper.java接口中的类方法名相同,即<select id="getUser" parameterType="String" resultType="User">的getUser和public User getUser(String email);的getUser方法名对应。 - Mapper.xml中的
parameterType的类型要和Mapper接口中方法的 传入参数类型 相同。 - Mapper.xml中的
resultType的类型要和Mapper接口中方法的 返回参数类型 相同。
实现过程
配置Mapper.xml
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>
复制代码
配置Mapper.java接口
IUser.java:
package cn.ddnd.www.Dao;
import cn.ddnd.www.Entity.User;
public interface IUser {
public User getUser(String email);
}
复制代码
编写Mybatis配置文件
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"); 代理对象方法。
转自:ddnd.cn/2018/11/30/…
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 使用 AJP 方式配置反向代理
- PHP CURL方式使用代理访问网站
- 用轻代理的方式做数据库安全
- (一)Mybatis基本配置,Statement方式,动态代理增删改查
- Zabbix4.0使用SNMP代理方式监控vcenter6.5
- Proxy-Go v8.7 发布,增强 socks5 代理 UDP 功能,DNS 支持 TCP 方式查询!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
设计原本
Frederick P. Brooks, Jr. / InfoQ中文站、王海鹏、高博 / 机械工业出版社 / 2011-1-1 / 55.00元
无论是软件开发、工程还是建筑,有效的设计都是工作的核心。《设计原本:计算机科学巨匠Frederick P. Brooks的思考》将对设计过程进行深入分析,揭示进行有效和优雅设计的方法。 本书包含了多个行业设计者的特别领悟。Frederick P. Brooks, Jr.精确发现了所有设计项目中内在的不变因素,揭示 了进行优秀设计的过程和模式。通过与几十位优秀设计者的对话,以及他自己在几个设计......一起来看看 《设计原本》 这本书的介绍吧!