zoukankan      html  css  js  c++  java
  • 3、Mybatis的CRUD实现

    ------------恢复内容开始------------

    1、实体类配置文件解释:

    <?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">
    
    <!--命名空间:绑定UserDao相当于实现这个接口-->
    <mapper namespace="com.zhixi.dao.IUserDao">
        
        <!--不需要再写接口的实现类,在配置文件里写sql即可-->
        <select id="getUserList" resultType="com.zhixi.pojo.User">
            select * from mybatis.user;
        </select>
        <insert id=""></insert>
    </mapper>

    在配置文件中写CRUD代码即可

    1、namespace
    namespace中的包名要和Dao/mapper接口的包名- -致!
    2、select
    选择,查询语句;

    • id:就是对应的namespace中的方法名;
    • resultType: Sq|语句执行的返回值类型!
    • parameterType :参数类型!

    2、编写代码

    1、根据id查询用户

      1、在dao层接口编写业务类型

    public interface IUserDao {/**
         * 根据用户id查询用户
         */ User getUserById(int id);
    }

      2、编写实现类(.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">
    
    <!--命名空间:绑定UserDao相当于实现这个接口-->
    <mapper namespace="com.zhixi.dao.IUserDao">
    
        <!--不需要再写接口的实现类,在配置文件里写sql即可-->
        <select id="getUserById" resultType="com.zhixi.pojo.User" parameterType="int">
            select * from mybatis.user where id = #{id}
        </select>
    </mapper>

      3、测试类

    package com.zhixi.Dao;
    
    import com.zhixi.dao.IUserDao;
    import com.zhixi.pojo.User;
    import com.zhixi.util.MybatisUtils;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    import java.util.List;
    
    public class IUserDaoTest {
    
        @Test
        public void getUserByIdTest(){
            //1、获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //2、获取映射器接口
            IUserDao mapper = sqlSession.getMapper(IUserDao.class);
    
            //3、调用接口中根据id查询用户的方法
            User user = mapper.getUserById(1);
            System.out.println(user);
    
            sqlSession.close();
        }
    }

     2、增删改查:增删改需要提交事务

    IUserDao:编写业务功能

    package com.zhixi.dao;
    
    import com.zhixi.pojo.User;
    
    import java.util.List;
    
    /**
     * @author zhangzhixi
     */
    public interface IUserDao {
        /**
         * 查询所有的用户
         */
        List<User> getUserList();
    
        /**
         * 根据用户id查询用户
         */
        User getUserById(int id);
    
        /**
         * 添加用户
         */
        int addUser(User user);
    
        /**
         * 删除用户
         */
        int delUser(int id);
    
        /**
         * 修改用户
         */
        int updateUser(User user);
    }

    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">
    
    <!--命名空间:绑定UserDao相当于实现这个接口-->
    <mapper namespace="com.zhixi.dao.IUserDao">
    
        <!--不需要再写接口的实现类,在配置文件里写sql即可-->
              id是接口中的方法名                    
    
        <select id="getUserList" resultType="com.zhixi.pojo.User">
            /*查询所有用户*/
            select * from mybatis.user;
        </select>
    
        <select id="getUserById" resultType="com.zhixi.pojo.User" parameterType="int">
            /*根据id查询用户*/
            select * from mybatis.user where id = #{id};
        </select>
    
        <!--对象中的属性可以直接取出来-->
        <insert id="addUser" parameterType="com.zhixi.pojo.User">
            /*插入用户*/
            insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd});
        </insert>
    
        <delete id="delUser" parameterType="int">
            /*删除用户*/
            delete from mybatis.user where id = #{id};
        </delete>
    
        <update id="updateUser" parameterType="com.zhixi.pojo.User">
            /*修改用户*/
            update mybatis.user set name=#{name},pwd=#{pwd}  where id = #{id};
        </update>
    </mapper>

    测试类:添加背景的就是固定代码

    package com.zhixi.Dao;
    
    import com.zhixi.dao.IUserDao;
    import com.zhixi.pojo.User;
    import com.zhixi.util.MybatisUtils;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    import java.util.List;
    
    public class IUserDaoTest {
        @Test
        public void test() {
            //1、获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
    
            IUserDao userDao = sqlSession.getMapper(IUserDao.class);
            List<User> userList = userDao.getUserList();
            for (User user : userList) {
                System.out.println(user);
            }
            //关闭流
            sqlSession.close();
        }
    
        @Test
        public void getUserByIdTest() {
            //1、获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //2、获取映射器接口
            IUserDao mapper = sqlSession.getMapper(IUserDao.class);
    
            //3、调用接口中根据id查询用户的方法
            User user = mapper.getUserById(1);
            System.out.println(user);
    
            sqlSession.close();
        }
    
        @Test
        public void addUserTest() {
            //1、获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //2、获取映射器接口
            IUserDao mapper = sqlSession.getMapper(IUserDao.class);
            //3、插入
            int demo = mapper.addUser(new User(6, "demo", "123"));
            sqlSession.commit();
            if (demo > 0) {
                System.out.println("插入用户成功!");
            }
    
            sqlSession.close();
        }
    
        @Test
        public void delUserTest() {
            //1、获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //2、获取映射器接口
            IUserDao mapper = sqlSession.getMapper(IUserDao.class);
            //3、插入
            int i = mapper.delUser(6);
            //提交事务
            sqlSession.commit();
    
            if (i > 0) {
                System.out.println("删除用户成功!");
            }
            sqlSession.close();
        }
    
        @Test
        public void updateUserTest() {
            //1、获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //2、获取映射器接口
            IUserDao mapper = sqlSession.getMapper(IUserDao.class);
            //3、修改
            int i = mapper.updateUser(new User(4, "zhangzhixi", "zhixi158"));
            if (i > 0) {
                System.out.println("修改数据成功!");
            }
            //提交事务
            sqlSession.commit();
            sqlSession.close();
        }
    }

     3、模糊查询

    IUserDao:

    public interface IUserDao {
        /**
         * 模糊查询
         */
        List<User> getUserLike(String value);
    }

    UserMapper:

    <?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">
    
    <!--命名空间:绑定UserDao相当于实现这个接口-->
    <mapper namespace="com.zhixi.dao.IUserDao">
    
        <select id="getUserLike" resultType="com.zhixi.pojo.User" parameterType="String">
            /*模糊查询*/
            select * from mybatis.user where name like "%"#{value}"%";
        </select>
    
    </mapper>

    测试:

    public void selUserLikeTest() {
            //1、获取SQLSession对象
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            //2、获取映射器接口
            IUserDao mapper = sqlSession.getMapper(IUserDao.class);
    
            List<User> userLike = mapper.getUserLike("z");
            for (User user : userLike) {
                System.out.println(user);
            }
    
            sqlSession.close();
        }

    找到名字中带有z的人、这种字符串拼接的方式容易造成SQL注入,需要注意,在使用时候常用:

    /*模糊查询*/
    select * from mybatis.user where name like #{value};
    List<User> userLike = mapper.getUserLike("%z%");  
  • 相关阅读:
    CSS之关于clearfix--清除浮动
    C#之垃圾回收
    C#缓存处理
    Asp.Net Mvc4 Ajax提交数据成功弹框后跳转页面
    Asp.Net MVC Ajax
    C#和SQL操作Xml
    领域模型
    Sql操作表字段
    水晶报表行之间切换颜色
    触发器-Trigger
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14197669.html
Copyright © 2011-2022 走看看