1. pom文件添加依赖
<dependencies>
<!--MyBatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--junit测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--日志包,方便看sql语句-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
2. 创建核心配置文件SqlMapConfig.xml
<property name="url" value="jdbc:mysql://localhost:3306/mytestdb"/>
报错:
org.apache.ibatis.exceptions.PersistenceException
注意: 在用8.0版本的jdbc链接数据库jar包时,对于url的设置要加上时区,要用分号分割,在xml文件中,分号;要用&表示,代码示例:jdbc:mysql://localhost:3306/XXXXX?useSSL=false&serverTimezone=GMT
引用自:https://blog.csdn.net/qq_25634457/article/details/82786903评论区
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mytestdb?useSSL=false&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zzz/pojo/DeptMapper.xml"/> <!--这里要与自己的映射文件名和路径一致-->
</mappers>
</configuration>
3. 创建实体类
package com.zzz.pojo;
public class Dept {
//属性名跟mysql表中的列名完全相同
private Integer deptno;
private String dname;
private String loc;
public Integer getDeptno() {
return deptno;
}
public String getDname() {
return dname;
}
public String getLoc() {
return loc;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public void setDname(String dname) {
this.dname = dname;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Dept() {
}
public Dept(Integer deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
}
4. 创建映射配置文件DeptMapper.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.zzz.mapper.DeptMapper">
<select id="selectAllDept" resultType="com.zzz.pojo.Dept">
select * from dept;
</select>
</mapper>
注:正常该文件应该放在resources文件夹下,这里为了方便和实体类放在了同一个文件夹下,要在pom文件中添加如下依赖:
<!--maven默认编译resources下的文件,添加build,里面有的路径下的xml文件都要编译-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
5. junit测试
package com.zzz.test;
import com.zzz.pojo.Dept;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLOutput;
import java.util.List;
public class MyBatisTest {
@Test
public void test1() throws IOException {
// 加载配置文件
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(resource);
SqlSession session = factory.openSession();
List<Dept> list = session.selectList("selectAllDept");
for(Dept dept:list){
System.out.println(dept.getDeptno() + ";" + dept.getDname() + ";" + dept.getLoc());
}
session.close();
}
}
数据库表:
测试结果输出:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
10;accounting;new york
20;research;dallas
30;oprations;boston
40;sales;chicago
6. 添加log4j日志文件
在resources下创建文件log4j.properties,添加如下内容:
参考:
https://www.cnblogs.com/sharpest/p/10293737.html
# Global logging configuration 开发时候建议使用 debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
之后运行程序会打印日志文件:
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 306980751.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@124c278f]
DEBUG [main] - ==> Preparing: select * from dept;
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 4
10;accounting;new york
20;research;dallas
30;oprations;boston
40;sales;chicago
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@124c278f]
DEBUG [main] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@124c278f]
DEBUG [main] - Returned connection 306980751 to pool.