JAVAWEB(二)mybatis实现对数据库实现更新查询

一.编写UserMapper.xml,添加相应的元素

 
        
        
            
              insert into users(name,age) values(#{name},#{age})
            
            
                update users set name = #{name} , age = #{age} where id = #{id}
            

            
            

            
            
        
  • 编写更新的单元测试程序
/**
         * Created by Administrator on 2017/4/6.
         */
        public class TestCRUD {

            /**
             * insert
             */
            @Test
            public void insert() throws Exception {
                //指定配置文件的路径(类路径)
                String resource = "mybatis-config.xml";
                //加载文件
                InputStream inputStream = Resources.getResourceAsStream(resource);

                //创建会话工厂Builder,相当于连接池
                SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);

                //通过sf开启会话,相当于打开连接。
                SqlSession s = sf.openSession();
                User u = new User();

                u.setName("jerry");
                u.setAge(2);
                s.insert("users.insert", u);
                s.commit();
                s.close();
            }

            /**
             * update
             */
            @Test
            public void update() throws Exception {
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
                SqlSession s = sf.openSession();
                User u = new User();
                u.setId(1);
                u.setName("tomas");
                u.setAge(32);
                s.update("users.update", u);
                s.commit();
                s.close();
            }

            /**
             * selectOne
             */
            @Test
            public void selectOne() throws Exception {
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
                SqlSession s = sf.openSession();
                User user = s.selectOne("users.selectOne",1);
                System.out.println(user.getName());
                s.commit();
                s.close();
            }

            /**
             * selectOne
             */
            @Test
            public void selectAll() throws Exception {
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(inputStream);
                SqlSession s = sf.openSession();
                List users = s.selectList("users.selectAll");
                for(User uu : users){
                    System.out.println(uu.getName() + "," + uu.getAge());
                }
                s.commit();
                s.close();
            }
        }

你可能感兴趣的:(JAVAWEB(二)mybatis实现对数据库实现更新查询)