MyBatis summary Day01

1. Introduction to MyBatis

​ MyBatis is an excellent semi-automatic persistence layer framework that supports ordinary SQL queries, stored procedures and advanced mapping . MyBatis eliminates almost all manual settings of JDBC code and parameters and retrieval and packaging of result sets. Instead, simple XML or annotations are used to configure the original mapping, and the interface and Java POJO (Ordinary Java Object) are mapped to the database. record of.

​ Semi-automated: Need to write SQL statements by hand.

JDBC–>dbutils–>MyBatis–>Hibernate (too automated)

2. Getting started with MyBatis

2.1 Guide package

【mybatis】
mybatis-3.1.1.jar
【MYSQL 驱动包】
mysql-connector-java-5.1.7-bin.jar

2.2 Configure the mybatis-config.xml file

<?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/XXX" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/XXXMapper.xml"></mapper>
        (可以配置多个mapper)
    </mappers>
</configuration>

2.3 Writing Dao layer interface

public interface StudentDao {
    
    
    /**
     * 查询所有学生
     * @return
     */
    List<Student> queryAllStudent();

    /**
     * 增加一个学生
     * @param student
     * @return
     */
    int insertStudent(Student student);

    /**
     * 删除一个学生
     * @param sid
     * @return
     */
    int deleteStudent(Integer sid);

    /**
     * 修改一个学生(测试{param1}形式接收参数)
     * @param name
     * @param sex
     * @return
     */
    int updateStudent(String name,String sex,Integer sid);
}

2.4 Configure the corresponding Mapper.xml mapping file It is
recommended to create a Mapper folder to store various Mapper mapping files

<?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.xxx.dao.StudentDao(接口全类名)">
    <select id="queryAllStudent" resultType(返回类型)="com.xxx.entity.Student">
        select * from student
    </select>

    <insert id="insertStudent" parameterType(提供参数类型)="com.xxx.entity.Student">
        insert into student(sid,sname,ssex,classid) values(#{sid},#{sName},#{sSex},#{classId})
    </insert>

    <update id="updateStudent" >
        update student set sname=#{param1},ssex=#{param2} where sid=#{param3}
    </update>

    <delete id="deleteStudent">
        delete from student where sid=#{sid}
    </delete>
</mapper>

3. BUG encountered in use


Insert picture description here
Solution: add the time zone after the database connection url in mybatis-config.xml


Insert picture description here
Solution: Configure mapper in mybatis-config.xml
or the namespace in the mapper mapping file is not the full class name of the dao layer interface

③Solution
Insert picture description here
: pay attention to the field name corresponding to the SQL statement in the mapper mapping file

④If
Insert picture description here
the Maven project used may report this error, it is a compatibility problem of IDEA.

⑤Solution
Insert picture description here
: Do not use main method to test, use junit test

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44233253/article/details/115020660