• [Mybatis学习] 2.接口代理实现的数据库操作


    回顾-

      

    丰富接口实现更多的操作

    public interface IUserDao {
        public List<User> findALL();
        public List<User> findById(int id);
        public List<User> findByName(String username);
        public void saveUser(User u);
        public void updateUser(User u);
        public void deleteUser(int id);
        public List<User> findByQueryBean(QueryBean queryBean);
    
    }

    现在我们一下子添加了对数据的增删改和各种的查询操作

    对应的Mapper.xml文件也要增加相应的条目

    <mapper namespace="mybatis.CRUD.dao.IUserDao">
        <select id="findALL" resultType="mybatis.CRUD.bean.User">
            SELECT * FROM user;
        </select>
        <select id="findById" resultType="mybatis.CRUD.bean.User" parameterType="int">
            SELECT * FROM user WHERE id=#{id} ;
        </select>
        <select id="findByName" resultType="mybatis.CRUD.bean.User" parameterType="java.lang.String">
            SELECT * FROM USER WHERE username  LIKE #{username};
        </select>
        <select id="findByQueryBean" parameterType="mybatis.CRUD.bean.QueryBean" resultType="mybatis.CRUD.bean.User">
            sELECT * FROM user WHERE username LIKE #{user.username};
        </select>
        <insert id="saveUser" parameterType="mybatis.CRUD.bean.User">
            <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
                SELECT last_insert_id();
            </selectKey>
            INSERT INTO user(username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address});
        </insert>
        <update id="updateUser" parameterType="mybatis.CRUD.bean.User">
            UPDATE user SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id};
        </update>
        <delete id="deleteUser" parameterType="int">
            DELETE FROM user WHERE id=#{id};
        </delete>
    </mapper>

     另外Mybatis中Sql语句的拼接中有‘#{}’与‘${}’两种形式,其实这两个都是老朋友了

    #{}:使用的是PreparedStatement提交sql语句,#{}中的数据就是原来的?对应的位置。

    ${}:使用的是Statement提交sql语句,${}中的数据会直接拼接进sql语句中。

    最后编写测试类测试代码

    public class MybatisTest {
        InputStream in;
        SqlSession session;
        IUserDao userDao;
        @Before
        public void init()throws Exception{
            in= Resources.getResourceAsStream("SqlMapConfig.xml");
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            SqlSessionFactory factory=builder.build(in);
            session=factory.openSession();
            userDao=session.getMapper(IUserDao.class);
    
        }
    
        @After
        public void destory() throws IOException {
            session.close();
            in.close();
        }
        @Test
        public void testFindAll() throws Exception {
            List<User> users=userDao.findALL();
            for(User user:users) {
                System.out.println(user.toString());
            }
        }
        @Test
        public void testSaveUser()throws Exception{
            User u=new User();
            u.setUsername("XX");
            u.setSex("男");
            u.setAddress("XX");
            u.setBirthday(new Date());
            System.out.println("保存数据之前的"+u.toString());
            userDao.saveUser(u);
            System.out.println("保存数据之后的"+u.toString());
            System.out.println("获得id:"+u.getId());
            session.commit();
        }
    
        @Test
        public void testUpdateUser()throws Exception{
            User u=new User();
            u.setId(51);
            u.setUsername("XXXXX");
            u.setSex("男");
            u.setAddress("XXXXX");
            u.setBirthday(new Date());
            userDao.updateUser(u);
            session.commit();
        }
    
        @Test
        public void testDeleteUser()throws Exception{
            userDao.deleteUser(48);
            session.commit();
        }
        @Test
        public void testFindById() throws Exception {
            List<User> users=userDao.findById(51);
            for(User user:users) {
                System.out.println(user.toString());
            }
        }
        @Test
        public void testFindByName() throws Exception {
            List<User> users=userDao.findByName("YF%");
            for(User user:users) {
                System.out.println(user.toString());
            }
        }
    
        @Test
        public void testFindByQueryBean(){
            QueryBean qb=new QueryBean();
            User user=new User();
            user.setUsername("%X%");
            qb.setUser(user);
            List<User> users=userDao.findByQueryBean(qb);
            for(User u:users) {
                System.out.println(u.toString());
            }
        }
    
    }
    <mapper namespace="mybatis.CRUD.dao.IUserDao">
    <select id="findALL" resultType="mybatis.CRUD.bean.User">
    SELECT * FROM user;
    </select>
    <select id="findById" resultType="mybatis.CRUD.bean.User" parameterType="int">
    SELECT * FROM user WHERE id=#{id} ;
    </select>
    <select id="findByName" resultType="mybatis.CRUD.bean.User" parameterType="java.lang.String">
    SELECT * FROM USER WHERE username LIKE #{username};
    </select>
    <select id="findByQueryBean" parameterType="mybatis.CRUD.bean.QueryBean" resultType="mybatis.CRUD.bean.User">
    sELECT * FROM user WHERE username LIKE #{user.username};
    </select>
    <insert id="saveUser" parameterType="mybatis.CRUD.bean.User">
    <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
    SELECT last_insert_id();
    </selectKey>
    INSERT INTO user(username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address});
    </insert>
    <update id="updateUser" parameterType="mybatis.CRUD.bean.User">
    UPDATE user SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id};
    </update>
    <delete id="deleteUser" parameterType="int">
    DELETE FROM user WHERE id=#{id};
    </delete>
    </mapper>
  • 相关阅读:
    洛谷P1072 Hankson 的趣味题(题解)
    18.3.19晚听韩明睿大佬讲题收获
    题解+新技巧--一本通1282:最大子矩阵
    题解-洛谷P1303 A*B Problem(高精)
    Java重载和覆盖
    propagation属性的7个传播行为
    脏读、不可重复读、幻读
    RSA 数据加密和数字签名算法
    大型互联网站解决高并发的常见策略
    死锁和活锁
  • 原文地址:https://www.cnblogs.com/YFEYI/p/13187381.html
Copyright © 2020-2023  润新知