💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
通过主键查询 1.UserMapper.java ~~~ //通过主键查询 User findById(int id); ~~~ 2.UserMapper.xml ~~~ <select id="findById" resultType="com.nobb.domain.User" parameterType="int"> select * from t_user where id = #{id} </select> ~~~ 3.测试代码(Test.java) ~~~ package com.test; import com.nobb.domain.User; import com.nobb.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 java.io.IOException; import java.io.InputStream; import java.util.List; public class Test { @org.junit.Test public void testFindAll() { //定义配置文件路径 String resource = "mybatis-config.xml";//不能加 根目录 / InputStream inputStream = null; try {//加载配置文件 inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); }//根据配置文件创建SessionFactory对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获得SqlSession SqlSession session = sqlSessionFactory.openSession(); //获得UserMapper接口的实现类 UserMapper mapper = session.getMapper(UserMapper.class); //调用UserMapper方法,执行xml中定义的SQL语句 // List<User> list = mapper.findAll(); //打印list // System.out.println(list); User u = mapper.findById(15); System.out.println(u); } } ~~~