初始Mybatis项目
1. 建立一个普通的Maven项目
2. 在pom.xml中导入基础的依赖
mybatis依赖
mysql依赖
junit依赖
lombok依赖
log4j依赖
前三者必须,有这是建立Mybatis的基础,后两者是在开发上更方便
另外需要设置一个过滤器,防止资源导出失败!
<dependencies>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
</dependencies>
<!--防止资源导出失败的问题,设置过滤器-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3. 在资源路径下编写mybatis-config.xml,配置核心文件
其中mapper标签是注册是时在写入的,一个mapper只对应一个项目
<?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"></transactionManager>
<dataSource type="POLLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///school?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com.wang.dao.StuMapper.xml"></mapper>
</mappers>
</configuration>
4. 在src/main/java/ 建包 分别为dao包,pojo包,untils包
- dao包写***Mapper接口和***Mapper.xml
- pojo写实体类
- untils写工具类
先写工具类
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//使用Mybatis第一步:获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们就可以从中获得 SqlSession 的实例了。
// SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
写pojo(实体类)
package com.wang.pojo;
/**
* @author Charlie Puth
* @version 1.0
**/
public class Student {
private int id;
private String name;
private int tid;
public Student() {
}
public Student(int id, String name, int tid) {
this.id = id;
this.name = name;
this.tid = tid;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", tid=" + tid +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
}
写***Mapper接口
public interface StuMapper {
List<Student> getStudent();
}
写***Mapper.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.wang.dao.StuMapper">
<select id="getStudent" parameterType="com.wang.pojo.Student" resultType="com.wang.pojo.Student">
select * from student;
</select>
</mapper>
5.连接数据库
6.测试
- 调用工具类实现SqlSession的使用
- 解析***Mapper
- 中间实现具体代码
- 关闭资源(勿忘)
@Test
public void test1(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
StuMapper mapper = sqlSession.getMapper(StuMapper.class);
List<Student> student = mapper.getStudent();
for (Student students : student) {
System.out.println(students);
}
sqlSession.close();
}