💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ import net.youworker.domain.User; import net.youworker.mapper.UserMapper; 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 org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-09 12:24 */ public class MabatisTest { /** * 实例 * @param args */ public static void main(String[] args) { //1.读取配置文件 try { InputStream inputStream = Resources.getResourceAsStream("./config/sqlMapConfig.xml"); System.out.println(inputStream); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream); //3.使用工厂生产SqlSession对象 SqlSession session = factory.openSession(); //4.使用SqlSession创建Dao接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); //5.使用代理对象执行方向 List<User> users = userMapper.findAll(); for (User user : users) { System.out.println(user); } //6.释放资源 session.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 注解方式查询 */ @Test public void testFindAll1(){ //1.读取配置文件 try { InputStream inputStream = Resources.getResourceAsStream("./config/sqlMapConfig.xml"); System.out.println(inputStream); //2.创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream); //3.使用工厂生产SqlSession对象 SqlSession session = factory.openSession(); //4.使用SqlSession创建Dao接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); //5.使用代理对象执行方向 List<User> users = userMapper.findAll1(); for (User user : users) { System.out.println(user); System.out.println(user.getUsername()); } //6.释放资源 session.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ~~~