MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。
免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
可以通过简单的 XML 或注解来配置和映射,Java POJO(Plain Old Java Objects)为数据库中的记录。
1 、Maven依赖
使用maven构建项目,需要引入如下依赖:
junit
mysql
mybatis
mybatis-spring
spring-webmvc
spring-jdbc
在mvnrepository.com网站搜索关键字,可获取对应包的pom.xml文件配置。
2、核心配置文件
沿用官方文件名mybatis-config.xml。
<?xml version="1.0" encoding="UTF-8" ?><!--每一个mapper都要注册到核心配置文件-->
<mappers>
<mapper resource="user-mapping.xml"/>
</mappers>
3、编写工具类
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。
SqlSession包含所有访问库表的方法。
把获取SqlSession的方法封装在工具类中。
public class SqlSessionUtil {
public static SqlSessionFactory sqlSessionFactory;
static{
try{
//按照官方文档来编写。
String resource = “mybatis-config.xml”;
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (Exception e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
4、编写Pojo类
【注意】属性字段名跟要访问的数据表字段名保持一致。防止自动映射失败。
public class User {
private int ID;
private String name;
private String password;
public User() {
}
public User(int ID, String name, String password) {
this.ID = ID;
this.name = name;
this.password = password;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"ID=" + ID +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
5、编写mapper接口
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
http://…
public interface UserMapper {
public List getUserList();
}
6、配置mapping文件
用于管理增删查改sql语句。
<?xml version="1.0" encoding="UTF-8" ?> select * from user; 7、mapper接口与mapping文件绑定与映射mapper接口与mapping文件,通过namespace,id属性进行绑定。
mapper接口 mapping文件
全限定类名 命名空间namespace
方法名 id属性
8、编写测试类
public class UserTest extends TestCase {
@Test
public void test1(){
//从工具类获取SqlSession
SqlSession sqlSession= SqlSessionUtil.getSqlSession();
//获取mapper接口
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
//调用mapper接口方法
List result=userMapper.getUserList();
for(User user:result){
System.out.println(user);
}
}
}
返回结果如下:
9、静态资源过滤问题
Maven是约定大于配置,如果报错找不到xml配置文件,需要在pom.xml文件中增加如下配置,并且刷新Maven生效。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>