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

2018-07-03  本文已影响10人  文子轩

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

 <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        <mapper namespace="users">
            <insert id="insert">
              insert into users(name,age) values(#{name},#{age})
            </insert>
            <update id="update">
                update users set name = #{name} , age = #{age} where id = #{id}
            </update>

            <!-- selectOne -->
            <select id="selectOne" parameterType="int" resultType="com.it18zhang.mybatisdemo.domain.User">
                select * from users where id = #{id}
            </select>

            <!-- selectAll -->
            <select id="selectAll" resultType="com.it18zhang.mybatisdemo.domain.User">
                select * from users
            </select>
        </mapper>
/**
         * 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<User> users = s.selectList("users.selectAll");
                for(User uu : users){
                    System.out.println(uu.getName() + "," + uu.getAge());
                }
                s.commit();
                s.close();
            }
        }
上一篇下一篇

猜你喜欢

热点阅读