1、pom.xml
org.mybatis mybatis mysql mysql-connector-java org.junit.jupiter junit-jupiter-api org.projectlombok lombok ch.qos.logback logback-classic
2、logback.xml
[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n UTF-8
3、建库建表
CREATE DATABASE `mybatis-example`; USE `mybatis-example`; CREATE TABLE `t_emp`( emp_id INT AUTO_INCREMENT, emp_name CHAR(100), emp_salary DOUBLE(10,5), PRIMARY KEY(emp_id) ); INSERT INTO `t_emp`(emp_name,emp_salary) VALUES("tom",200.33); INSERT INTO `t_emp`(emp_name,emp_salary) VALUES("jerry",666.66); INSERT INTO `t_emp`(emp_name,emp_salary) VALUES("andy",777.77);
4、Employee.java
package com.atguigu.mybatis.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Employee { private Integer empId; private String empName; private Double empSalary; }
5、mybatis-config.xml(mybatis的总配置文件)
6、MybatisTest.java
package com.atguigu.mybatis; import com.atguigu.mybatis.mapper.EmpMapper; 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.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; import java.io.InputStream; public class MybatisTest { SqlSessionFactory sqlSessionFactory; SqlSession sqlSession; EmpMapper empMapper; @BeforeEach public void setup() throws IOException { // 获取资源流,读取"mybatis-config.xml"文件 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); // 使用资源流创建SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 使用SqlSessionFactory打开一个Session sqlSession = sqlSessionFactory.openSession(); // 使用Session获取EmpMapper的Mapper对象 empMapper = sqlSession.getMapper(EmpMapper.class); } // 在每个测试用例之后执行的清理方法 @AfterEach public void teardown() { sqlSession.close(); // 关闭SqlSession } @Test public void getEmployeeListTest() { empMapper.getEmployeeList().forEach(System.out::println); } //Employee(empId=1, empName=tom, empSalary=200.33) //Employee(empId=2, empName=jerry, empSalary=666.66) //Employee(empId=3, empName=andy, empSalary=777.77) @Test public void getEmployeeByIdTest() throws IOException { System.out.println(empMapper.getEmployeeById(1)); //Employee(empId=1, empName=tom, empSalary=200.33) } }
D:Javajdk-17binjava.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2libidea_rt.jar=18702:D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2bin" -Dfile.encoding=UTF-8 -classpath "D:developmavenrepositoryorgjunitplatformjunit-platform-launcher1.3.1junit-platform-launcher-1.3.1.jar;D:developmavenrepositoryorgapiguardianapiguardian-api1.0.0apiguardian-api-1.0.0.jar;D:developmavenrepositoryorgjunitplatformjunit-platform-engine1.3.1junit-platform-engine-1.3.1.jar;D:developmavenrepositoryorgjunitplatformjunit-platform-commons1.3.1junit-platform-commons-1.3.1.jar;D:developmavenrepositoryorgopentest4jopentest4j1.1.1opentest4j-1.1.1.jar;D:developmavenrepositoryorgjunitjupiterjunit-jupiter-engine5.3.1junit-jupiter-engine-5.3.1.jar;D:developmavenrepositoryorgjunitjupiterjunit-jupiter-api5.3.1junit-jupiter-api-5.3.1.jar;D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2libidea_rt.jar;D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2pluginsjunitlibjunit5-rt.jar;D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2pluginsjunitlibjunit-rt.jar;F:Idea服务器托管网Projectsworkspacepro-ssmpro18-mybatis-begintargettest-classes;F:IdeaProjectsworkspacepro-ssmpro18-mybatis-begintargetclasses;F:my_javamaven_repositoryorgmybatismybatis3.5.11mybatis-3.5.11.jar;F:my_javamaven_repositorymysqlmysql-connector-java8.0.25mysql-connector-java-8.0.25.jar;F:my_javamaven_repositorycomgoogleprotobufprotobuf-java3.11.4protobuf-java-3.11.4.jar;F:my_javamaven_repositoryorgjunitjupiterjunit-jupiter-api5.3.1junit-jupiter-api-5.3.1.jar;F:my_javamaven_repositoryorgapiguardianapiguardian-api1.0.0apiguardian-api-1.0.0.jar;F:my_javamaven_repositoryorgopentest4jopentest4j1.1.1opentest4j-1.1.1.jar;F:my_javamaven_repositoryorgjunitplatformjunit-platform-commons1.3.1junit-platform-commons-1.3.1.jar;F:my_javamaven_repositoryorgprojectlomboklombok1.18.20lombok-1.18.20.jar;F:my_javamaven_repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;F:my_javamaven_repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;F:my_javamaven_repositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.atguigu.mybatis.MybatisTest,getEmployeeListTest 19:39:19,463 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 19:39:19,463 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 19:39:19,463 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/F:/IdeaProjects/workspace/pro-ssm/pro18-mybatis-begin/target/classes/logback.xml] 19:39:19,537 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 19:39:19,540 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 19:39:19,548 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 19:39:19,575 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG 19:39:19,575 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 19:39:19,576 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.atguigu.mybatis] to DEBUG 19:39:19,576 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 19:39:19,577 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@62230c58 - Registering current configuration as safe fallback point [19:39:19.582] [DEBUG] [main] [org.apache.ibatis.logging.LogFactory] [Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.] [19:39:19.596] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:39:19.596] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:39:19.596] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:39:19.596] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:39:19.682] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Opening JDBC Connection] [19:39:20.235] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [Created connection 1193398802.] [19:39:20.236] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4721d212]] [19:39:20.239] [DEBUG]服务器托管网 [main] [com.atguigu.mybatis.mapper.EmpMapper.getEmployeeList] [==> Preparing: select emp_id empId, emp_name empName, emp_salary empSalary from t_emp] [19:39:20.280] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.getEmployeeList] [==> Parameters: ] [19:39:20.320] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.getEmployeeList] [
D:Javajdk-17binjava.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2libidea_rt.jar=18726:D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2bin" -Dfile.encoding=UTF-8 -classpath "D:developmavenrepositoryorgjunitplatformjunit-platform-launcher1.3.1junit-platform-launcher-1.3.1.jar;D:developmavenrepositoryorgapiguardianapiguardian-api1.0.0apiguardian-api-1.0.0.jar;D:developmavenrepositoryorgjunitplatformjunit-platform-engine1.3.1junit-platform-engine-1.3.1.jar;D:developmavenrepositoryorgjunitplatformjunit-platform-commons1.3.1junit-platform-commons-1.3.1.jar;D:developmavenrepositoryorgopentest4jopentest4j1.1.1opentest4j-1.1.1.jar;D:developmavenrepositoryorgjunitjupiterjunit-jupiter-engine5.3.1junit-jupiter-engine-5.3.1.jar;D:developmavenrepositoryorgjunitjupiterjunit-jupiter-api5.3.1junit-jupiter-api-5.3.1.jar;D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2libidea_rt.jar;D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2pluginsjunitlibjunit5-rt.jar;D:BaiduNetdiskDownloadIntelliJ IDEA 2023.2pluginsjunitlibjunit-rt.jar;F:IdeaProjectsworkspacepro-ssmpro18-mybatis-begintargettest-classes;F:IdeaProjectsworkspacepro-ssmpro18-mybatis-begintargetclasses;F:my_javamaven_repositoryorgmybatismybatis3.5.11mybatis-3.5.11.jar;F:my_javamaven_repositorymysqlmysql-connector-java8.0.25mysql-connector-java-8.0.25.jar;F:my_javamaven_repositorycomgoogleprotobufprotobuf-java3.11.4protobuf-java-3.11.4.jar;F:my_javamaven_repositoryorgjunitjupiterjunit-jupiter-api5.3.1junit-jupiter-api-5.3.1.jar;F:my_javamaven_repositoryorgapiguardianapiguardian-api1.0.0apiguardian-api-1.0.0.jar;F:my_javamaven_repositoryorgopentest4jopentest4j1.1.1opentest4j-1.1.1.jar;F:my_javamaven_repositoryorgjunitplatformjunit-platform-commons1.3.1junit-platform-commons-1.3.1.jar;F:my_javamaven_repositoryorgprojectlomboklombok1.18.20lombok-1.18.20.jar;F:my_javamaven_repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;F:my_javamaven_repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;F:my_javamaven_repositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.atguigu.mybatis.MybatisTest,getEmployeeByIdTest 19:40:00,097 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 19:40:00,097 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 19:40:00,097 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/F:/IdeaProjects/workspace/pro-ssm/pro18-mybatis-begin/target/classes/logback.xml] 19:40:00,180 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 19:40:00,184 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 19:40:00,190 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 19:40:00,218 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG 19:40:00,219 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 19:40:00,220 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.atguigu.mybatis] to DEBUG 19:40:00,220 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 19:40:00,221 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@62230c58 - Registering current configuration as safe fallback point [19:40:00.226] [DEBUG] [main] [org.apache.ibatis.logging.LogFactory] [Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.] [19:40:00.242] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:40:00.242] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:40:00.242] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:40:00.242] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [PooledDataSource forcefully closed/removed all connections.] [19:40:00.316] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Opening JDBC Connection] [19:40:00.869] [DEBUG] [main] [org.apache.ibatis.datasource.pooled.PooledDataSource] [Created connection 1626852381.] [19:40:00.870] [DEBUG] [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction] [Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@60f7cc1d]] [19:40:00.874] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.getEmployeeById] [==> Preparing: select emp_id empId, emp_name empName, emp_salary empSalary from t_emp where emp_id = ?] [19:40:00.918] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.getEmployeeById] [==> Parameters: 1(Integer)] [19:40:00.961] [DEBUG] [main] [com.atguigu.mybatis.mapper.EmpMapper.getEmployeeById] [
7、SLF4J
SLF4J,即 简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统。按照官方的说法,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统。实际上,SLF4J所提供的核心API是一些接口以及一个LoggerFactory的工厂类。
使用SLF4J的时候,不需要在代码中或配置文件中指定你打算使用那个具体的日志系统。如同使用JDBC基本不用考虑具体数据库一样,SLF4J提供了统一的记录日志的接口,只要按照其提供的方法记录即可,最终日志的格式、记录级别、输出方式等通过具体日志系统的配置来实现,因此可以在应用中灵活切换日志系统。
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net