🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ public class MybatisFirst { // 会话工厂 private SqlSessionFactory sqlSessionFactory; @Before public void createSqlSessionFactory() throws Exception{ // 配置文件 String resource = "sqlMapConfig.xml"; InputStream resourceAsStream = Resources.getResourceAsStream(resource); // 从配置文件中创建SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); } @Test public void testFindUserById(){ // 会话 SqlSession sqlSession = sqlSessionFactory.openSession(); // 查询单条记录 User user = sqlSession.selectOne("test.findUserById", 1); System.out.println(user.toString()); } @Test public void testFindUserByUsername(){ // 会话 SqlSession sqlSession = sqlSessionFactory.openSession(); // 查询多条记录 List<User> list = sqlSession.selectList("test.findUserByUsername", "bb"); System.out.println(list); } @Test public void testInserUser(){ // 会话 SqlSession sqlSession = sqlSessionFactory.openSession(); // 插入 User user = new User(); user.setUsername("test"); user.setBirthday(new Date(System.currentTimeMillis())); user.setSex("1"); user.setAddress("111"); // 成功返回1 int insert = sqlSession.insert("test.insertUser", user); System.out.println(insert); // 提交事务 sqlSession.commit(); } @Test public void testDeleteUserById(){ // 会话 SqlSession sqlSession = sqlSessionFactory.openSession(); // 查询多条记录 int result = sqlSession.delete("test.deleteUserById", 1); System.out.println(result); sqlSession.commit(); } @Test public void testUpdate(){ // 会话 SqlSession sqlSession = sqlSessionFactory.openSession(); // 更新 User user = new User(); user.setId(8); user.setUsername("test1111"); user.setBirthday(new Date(System.currentTimeMillis())); user.setSex("1"); user.setAddress("11122222"); int result = sqlSession.update("test.updateUser", user); System.out.println(result); sqlSession.commit(); } } ~~~