ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1.UserMapper.java ~~~ //新增,返回影响行数 int save(User u); ~~~ 2.UserMapper.xml ~~~ <!--新增语句--> <insert id="save" parameterType="com.nobb.domain.User"> insert into t_user(name,password) values(#{name},#{password}) </insert> ~~~ 3.Test.java ~~~ 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); User u = new User(); u.setName("nobb1121"); u.setPassword("11211"); int row = mapper.save(u); //注:写入需要显示提交session session.commit(); session.close(); System.out.println(row); } } ~~~