Mybatis的学习笔记(从零开始)

0.思路:搭建环境—>导入mybatis—>编写代码—>测试

参考文档:mabatis中文文档

1.搭建环境

1.首先通过maven导入jar包

<dependencies>
<!--        mysql驱动-->
<!--        <dependency>-->
<!--            <groupId></groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>


    </dependencies>

2.创建一个mybatis-config.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;characterEncoding=utf8&amp;useUnicode=true"/>
                <property name="username" value="root"/>
                <property name="password" value="lin"/>
            </dataSource>
        </environment>
    </environments>

</configuration>

具体的配置要自己修改
3.编写代码…

4.org.apache.ibatis.binding.BindingException: Type interface dao.UserDao is not known to the MapperRegistry.
第一个常见的错误:进行解决

 <!--每一个Mapper.xml都需要在mybatis核心配置文件中注册! -->
    <mappers>
       <mapper resource="dao/UserMapper.xml">
           
       </mapper>

第二个常见的错误:

Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  

原因:由于maven的约定大于配置,我们之后可能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案:

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>

            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

而且

 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;characterEncoding=utf8&amp;useUnicode=true"/>

上面的useSSL一定要等于false,否则就会报错,切记(具体原因不知道为啥)

5.CRUD(增删改查)

id:就是对应的namespace中的方法名
resultType:sql语句执行的返回值
parameterType:参数类型
然后在dao接口和xml文件中进行

//接口的代码
package dao;

import pojo.User;

import java.util.List;

public interface UserMapper {
    //查询全部用户
    List<User> getUserList();
    //根据id查询用户
    User getUserById(int id);
    //insert一个用户
    int adduser(User user);
    //修改用户
    int updateUser(User user);
    //删除一个用户
    int deleteUser(int id);
}

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

<!--namespace绑定一个对应的Dao接口 -->
<mapper namespace="dao.UserMapper">

    <!--查询语句-->
<select id="getUserList" resultType="pojo.User">
    select * from mybatis.user
</select>
    <select id="getUserById" parameterType="int" resultType="pojo.User">
        select * from mybatis.user where id = #{id}
    </select>
<insert id="adduser" parameterType="pojo.User" >
    insert into mybatis.user (companyname, username, pwd) VALUES (#{companyname},#{username},#{pwd});
</insert>
    <update id="updateUser" parameterType="pojo.User" >
        update mybatis.user set companyname=#{companyname},username=#{username},pwd=#{pwd}  where id=#{id};
    </update>
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id = #{id};
    </delete>

</mapper>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值