为什么需要Mybatis:方便;传统的JDBC代码太复杂了,简化程序员编写与数据库实现交互的代码的过程
优点:
- 简单易学
- 灵活
- SQL与代码分离,提高了可维护性
- 提供映射标签,支持对象与数据库的ORM字段关系映射
- 提供对象关系映射标签,支持对象关系组件维护
- 提供XML标签,支持编写动态SQL
第一个Mybatis程序
搭建数据库
CREATE TABLE user (
id INT(20) NOT NULL,
name VARCHAR(30) DEFAULT NULL,
pwd VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (id)
)
在项目中创建一个模块Mybatis01
- 编写Mybatis的核心配置文件mybatis-config.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.Drive"/>
<property name="url" value="jdbc:mysql://localhost:3306/learn?serverTimezone=GMT&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="rooot "/>
<property name="password" value="123456789"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册-->
<mappers>
<!--UserMapper.xml文件放在resource的mapper文件夹中-->
<mapper resource="mapper/UerMapper.xml"/>
</mappers>
</configuration>
- 编写Mybatis工具类
/**
* mybatis的sqlsessionfactory工具类,获取sqlSessionFactory对象
* Created by 罗星
* 2020/11/19 0019 20:16
*/
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
InputStream inputStream = null;
try {
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取Sqlsession实例
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
//设置自动提交
//return sqlSessionFactory.openSession(true);
}
}
- 编写项目代码
- 实体类
public class User {
private int id;
private String name;
private String 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();
}
- 接口实现。由原来的Impl类转换为一个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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.luoqing.dao.UserMapper">
<!--id是需要重写方法的名字-->
<select id="getUserList" resultType="com.luoqing.model.User">
select * from learn.user
</select>
</mapper>
如果需要放在java文件夹中则需要在主pom.xml文件中添加以下配置
<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>
- 测试
public class UserDaoTest {
@Test
public void test() {
//获取SQLSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
try {
//方式一:getMapper
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
//方式二:
//List<User> userList = sqlSession.selectList("com.luoqing.dao.UserDao.getUserList");
for (User user : userList) {
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭SQLSession
sqlSession.close();
}
}
}
可能遇到的问题:
- 配置文件没有注册:所有的Mapper.xml文件都需要在mybatis-config.xml文件中的mappers标签中注册
- 绑定接口错误:Mapper.xml的namespace属性的值需要准确对应接口的路径的名字