思路:搭建环境–>导入Mybatis–>编写代码–>测试
2.1 搭建环境(XML文件不能有中文注释!!!)
2.1.1 创建数据库
2.1.2 创建项目
- 创建一个普通maven项目
- 删除src文件
- 在Pom添加驱动
<!--加载驱动-->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--mybatis驱动-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!--junit驱动-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
- 再创建一个普通maven项目(Module子工程的pom会自动生成父工程的pom驱动)
- 在resource文件里面配置一个mybatis-config.xml文件(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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
//需要记住!useSSL必须关闭(因为没有配置),否则访问数据库时被拦截。从而导致通信链路异常
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
- 编写MybatisUtils工具类
- 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例
- SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
package com.node.utils工具包;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
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;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//获取sqlSessionFactory对象
String resource="mybatis-config.xml";
InputStream inputStream;
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例
//org.apache.ibatis.session.SqlSession 提供了在数据库执行 org.apache.ibatis.jdbc.SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
public static SqlSession getSqlSession(){
SqlSession sqlSession=sqlSessionFactory.openSession();
return sqlSession;
}
}
-
编写代码
- 实体类 (Alt+Insert)
package com.node.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
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 String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
- Dao接口
public interface UserDao {
List<User> getUserList();
}
- 接口实现类(由原来的UserDaolmp转变为一个Mapper配置文件)
配置Dao/Mapper文件(UserMapper.xml中文手册有–>探究已映射的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">
<!--绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.node.dao.UserDao">
<!--select 查询语句-->
<select id="getUserList" resultType="com.node.pojo.User">
select * from mybatis.tables1
</select>
</mapper>
2.1.3测试
- junit测试
- 出现问题
- 解决方法:
- 出现问题
- 解决方法:在核心和副核心的xml文件中添加
<!--在bui1d中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory> src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xm1</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>
这时候需要导入build(导进去后刷新),如果还是报红,则把build注释(注释后再刷新)即可
characterEncoding=UTF-8
serverTimezone=UTC 或 serverTimezone=Asia/Shanghai
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://192.168.43.88:3306/smbms?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
allowPublicKeyRetrieval=true: 允许公钥检索–> 请注意,AllowPublicKeyRetrieval=True 可能允许恶意代理执行MITM攻击以获取明文密码,因此False默认情况下它必须明确启用。实在连接不上就用
useUnicode=true&characterEncoding=UTF8: 指定字符的编码、解码格式(存数据先用UTF-8解码成字节码,然后用GBK编码存在数据库中;取数据则分反之)