如何获得MyBatis?
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
数据持久化:
Dao层,Service层,Controller层…
帮助程序猿将数据存进数据库中
方便
传统的JDBC代码太复杂了,简化,框架,自动化
不用MyBatis也可以,更容易上手。技术没有高低之分
优点:
最重要的一点:使用的人多!
思路:搭建环境——导入MyBatis——编写代码——测试
1、搭建数据库
CREATE DATABASE `mybatis`;
USE `mybatis`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` INT(20) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES (1,'狂神','123456'),(2,'张三','abcdef'),(3,'李四','987654');
2、新建一个普通的maven项目Mybatis-Study,删除src目录
3、导入maven依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
4、编写mybatis核心配置文件
<?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核心配置文件 -->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="sa123"/>
</dataSource>
</environment>
</environments>
<!-- 每一个xxxmapper.xml都需要在Mybatis核心配置文件中注册!! -->
<mappers>
<mapper resource="com/gx/dao/UserMapper.xml"/>
</mappers>
</configuration>
5、编写mybatis工具类
//sqlSessionFactory-->sqlSession
public class MybatisUtils {
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 命令所需的所有方法。
// 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
6、编写实体类
//实体类
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", 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;
}
}
7、Dao接口
public interface UserMapper {
//查询全部用户
List<User> getUserList();
}
8、接口实现类由原来的UserDaoImpl转变为一个Mapper配置文件
<?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">
<!-- namespace = 绑定一个对应的Mapper接口 -->
<mapper namespace="com.gx.dao.UserMapper">
<!-- select查询语句 -->
<select id="getUserList" resultType="com.gx.pojo.User">
select * from mybatis.user
</select>
</mapper>
注意点:
org.apache.ibatis.binding. BindingException:Type interface com.kuang.dao.UserDao is not known to theMapperRegistry
MapperRegistry是什么?
核心配置文件mybatis-config.xml中注册mappers
<mappers>
<mapper resource="com/gx/dao/UserMapper.xml"/>
mappers>
junit测试
public class UserDaoTest {
@Test
public void test(){
// 第一步:获取sqlSession对象
SqlSession sqlSession= MybatisUtils.getSqlSession();
// 方法一:getMapper 建议使用
UserMapper userDao=sqlSession.getMapper(UserMapper.class);
List<User> userList=userDao.getUserList();
// 方法二: 知道就行
// List userList = sqlSession.selectList("mapper.UserMapper.getUserList");
for (User user: userList){
System.out.println(user);
}
//关闭SqlSession
sqlSession.close();
}
你们可以能会遇到的问题:
1、配置文件没有注册
2、绑定接口错误
3、方法名不对
4、返回类型不对
5、Maven导出资源问题
maven由于他的约定大于配置,我们之后可以能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案:
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>