mybaits知识点

mybaits知识点非注解方式
SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--配置mybatis环境 -->
    <environments default="en1">
        <!--配置mysql环境 可以有多个数据库,自己测试用一个正紧开发用一个-->
        <environment id="en1">
<!--        配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
<!--            配置数据源(连接池)-->
            <dataSource type="POOLED">
<!--                配置数据库的四个基本信息-->
                <property name="password" value=""/>
                <property name="url" value="jdbc:mysql://localhost:3306/kidsshop?characterEncoding=utf8"/>
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="username" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
<!--        配置mapper.xml文件-->
        <mapper resource="com/dao/mapping.xml"/>
    </mappers>
</configuration>

mappering的基本格式

<?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="com.dao.CartDao">
    <insert id="insert1" parameterType="com.info.Cart">
        insert cart(cart_id,user_id)values(#{cart_id},#{user_id});
    </insert>
    <select id="selectAll" resultType="com.info.Cart">
        select *from cart;
    </select>
</mapper>

测试方法

    @Test
    public void test() throws IOException {
        InputStream in=Resources.getResourceAsStream("com/dao/mybatis.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        CartDao cartDao=sqlSession.getMapper(CartDao.class);
        Cart c=new Cart();
        c.setCart_id(1);
        c.setUser_id(1);

//        List<Cart>list=cartDao.selectAll();
//        for(Cart ca:list){
//            System.out.println(ca.getCart_id());
//        }
        cartDao.insert1(c);
    }

dao接口类

package com.dao;

import com.info.Cart;

import java.util.List;

public interface CartDao {
  void insert1(Cart c);
  List<Cart>selectAll();
}

上一篇:【Android】【GreenDao】GreenDao缓存机制中隐藏的炸弹


下一篇:简述 SSH 的概念以及中主要的设计思想?