java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MyBatis mapping类

MyBatis mapping类基本用法

作者:channingbj

这篇文章主要为大家介绍了MyBatis mapping类基本用法示例详解,
有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

定义mapping类

MyBatis 有两种定义查询结果到 Java 类的映射关系的方式,一种是通过xml文件定义,一种是通过Java annonation 定义,这里使用第二种方法。

现在我们有一张mysql的表定义如下:

CREATE TABLE `MY_BATIS_TEST` (
  `id` varchar(255) NOT NULL DEFAULT '',
  `url` varchar(255) DEFAULT NULL
)

首先定义table一条数据在Java中对应的class

public class Redord {
    public String url;
}

定义sql查询到Java class 结果集合的映射:

public interface SimpleMapper {
    @Select("select url from testdb.MY_BATIS_TEST limit 1;")
    Redord selectOneRecord();
    @Select("select url from testdb.MY_BATIS_TEST;")
    Set<Record> selectRecords();
    @Select("select url from testdb.MY_BATIS_TEST where id=#{id};")
    Record selectRecordByID(int id);
}

初始化并注册mapping类

Properties properties = new Properties();
properties.setProperty("driver", "com.mysql.jdbc.Driver");
properties.setProperty("url", "jdbc:mysql://127.0.0.1:3306/testdb");
properties.setProperty("username", "the_user_name");
properties.setProperty("password", "the_password");
PooledDataSourceFactory pooledDataSourceFactory = new PooledDataSourceFactory();
pooledDataSourceFactory.setProperties(properties);
DataSource dataSource = pooledDataSourceFactory.getDataSource();
Environment environment = new Environment("development", new JdbcTransactionFactory(), dataSource);

Configuration configuration = new Configuration(environment);
configuration.addMapper(SimpleMapper.class); //注册mapping类

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

从mysql中查询数据

SqlSession session = sqlSessionFactory.openSession();
try{
    PluginMapper mapper = session.getMapper(PluginMapper.class);
    Plugin pl = mapper.selectPluginsByID(1000);
    System.out.println(pl.url);
} finally {
    session.close();
}

全局唯一以及线程安全

SqlSessionFactory 可以在整个app的生命周期中只创建一次,SqlSession需要在每次执行sql的函数中创建一次并且使用后需要进行关闭:

SqlSession session = sqlSessionFactory.openSession();
try {
  // do work
} finally {
  session.close();
}

以上就是MyBatis mapping类基本用法的详细内容,更多关于MyBatis mapping类的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文