参考官方文档:
mybatis3中文文档
环境:idea+maven+mysql+mybatis。
**1、**下载idea,并配置maven项目管理工具(我使用的是idea自带的maven,配置了阿里云下载以及修改了仓库地址)。【第一次创建好maven项目后,会自动下载plugins。】
**2、**创建好maven项目firstdemo后,会自动下载plugins(插件),使用阿里的下载速度很快。
**3、**删除项目firstdemo中的src文件(原因见下面),并配置依赖。使用xml编写并加入Mybatis,mysql,junit这几个依赖项。
<!--父工程-->
<groupId>org.example</groupId>
<artifactId>firstdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入依赖项-->
<dependencies>
<!--先导入mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</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>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
**4、**右击firstdemo,新建一个Module,命名为mybatis1,即在firstdemo中又建立了一个模块,并且mybatis1可以直接使用firstdemo导入的依赖。即:firstdemo是父工程,后续在它基础上new的Module是子工程,子工程可直接使用父工程导入的依赖。
此时,父工程的xml文件中多了 :<module>mybatis1</module>
<!--父工程-->
<groupId>org.example</groupId>
<artifactId>firstdemo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis1</module>
</modules>
5、根据文档,编写mybatis的配置文件。
在firstdemo–>mybatis1下的resourecs中建立一个file,命名为mybatis-config.xml。
将官方文档中的代码(配置环境等)粘贴进该文件,删除<mappers>
一块(为第十步报错埋伏笔):
<?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="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>
修改其中的参数:
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/majinbuu1?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
测试后,连接成功!
6、编写mybatis工具类:
查看文档:
SqlSession 提供了在数据库执行 SQL 命令所需的所有方法,可以理解为jdbc中的preparedstatment。工厂模式SqlSessionFactory,SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。
开始编写:
在mybatis–>main–>java中新建一个package,并在其中创建一个class:
package com.pht.dao;
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 java.io.IOException;
import java.io.InputStream;
/**
* SqlSessionFactory工厂用于生产SqlSession
*/
public class Utils {
//提升作用域
private static SqlSessionFactory sqlSessionFactory;
//1、获取SqlSessionFactory 对象
//用个静态代码块,一开始就加载
static{
InputStream inputStream = null;
try {
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建SqlSeesion对象
public static SqlSession getsqlseesion(){
return sqlSessionFactory.openSession();
}
}
7、编写实体类
构建数据库字段对应的实体类:
idea快捷键:alt+insert,可以生成构造器,setget方法,以及重写的tostring方法。(一定要加无参构造器!报错吐了)
package com.pht.dao;
public class Myda {
String name_0;
int id_0;
//快捷键:alt+insert
public Myda() {
}
public Myda(String name_0, int id_0) {
this.name_0 = name_0;
this.id_0 = id_0;
}
public String getName_0() {
return name_0;
}
public void setName_0(String name_0) {
this.name_0 = name_0;
}
public int getId_0() {
return id_0;
}
public void setId_0(int id_0) {
this.id_0 = id_0;
}
@Override
public String toString() {
return "Myda{" +
"name_0='" + name_0 + '\'' +
", id_0=" + id_0 +
'}';
}
}
8、编写Mapper(或dao)接口
先在接口中,定义一个方法:
package com.pht.dao;
import java.util.List;
public interface Dao {
List<Myda> getData(); //获得表单
}
9、实现接口类
建立配置文件 XML,以满足 SqlSession 的调用。根据官方文档:
<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
根据实际项目修改namespace和sql语句:(先只执行一条查询语句)(实际代码中不要有中文注释!可能报错)
<?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.pht.dao.Dao">
<!--id为接口中方法的名字,resultType写到具体,因为配置文件不像java类会自动去找关联-->
<select id="getData" resultType="com.pht.dao.Myda">
<!-- 数据库.表单名 -->
select * from majinbuu1.pht_01
</select>
</mapper>
10、测试
在maven中,测试的代码和开发的分别放在test和main下,且包名路径建议相同,养成规范,这里都为com.pht.dao。
测试需要使用,junit
package com.pht.dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class Mydata_test {
@Test
public void test(){
//1、获取session对象
SqlSession obj =Utils.getsqlseesion();
//2、执行sql语句
//获取接口代理对象
Dao dao=obj.getMapper(Dao.class);
List<Myda> data = dao.getData();
for (Myda datum : data) {
System.out.println(datum);
}
//关闭session
obj.close();
}
}
报错(需要记住的一个报错):
org.apache.ibatis.binding.BindingException: Type interface com.pht.dao.Dao is not known to the MapperRegistry.
原因:在第5步中,配置文件我们删除了mappers
一块
每一个mapper.xml文件(即第九步中的xml文件)都需要在mybatis核心配置文件中注册!!!
因此,在核心配置文件中(第五步那里),添加注册信息:
使用斜杠:
<mappers>
<mapper resource="com/pht/dao/Daox.xml"/>
</mappers>
11、资源过滤报错
由于maven的资源过滤,Daox.xml无法被导出或生效的问题。
找不到UserMapper所对应的xml文件解决方法:配置POM.XML的resource把xml也打包到mapper目录下。
maven 配置pom build resource…
<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>
将以上代码加入到pom.xml文件中,刷新或重启下,再运行。
12、查询结果
sheet如下:
执行代码后,查询结果如下:
Myda{name_0=‘luffy’, id_0=1}
Myda{name_0=‘pht’, id_0=2}
Myda{name_0=‘majinbuu’, id_0=3}