首先在src目录下建立lib文件夹, 然后导入jar包
然后简历conf文件夹用来存放配置文件
在conf目录下建立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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
加入log4j.xml文件
<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!--#log4j中有5级logger ,#FATAL 0 ,#ERROR 3 ,#WARN 4 ,#INFO 6 ,#DEBUG 7 -->
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!--输出到控制台-->
<!--
<appender name="LOG.Console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
</appender>
-->
<!--将级别为DEBUG的信息输出到控制台-->
<appender name="LOG.DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/debug.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<!--将级别为INFO的信息输出到控制台-->
<appender name="LOG.INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/info.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<!--将级别为WARN的信息输出到控制台-->
<appender name="LOG.WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/warn.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<!--将级别为ERROR的信息输出到控制台-->
<appender name="LOG.ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/error.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!--将级别为FATAL的信息输出到控制台-->
<appender name="LOG.FATAL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="d:/log/fatal.log" />
<param name="MaxFileSize" value="5120KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} [%-5p] %c {%F:%L} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="FATAL" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<!--
<appender name="InitAction" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/dbcon.log"/>
<param name="MaxFileSize" value="5120KB"/>
<param name="MaxFileSize" value="10" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.'log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyy-MM-dd HH:mm:ss} - %m%n"/>
<param name="ConversionPattern" value="%d{DATE} [%-5p] %c {%F:%L} - %m%n"/>
</layout>
</appender>
-->
<!--设置org.logicalcobwebs包的输出级别为INFO-->
<!--
<category name="org.logicalcobwebs">
<priority value="INFO" />
<appender-ref ref="LOG.Console" />
</category>
-->
<root>
<priority value="DEBUG"/>
<!--
<appender-ref ref="LOG.Console" />
-->
<appender-ref ref="LOG.DEBUG" />
<appender-ref ref="LOG.INFO" />
<appender-ref ref="LOG.WARN" />
<appender-ref ref="LOG.ERROR" />
<appender-ref ref="LOG.FATAL" />
</root>
</log4j:configuration>
在数据库总建立mybatis数据库创建tbl_employee数据表
创建对应的Employee对象
在conf下加入EmployeeMapper.xml配置文件
<?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="com.atguigu.mybatis.EmployeeMapper">
<select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee">
select id, last_name lastName, email, gender from tbl_employee where id = #{id}
</select>
</mapper>
創建mybatisTest測試類
@Test
void test() throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try {
Employee employee = openSession.selectOne("selectEmp", 1);
System.out.println(employee);
} finally {
openSession.close();
}
}