返回顶部

收藏

java使用MyBatis的@Select注解

更多

java使用MyBatis的@Select注解示例:

package cn.outofmemory.snippets.enterprise;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class HowUseSelectMyBatisAnnotation {

    private static final String conf = "mybatis.conf.xml";

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

        Reader reader = Resources.getResourceAsReader(conf);

        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        SqlSessionFactory sessionFactory = builder.build(reader);

        SqlSession session = sessionFactory.openSession();

        long id = 1;

        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Employee employee = mapper.findById(id);

        System.out.println(employee.getId() + " - " + employee.getName() +
                " - " + employee.getSurname());

    }

}

package cn.outofmemory.snippets.enterprise;

import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

public interface EmployeeMapper {

    @Select("SELECT * FROM employee WHERE id = #{id}")
    @ResultMap("employeeResultMap")
    Employee findById(long id);

}

package cn.outofmemory.snippets.enterprise;

import java.util.Date;

public class Employee {

    private Long id;
    private String name;
    private String surname;
    private String title;
    private Date created;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }

}

mybatis.conf.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/companydb" />
                <property name="username" value="jcg" />
                <property name="password" value="jcg" />
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="EmployeeMapper.xml" />
    </mappers>

</configuration>

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

<mapper namespace="cn.outofmemory.snippets.enterprise.EmployeeMapper">
    <resultMap id="employeeResultMap" type="cn.outofmemory.snippets.enterprise.Employee">
        <result column="name" property="name"/>
        <result column="surname" property="surname"/>
        <result column="title" property="title"/>
    </resultMap>
</mapper>

输出:

1 - Jack - Thomson

标签:java,MyBatis

收藏

0人收藏

支持

0

反对

0

发表评论