MyBatis annotations entry

step one:

Remove mapping configuration file (IUserDao.xml), use the method dao interface @Selectannotation, and specify SQL

package com.dao;
import com.domain.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface IUserDao {
    //查询所有操作
    @Select("select * from user")
    List<User> findAll();
}

Step two:

At the same time when the mapper configuration SqlMapConfig.xml, dao class attribute specified interface fully qualified class name

<?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">
<!--mybatis的主配置文件-->
<configuration>
    <!--配置环境-->
    <environments default="mysql">
        <!--配置mysql的环境-->
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源 也叫连接池-->
            <dataSource type="POOLED">
                <!--配置连接数据库的四个基本信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            </dataSource>
        </environment>
    </environments>
    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件
        如果是用注解来配置的话,此处应该使用class属性指定被注解的dao权限类名
    -->
    <mappers>
        <mapper class="com.dao.IUserDao"/>
    </mappers>
</configuration>

Published 165 original articles · won praise 8 · views 9003

Guess you like

Origin blog.csdn.net/wait_13/article/details/104270345