Mybatis的CRUD操作

一、编写实体类和dao接口

二、主配置文件SqlMapConfig.xml


<configuration>
    
    <properties resource="jdbcConfig.properties">properties>
    
    <typeAliases>
        
        <package name="com.li.domain"/>
    typeAliases>
    
    <environments default="mysql">
        
        <environment id="mysql">
            
            <transactionManager type="JDBC">transactionManager>
            
            <dataSource type="POOLED">
                
                <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 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">

    <select id="findAll" resultType="user">
        select * from user
    select>

    <insert id="saveUser" parameterType="user">
        
      <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>

    <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>

    <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 userList= userDao.findAll();
        userList.forEach(user -> System.out.println(user));
    }

 

你可能感兴趣的:(Mybatis的CRUD操作)