mybatis entry --day01: configured via annotations

1. Attach a copy of a copy of improvement (notes). Due to the configuration using annotations, so no xml, and delete the resources in the file com

Here Insert Picture Description
Here's what com in IUserDao.xml inside:

<?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.itheima.dao.IUserDao">
    <!--配置查询所有-->
    <select id="findAll" resultType="com.itheima.domain.User">
        select * from user
    </select>
</mapper>

Because deleted IUserDao.xml, so the location of the configuration file of the master configuration file to specify the mapping disappeared:

    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
    <mappers>
        <mapper resource="com/itheima/dao/IUserDao.xml"/>
    </mappers>

It needs to be changed:

    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
    <!--如果是用注解来配置,此处应该使用class属性指定被注解的dao全限定类名-->
    <mappers>
        <mapper class="com.itheima.dao.IUserDao"/>
    </mappers>

Because deleted IUserDao.xml, so the need to re-write the query. Join @Select ( "select * from user") in IUserDao interface.

public interface IUserDao {

    //查询所有操作
    @Select("select * from user")
    List<User> findAll();
}

Then run, it is still feasible.

2. A summary of 1

The xml configuration to configuration steps Notes:
① delete mapping configuration file resources in IUserDao.xml
② mappers will master the contents of the configuration file to read:

	<mappers>
        <mapper class="com.itheima.dao.IUserDao"/>
    </mappers>

I.e. mappers in the main configuration file attribute is used to specify the fully qualified class name of the class dao interface.
Add query ③ using annotations in IUserDao interface mode:

public interface IUserDao {

    //查询所有操作
    @Select("select * from user")
    List<User> findAll();
}

Guess you like

Origin blog.csdn.net/dl674756321/article/details/90665432