MyBatis基础入门《一》环境搭建

MyBatis基础入门《一》环境搭建

  参考资料链接:http://www.mybatis.org/mybatis-3/

  使用maven构建项目,STS开发工具,jdk1.8

  

项目结构:

  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> <!-- 引入database.properties文件 -->
<properties resource="properties/database.properties"></properties> <!-- 配置mybatis的log实现log4j -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings> <!-- 配置Mybatis的环境 -->
<environments default="development">
<environment id="development">
<!-- 配置事物管理 -->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
</configuration>

database.properties

  jdbc.driver=com.mysql.jdbc.Driver

  jdbc.url=jdbc:mysql://填写数据库的IP地址:3306/test
  jdbc.username=charles
  jdbc.password=charles

log4j.properties

  MyBatis基础入门《一》环境搭建

### set log levels ###
log4j.rootLogger = debug,stdout,D

### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d(%r) --> [%t] %l: %m %x %n

### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
## 输出DEBUG级别以上的日志
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

### 保存异常信息到单独文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
## 异常日志文件名
log4j.appender.D.File = logs/error.log
log4j.appender.D.Append = true
## 只输出ERROR级别以上的日志!!!
log4j.appender.D.Threshold = ERROR
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

JunitMybatisConfig.java

 package com.charles.junit;

 import static org.junit.Assert.fail;

 import java.io.IOException;
import java.io.InputStream; 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 org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test; public class JunitMybatisConfig { @Test
public void test() {
fail("Not yet implemented");
} @Test
public void testLog4j() {
PropertyConfigurator.configure("D:/DISK WORKSPACE/STS MAVEN/mybatis-base/src/main/resources/lo4j.properties");
Logger logger = Logger.getLogger(JunitMybatisConfig.class);
logger.debug(" debug ");
logger.error(" error ");
} @Test
public void testMybaits() { try {
/** 1.获取mybatis-config.xml文件 **/
String resource = "mybatis/mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource); /** 2.创建SQLSessionFactory对象 **/
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); /** 3.创建SQLSession **/
SqlSession session = sqlSessionFactory.openSession(); /** 4.输出SQLSession对象 **/
System.out.println(session);
} catch (IOException e) {
e.printStackTrace();
}
}
}

pom.xml

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.charles.mybatis</groupId>
<artifactId>mybatis-base</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies> </project>

如有问题,欢迎纠正!!!

如有转载,请标明源处:https://www.cnblogs.com/Charles-Yuan/p/9859617.html

上一篇:安装MariaDB和简单配置


下一篇:学习html5的WebSocket连接