• Mybatis的CRUD操作


    一、编写实体类和dao接口

    二、主配置文件SqlMapConfig.xml

    <!--Mybatis的主配置文件-->
    <configuration>
        <!--   数据库的连接信息 -->
        <properties resource="jdbcConfig.properties"></properties>
        <!-- 使用typeAliases配置别名,它只能配置domain中类的别名-->
        <typeAliases>
            <!--用于指定别名配置的包,当指定后,该包下的类会注册别名,并且类名就是别名,不区分大小写-->
            <package name="com.li.domain"/>
        </typeAliases>
        <!--    配置环境-->
        <environments default="mysql">
            <!--        配置mysql环境-->
            <environment id="mysql">
                <!--           配置与事务相关的-->
                <transactionManager type="JDBC"></transactionManager>
                <!--            配置连接池-->
                <dataSource type="POOLED">
                    <!--                配置4个数据库的连接信息-->
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                </dataSource>
            </environment>
        </environments>
        <!--    配置映射文件-->
        <mappers>
            <!--package标签是用于指定dao接口所在的包,当指定之后就保护需要写Mapper或resource或者class-->
           <package name="com.li.dao"/>
        </mappers>
    </configuration>

    //jdbcConfig.properties配置文件
    
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mbase
    jdbc.username=root
    jdbc.password=123
    
    
    
     

    三、编写IUSerDao.xml配置文件

         注意:此配置文件位置要和dao接口包名一致

    <mapper namespace="com.li.dao.IUserDao">
    <!--查询所有 resultType:返回值类型分装的结果集-->
        <select id="findAll" resultType="user">
            select * from user
        </select>
    <!--保存用户 parameterType:参数类型-->
        <insert id="saveUser" parameterType="user">
            <!--配置插入操作后,获取插入操作的id
                      keyProperty:id的属性名称对应实体类
                      keyColumn:数据库中id列名
                      resultType:返回值类型
                      order:什么时候执行这个,之后还是之前-->
          <selectKey keyProperty="id" keyColumn="id" resultType="integer" order="AFTER">
    
          </selectKey>
            insert into user(username,address,sex,birthday) values (#{username},#{address},#{sex},#{birthday});
        </insert>
    <!--更新用户-->
        <update id="updateUser" parameterType="user">
            update user set username=#{username},address=#{address} where id =#{id};
        </update>
    <!--删除用户-->
        <delete id="deleteUser" parameterType="integer">
            delete from user where id=#{id};
        </delete>
    <!--通过id查询-->
        <select id="findById" resultType="user" parameterType="integer">
            select * from user where id=#{id};
        </select>
    <!--模糊查询用户信息-->
       <select id="findByName" resultType="user" parameterType="String">
           select * from user where username like #{username}
       </select>
    <!--查询用户总数-->
        <select id="findCount" resultType="integer">
            select count(id)from user;
        </select>
    <!--根据QueryVo查询用户-->
        <select id="findByQueryVo" parameterType="QueryVo" resultType="user">
            select * from user where username like #{user.username}
        </select>
    </mapper>

    四、编写测试类

    private InputStream in = null;
        private SqlSessionFactory factory = null;
        private SqlSession sqlSession = null;
        private IUserDao userDao = null;
    //主方法执行之前执行
        @Before
        public void init() throws IOException {
            //1.获取配置文件
            in = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.创建SplSessionFactory工厂
            factory = new SqlSessionFactoryBuilder().build(in);
            //3.生产Sqlsession对象
            sqlSession = factory.openSession();
            //4.创建代理dao对象
            userDao = sqlSession.getMapper(IUserDao.class);
        }
    //主方法执行之后执行
        @After
        public void destory() throws IOException {
            //提交事务
            sqlSession.commit();
            //关闭资源
            sqlSession.close();
            in.close();
        }
    
        /**
         * 查询所有
         */
        @Test
        public void findAllTest(){
            List<User> userList= userDao.findAll();
            userList.forEach(user -> System.out.println(user));
        }
  • 相关阅读:
    Linux 网络编程六(socket通信UDP版)
    Redis 集群方案介绍
    华为交换机STP 根ID优先级设置
    Pacemaker+Corosync搭建PostgreSQL集群
    SpookyOTP
    世界级的开源项目:TiDB 如何重新定义下一代关系型数据库
    TiDB
    Fiddler显示响应时间 显示服务器IP
    DNS记录类型名单
    Django models中关于blank与null
  • 原文地址:https://www.cnblogs.com/cqyp/p/12491801.html
Copyright © 2020-2023  润新知