mybatis接口映射器

mybatis interface mapper

这个配置有什么问题?尝试使用接口而不是 xml 配置?我怎样才能做到这一点?找不到任何教程... 我的原因是:java.io.IOException:找不到资源 Student_mapper。我的文件夹位置是默认的。 "src/main/java" 文件夹中的 Student-mapper.java。或者我如何仅使用带有注释的 bean 配置来做到这一点?没有 xml

这是我的代码:

Student_mapper

public interface Student_mapper {

    final String getAll = "SELECT * FROM STUDENT";
    final String getById = "SELECT * FROM STUDENT WHERE ID = #{id}";
    final String deleteById = "DELETE from STUDENT WHERE ID = #{id}";
    final String insert = "INSERT INTO STUDENT (NAME, BRANCH, PERCENTAGE, PHONE, EMAIL ) VALUES (#{name}, #{branch}, #{percentage}, #{phone}, #{email})";
    final String update = "UPDATE STUDENT SET EMAIL = #{email}, NAME = #{name}, BRANCH = #{branch}, PERCENTAGE = #{percentage}, PHONE = #{phone} WHERE ID = #{id}";

    @Select(getAll)
    @Results(value = {
            @Result(property = "id", column = "ID"),
            @Result(property = "name", column = "NAME"),
            @Result(property = "branch", column = "BRANCH"),
            @Result(property = "percentage", column = "PERCENTAGE"),
            @Result(property = "phone", column = "PHONE"),
            @Result(property = "email", column = "EMAIL")
    })

    List getAll();

    @Select(getById)
    @Results(value = {
            @Result(property = "id", column = "ID"),
            @Result(property = "name", column = "NAME"),
            @Result(property = "branch", column = "BRANCH"),
            @Result(property = "percentage", column = "PERCENTAGE"),
            @Result(property = "phone", column = "PHONE"),
            @Result(property = "email", column = "EMAIL")
    })

    Student getById(int id);

    @Update(update)
    void update(Student student);

    @Delete(deleteById)
    void delete(int id);

    @Insert(insert)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Student student);
}

主要class

public class Annotations_Example {

    public static void main(String args[]) throws IOException{

        Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlSessionFactory.openSession();
        session.getConfiguration().addMapper(Student_mapper.class);

        Student_mapper mapper = session.getMapper(Student_mapper.class);

        //Create a new student object
        Student student = new Student();

        //Set the values
        student.setName("zara");
        student.setBranch("EEE");
        student.setEmail("zara@gmail.com");
        student.setPercentage(90);
        student.setPhone(123412341);

        //Insert student data
        mapper.insert(student);
        System.out.println("record inserted successfully");
        session.commit();
        session.close();

    }

}

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

    <typeAliases>
        <typeAlias alias = "Student" type = "Student"/>
    </typeAliases>

    <environments default = "development">
        <environment id = "development">
            <transactionManager type = "JDBC"/>

            <dataSource type = "POOLED">
                <property name = "driver" value = "org.postgresql.Driver"/>
                <property name = "url" value = "jdbc:postgresql://localhost:5432/postgres"/>
                <property name = "username" value = "postgres"/>
                <property name = "password" value = "postgres"/>
            </dataSource>

        </environment>
    </environments>

    <mappers>
        <mapper resource = "Student_mapper.java"/>
    </mappers>

</configuration>

您可以只使用 java 配置,而不是混合使用 xml 和 java 配置。

//Create data Source

 public DataSource getDataSource() {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("");
        dataSourceBuilder.url("");
        dataSourceBuilder.username("");
        dataSourceBuilder.password("");
        return dataSourceBuilder.build();   



//Inject dataSourec to SqlSessionFactory
public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(getDataSource());
        return factoryBean.getObject();   

//inject SqlSessionFactory to Mapper Suppose
public MapperFactoryBean<Object it can be return type> yourMapper() throws Exception {
        MapperFactoryBean<YourMapper> factoryBean = new MapperFactoryBean<>(YourMapper.class);
        factoryBean.setSqlSessionFactory(sqlSessionFactory());
        return factoryBean;
    }   

然后使用yurMapper对象调用里面定义的函数