返回顶部

收藏

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

相关聚客文章
  1. admin 发表 2016-06-13 13:36:44 Spring+SpringMVC+FreeMarker+Mybatis项目的搭建
  2. honway 发表 2015-01-02 15:22:09 MyBatis入门
  3. 博主 发表 2015-02-25 15:47:55 MyBatis-Generator最佳实践
  4. coderbee 发表 2015-09-13 14:10:41 隐式类型转换导致全表扫描
  5. zhu329599788@126 发表 2017-01-11 04:06:34 MyBatis 轻量级Java ORM框架
  6. 博主 发表 2017-10-22 06:15:53 MyBatis-Generator最佳实践
  7. buzheng 发表 2013-12-18 02:47:29 MyBatis 物理分页插件
  8. 博主 发表 2017-05-09 07:21:00 Generate Java Code with MyBatis Generator
  9. 博主 发表 2017-05-30 16:06:59 MyBatis-Generator最佳实践
  10. honway 发表 2015-01-03 14:03:31 MyBatis入门(下,完)
  11. 博主 发表 2016-04-26 09:40:55 Id Generator实现方案
  12. Harries 发表 2016-09-08 01:47:02 MyBatis介绍

发表评论