1、JNDI简介:JNDI(Java Naming and Directory Interface)是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通用、统一的接口,类似JDBC都是构建在抽象层上。
2、MyBatis配置JNDI:
MyBatis配置文件:
<?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>
<package name="Model"></package>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<!-- 配置连接数据库的必备信息 type属性表示是否使用数据源(连接池)
property标签中value属性值java:comp/env/ 此为固定写法 ;
jdbc/mybatis 数据源名称(在META-INF中的配置文件中配置的数据)
-->
<dataSource type="JNDI">
<property name="data_source" value="java:comp/env/jdbc/mybatis"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--此处使用package标签配置映射需要注意,接口映射文件与SQL映射文件必须是一一对应的,不然编译会出现异常-->
<package name="myBatis.dao"></package>
<!--<mapper resource="myBatis/dao/JNDIMapper.xml"></mapper>-->
</mappers>
</configuration>
**3、**项目配置中需要特别注意的是,在项目目录webapp下需要创建META-INF文件夹,在该文件夹下放置JNDI数据源需要引用的信息:
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!--
<Resource
name="jdbc/mybatis" 数据源的名称
type="javax.sql.DataSource" 数据源类型
auth="Container" 数据源提供者
maxActive="20" 最大活动数
maxWait="10000" 最大等待时间
maxIdle="5" 最大空闲数
username="root" 用户名
password="1234" 密码
driverClassName="com.mysql.jdbc.Driver" 驱动类
url="jdbc:mysql://localhost:3306/eesy_mybatis" 连接url字符串
/>
-->
<Resource
name="jdbc/mybatis"
type="javax.sql.DataSource"
auth="Container"
maxActive="20"
maxWait="10000"
maxIdle="5"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mybatis"
/>
</Context>
<?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="myBatis.dao.JNDIMapper">
<resultMap id="userAccountJNDI" type="userAcountJNDI">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<collection property="accounts" ofType="account">
<id property="id" column="aid"></id>
<result property="uid" column="UID"></result>
<result property="money" column="MONEY"></result>
</collection>
</resultMap>
<!--账户与用户一一对应的关系:
此处主要是实现一一对应关系的懒加载(延迟加载)
-->
<resultMap id="accountUser" type="accountUsermodel">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!--此处的column属性必须写,属性的值作为select属性中引用的查询sql的参数-->
<association property="user" column="uid" javaType="userCRUDmodeltwo" select="myBatis.dao.IUserCRUDDao.queryUserInfoById"></association>
</resultMap>
<select id="getAllUserInfo" resultMap="userAccountJNDI">
SELECT u.*,
a.id aid,
a.UID,
a.MONEY
FROM user u
left join account a
on a.UID = u.id
</select>
<select id="getAccountUserByLazy" resultMap="accountUser">
select * from account
</select>
</mapper>
5、封装实体类userAcountJNDI.java:
package Model;
import java.util.Date;
import java.util.List;
public class userAcountJNDI {
private Integer id;
private Date birthday;
private char sex;
private String username;
private String address;
private List<account> accounts;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<account> getAccounts() {
return accounts;
}
public void setAccounts(List<account> accounts) {
this.accounts = accounts;
}
@Override
public String toString() {
return "userAcountJNDI{" +
"id=" + id +
", birthday=" + birthday +
", sex=" + sex +
", username='" + username + '\'' +
", address='" + address + '\'' +
", accounts=" + accounts +
'}';
}
}
6、测试此方法需要用到的包:
pom.xml:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<!--此包是在延迟加载配置延迟加载开关后编译报错是缺少该包引入的-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.5</version>
</dependency>
</dependencies>
7、接口映射文件:
package myBatis.dao;
import Model.accountUserModel;
import Model.userAcountJNDI;
import java.util.List;
public interface JNDIMapper {
/**
* 查询所有用户及其账户信息
*/
List<userAcountJNDI> getAllUserInfo();
/**
* 账户与人员信息一一对应的 延迟加载 实现
*/
List<accountUserModel> getAccountUserByLazy();
}
8、jsp文件(index.jsp)
<%@ page import="java.io.InputStream" %>
<%@ page import="org.apache.ibatis.io.Resources" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactoryBuilder" %>
<%@ page import="org.apache.ibatis.session.SqlSessionFactory" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="java.util.List" %>
<%@ page import="myBatis.dao.JNDIMapper" %>
<%@ page import="Model.userAcountJNDI" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<body>
<h2>Hello World!</h2>
<%
//读取mybatis配置文件
InputStream ins = Resources.getResourceAsStream("MyBatis_JNDI.xml");
//创建工厂类
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory ssf = builder.build(ins);
//
//使用工厂类创建SqlSession对象
//SqlSession ss = ssf.openSession(true); //参数true设置自动提交
SqlSession ss = ssf.openSession();
//使用SqlSession创建dao层的动态代理类
JNDIMapper JNDIMap = ss.getMapper(JNDIMapper.class);
//使用代理对象执行方法
List<userAcountJNDI> list = JNDIMap.getAllUserInfo();
for (userAcountJNDI model: list) {
System.out.println(model);
}
//释放资源
ss.close();
ins.close();
%>
</body>
</html>
9、完成上述步骤之后,将项目的部署在tomcat中,启动tomcat服务器,访问项目,即可测试JNDI数据源得使用。