传统方式就是在项目下边建立dao
包,里面包含接口及其实现类,文件结构如下:
UserMapper.java
package com.example.demo.dao;
import com.example.demo.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
List<User> findAll() throws IOException;
}
UserMapperImpl.java
package com.example.demo.dao.impl;
import com.example.demo.dao.UserMapper;
import com.example.demo.domain.User;
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;
import java.util.List;
public class UserMapperImpl implements UserMapper {
@Override
public List<User> findAll() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> userList = sqlSession.selectList("userMapper.findAll");
sqlSession.close();
return userList;
}
}
测试其实现:
package com.example.demo.service;
import com.example.demo.dao.UserMapper;
import com.example.demo.domain.User;
import java.io.IOException;
import java.util.List;
public class ServiceDemo {
public static void main(String[] args) throws IOException {
// 创建dao层对象,目前dao层是手动编写的
UserMapper userMapper = new UserMapperImpl();
List<User> all = userMapper.findAll();
System.out.println(all);
}
}
其实这里边就是用MyBatis提供的API调用MyBatis配置,这个内容在刚开始学MyBatis的时候有提到过:【Spring集成MyBatis】MyBatis诞生及代码快速入门(非注解开发)
在上面的代码中,其实很多代码都是重复的,比如获取sqlSession的代码:
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
而sql语句的代码又是半重复的,只有括号里面的内容会发生变化:
List<User> userList = sqlSession.selectList("userMapper.findAll");
在这里介绍MyBatis的代理开发方式,其需要遵循一定的规范
即如下的对应部分应该相同:
使用以上介绍的开发方式,我们就不需要再写接口的实现类了,只需要让接口与XML文件中的定义对应即可。
此时,我的XML文件和接口分别为:
UserMapper.xml
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.UserMapper">
<select id="findAll" resultType="user">
select * from user
select>
mapper>
UserMapper.java
package com.example.demo.dao;
import com.example.demo.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
List<User> findAll() throws IOException;
}
测试的时候,只需要通过sqlSession.getMapper()
来获得对应类的实现配置即可
package com.example.demo.service;
import com.example.demo.dao.UserMapper;
import com.example.demo.domain.User;
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;
import java.util.List;
public class ServiceDemo {
public static void main(String[] args) throws IOException {
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.findAll();
System.out.println(userList);
}
}
<select id="findById" parameterType="int" resultType="user">
select * from user where id=#{id}
select>
接口中加上:
User findById(int id);
实现时调用:
User user = userMapper.findById(1);
即可获得对应的user结果