工程目录结构
创建maven项目,在pom.xml中导入依赖坐标
org.mybatis mybatis 3.4.5mysql mysql-connector-java 5.1.6log4j log4j 1.2.12junit junit 4.10
创建Mybatis全局配置文件SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
创建日志文件log4j.properties.xml
# Set root category priority to INFO and its only appender to CONSOLE.#log4j.rootCategory=INFO, CONSOLE debug info warn error fatallog4j.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.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m# LOGFILE is set to be a File appender using a PatternLayout.log4j.appender.LOGFILE=org.apache.log4j.FileAppender#log4j.appender.LOGFILE.File=d:logaxis.loglog4j.appender.LOGFILE.File=d:logsaxis.loglog4j.appender.LOGFILE.Append=truelog4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m
创建User实体类
package com.bigjava18.domain;import java.io.Serializable;import java.util.Date;/** * 用户的实体类 */public class User implements Serializable { private Integer id; //用户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 + ''' + '}'; }}
创建Dao接口
package com.bigjava18.dao;import com.bigjava18.domain.User;import java.util.List;/** * 用户持久层接口 */public interface IUserDao { /** * 查询所有的用户 * @return */ public List findAll();}
创建IUserDao.xml文件
<?xml version="1.0" encoding="UTF-8"?> select * from user
测试Mybatis运行结果
package com.bigjava18.test;import com.bigjava18.dao.IUserDao;import com.bigjava18.domain.User;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.InputStream;import java.util.List;/** * mybatis入门的案例 */public class MybatisTest { /** * 入门的案例 * @param args */ public static void main(String[] args) throws Exception{ //1.读取配置文件 InputStream inputStream= Resources.getResourceAsStream("SqlMapConfig.xml"); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); SqlSessionFactory sessionFactory=sqlSessionFactoryBuilder.build(inputStream); //3.使用工厂生产SqlSession对象 SqlSession session = sessionFactory.openSession(); //4.使用SqlSession创建Dao接口的代理对象 IUserDao userDao = session.getMapper(IUserDao.class); //5.使用代理对象执行方法 List users = userDao.findAll(); //6.循环输出查询数据 for(User user : users){ System.out.println(user); } //7.释放资源 session.close(); inputStream.close(); }}
运行结果
创作不易,希望大家多多关注,将会更新更多IT小技巧