Mybatis框架(八):Mybatis的JNDI详解

一、JNDI的简介

JNDI(Java Naming and Directory Interface,Java命名和目录接口)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访问提供者接口JNDI服务供应接口(SPI)的实现,由管理者将JNDI API映射为特定的命名服务和目录系统,使得Java应用程序可以和这些命名服务和目录服务之间进行交互。

二、mybatis中JNDI的使用

1、创建数据库表

参考《mybatis入门》:https://blog.csdn.net/yu1755128147/article/details/103358209

2、创建maven的web工程并导入坐标

Mybatis框架(八):Mybatis的JNDI详解_第1张图片

参考《idea开发maven项目》:https://blog.csdn.net/yu1755128147/article/details/103222724

3、创建实体类、持久层dao接口

实体类User

package com.wedu.mybatis11.domain;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户实体
 */
public class User implements Serializable {

    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

持久层接口IUserDao

package com.wedu.mybatis11.dao;

import com.wedu.mybatis11.domain.User;

import java.util.List;

/**
 * 用户持久层接口
 */
public interface IUserDao {

    /**
     * 查询所有用户
     * @return
     */
    List findAll();

}

4、在webapp下创建META-INF目录并在其中创建context.xml





5、创建日志文件:log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=E:\\project\\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

6、创建配置文件:SqlMapConfig.xml




    
    
        
        
            
            
            
            
                
                
            
        
    

    
    
        
    

7、创建映射文件:IUserDao.xml






    
    

8、启动tomcat,访问:http://localhost:8080/mybatis_jndi/

Mybatis框架(八):Mybatis的JNDI详解_第2张图片

你可能感兴趣的:(mybatis)