MyBatis基础入门《一》环境搭建
MyBatis基础入门《一》环境搭建
参考资料链接:http://www.mybatis.org/mybatis-3/
使用maven构建项目,STS开发工具,jdk1.8
项目结构:
mybatis-config.xml文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 6 <configuration> 7 8 <!-- 引入database.properties文件 --> 9 <properties resource="properties/database.properties"></properties> 10 11 <!-- 配置mybatis的log实现log4j --> 12 <settings> 13 <setting name="logImpl" value="LOG4J" /> 14 </settings> 15 16 <!-- 配置Mybatis的环境 --> 17 <environments default="development"> 18 <environment id="development"> 19 <!-- 配置事物管理 --> 20 <transactionManager type="JDBC" /> 21 <dataSource type="POOLED"> 22 <property name="driver" value="${jdbc.driver}" /> 23 <property name="url" value="${jdbc.url}" /> 24 <property name="username" value="${jdbc.username}" /> 25 <property name="password" value="${jdbc.password}" /> 26 </dataSource> 27 </environment> 28 </environments> 29 </configuration>
database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://填写数据库的IP地址:3306/test
jdbc.username=charles
jdbc.password=charles
log4j.properties
### 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
1 package com.charles.junit; 2 3 import static org.junit.Assert.fail; 4 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 import org.apache.ibatis.io.Resources; 9 import org.apache.ibatis.session.SqlSession; 10 import org.apache.ibatis.session.SqlSessionFactory; 11 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 12 import org.apache.log4j.Logger; 13 import org.apache.log4j.PropertyConfigurator; 14 import org.junit.Test; 15 16 public class JunitMybatisConfig { 17 18 @Test 19 public void test() { 20 fail("Not yet implemented"); 21 } 22 23 @Test 24 public void testLog4j() { 25 PropertyConfigurator.configure("D:/DISK WORKSPACE/STS MAVEN/mybatis-base/src/main/resources/lo4j.properties"); 26 Logger logger = Logger.getLogger(JunitMybatisConfig.class); 27 logger.debug(" debug "); 28 logger.error(" error "); 29 } 30 31 @Test 32 public void testMybaits() { 33 34 try { 35 /** 1.获取mybatis-config.xml文件 **/ 36 String resource = "mybatis/mybatis-config.xml"; 37 InputStream is = Resources.getResourceAsStream(resource); 38 39 /** 2.创建SQLSessionFactory对象 **/ 40 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); 41 42 /** 3.创建SQLSession **/ 43 SqlSession session = sqlSessionFactory.openSession(); 44 45 /** 4.输出SQLSession对象 **/ 46 System.out.println(session); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 } 51 }
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.charles.mybatis</groupId> 5 <artifactId>mybatis-base</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 8 <dependencies> 9 <dependency> 10 <groupId>junit</groupId> 11 <artifactId>junit</artifactId> 12 <version>4.11</version> 13 </dependency> 14 <dependency> 15 <groupId>log4j</groupId> 16 <artifactId>log4j</artifactId> 17 <version>1.2.17</version> 18 </dependency> 19 <dependency> 20 <groupId>org.mybatis</groupId> 21 <artifactId>mybatis</artifactId> 22 <version>3.4.6</version> 23 </dependency> 24 </dependencies> 25 26 </project>
如有问题,欢迎纠正!!!
如有转载,请标明源处:https://www.cnblogs.com/Charles-Yuan/p/9859617.html