使用Mybatis 开发Web 工程时,通过Mapper 动态代理机制,可以只编写接口以及方法的定义。

如下:

定义db.properties

driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=tiger

定义SqlMapConfig.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>
<!--引入外部 db.properties-->
<properties resource="db.properties"/> <!--配置Oracle 数据库信息-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mapper/UserInfo.xml"/>
<mapper resource="com/mapper/BatchCustomerOneToOne.xml"/>
<mapper resource="com/mapper/BatchCustomerOneToMany.xml"/>
<mapper resource="com/mapper/BatchCustomerManyToMany.xml"/>
<mapper resource="com/mapper/DelayedLoading.xml"/>
<mapper resource="com/service/impl/BatchCustomerMapper.xml"/>
</mappers>
</configuration>

定义一个Mapper 接口:

package com.service.impl;

import com.entity.onetoonebyresultMap.Customer;

/**
* @author 王立朝
* @version 1.0
* @description Mapper 动态代理类
* * @date 2019/1/24
**/
public interface BatchCustomerMapper {
Customer findOneCustomerById(Integer integer);
}

定义Customer 实体类

package com.entity.onetoonebyresultMap;

/**
* @author 王立朝
* @version 1.0
* @description com.entity.onetoonebyresultMap
* @date 2019/1/19
**/
public class Customer {
//用户id
private Integer cusId;
//用户名
private String username ;
//卡号
private String acno ;
//性别
private String gender ;
//联系方式
private String phone ; @Override
public String toString() {
return "Customer{" +
"cusId=" + cusId +
", username='" + username + '\'' +
", acno='" + acno + '\'' +
", gender='" + gender + '\'' +
", phone='" + phone + '\'' +
'}';
} public Customer() {
} public Integer getCusId() { return cusId;
} public void setCusId(Integer cusId) {
this.cusId = cusId;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getAcno() {
return acno;
} public void setAcno(String acno) {
this.acno = acno;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public Customer(Integer cusId, String username, String acno, String gender, String phone) { this.cusId = cusId;
this.username = username;
this.acno = acno;
this.gender = gender;
this.phone = phone;
}
}

定义BatchCustomerMapper.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="com.service.impl.BatchCustomerMapper"> <select id="findOneCustomerById" parameterType="java.lang.Integer"
resultType="com.entity.onetoonebyresultMap.Customer">
select * from customer where cus_id = 4
</select> </mapper>

编写获取SqlSession 会话的工具类  DataConnection.java

package com.util;

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; /**
* @author 王立朝
* @version 1.0
* @description 获取 SqlSession 会话对象
* @date 2019/1/13
**/
public class DataConnection { //mybatis 配置文件
private String resources = "SqlMapConfig.xml";
private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession; public SqlSession getSqlSession() {
try {
InputStream inputStream = Resources.getResourceAsStream(resources);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
System.out.println("获得连接");
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
} public static void main(String[] args) {
DataConnection dataConnection = new DataConnection();
dataConnection.getSqlSession();
}
}

编写单元测试 testBatchCustomerMapper.java

import com.entity.onetoonebyresultMap.Customer;
import com.service.impl.BatchCustomerMapper;
import com.util.DataConnection;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test; /**
* @author 王立朝
* @version 1.0
* @description PACKAGE_NAME
* @date 2019/1/24
**/
public class testBatchCustomerMapper { private static DataConnection dataConnection = new DataConnection(); //测试Mapper 动态代理
@Test
public void testMapper(){ SqlSession sqlSession = dataConnection.getSqlSession(); BatchCustomerMapper batchCustomerMapper = sqlSession.getMapper(BatchCustomerMapper.class);
Customer customer = batchCustomerMapper.findOneCustomerById(4);
System.out.println("用户信息为:"+ customer.getUsername()
+" 性别为:"+ customer.getGender());
} }

测试结果为:

最新文章

  1. iOS开发之&quot;省市&quot;二级联动的数据组织(PHP版)以及PickerView的实现与封装
  2. android 开发项目笔记1
  3. 未能找到类型或命名空间名称“Coco”(是否缺少 using 指令或程序集引用)
  4. nyoj 667 碟战 最小割(最大流)
  5. bnuoj 20834 Excessive Space Remover(水水)
  6. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )
  7. sqlserver关联更新问题
  8. JS引擎线程的执行过程的三个阶段(一)
  9. ConcurrentHashMap源码理解
  10. sql业务分割
  11. vue 图片加载失败调用
  12. Linux命令:dirs
  13. day20 二十、加密模块、操作配置文件、操作shell命令、xml模块
  14. (TIP 2018)Technology details of FFDNet
  15. zabbix使用微信报警(四)
  16. The &#39;INFORMATION_SCHEMA.GLOBAL_STATUS&#39; feature is disabled; see the documentation for &#39;show_compatibility_56&#39;
  17. 学习笔记:Rick&#39;s RoTs -- Rules of Thumb for MySQL
  18. Revit开发小技巧——撤销操作
  19. 奇怪的bug:javascript不执行
  20. &quot;Hello World&quot;团队召开的第三周第七次会议

热门文章

  1. ch6-定制数据对象(打包代码和数据)
  2. oracle非归档模式下的冷备份和恢复
  3. pl/sql编程2-综合
  4. 虚拟机 搭建LVS + DR + keepalived 高可用负载均衡
  5. Windows下重置Mysql密码
  6. php遍历文件夹下的所有文件及文件夹
  7. C++预处理和头文件保护符
  8. 移动前端开发viewport
  9. 关于wcf三大工具的使用(wsdl.exe svcutil.exe disco.exe)
  10. 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划