你的位置:首页 > Java教程

[Java教程]通过mybatis读取数据库数据并提供rest接口访问


1 mysql 创建数据库脚本

-- phpMyAdmin SQL Dump-- version 4.2.11-- http://www.phpmyadmin.net---- Host: localhost-- Generation Time: 2016-08-02 18:13:50-- 服务器版本: 5.6.21-- PHP Version: 5.6.3SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";SET time_zone = "+00:00";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;---- Database: `mybatis`---- ------------------------------------------------------------ 表的结构 `Student`--CREATE TABLE IF NOT EXISTS `Student` (`id` int(10) NOT NULL, `name` varchar(256) NOT NULL, `age` int(4) NOT NULL) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;---- 转存表中的数据 `Student`--INSERT INTO `Student` (`id`, `name`, `age`) VALUES(1, 'zhansan', 20);---- Indexes for dumped tables------ Indexes for table `Student`--ALTER TABLE `Student` ADD PRIMARY KEY (`id`);---- AUTO_INCREMENT for dumped tables------ AUTO_INCREMENT for table `Student`--ALTER TABLE `Student`MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

2  创建与数据库表Student对应的pojo类

package com.mtour.mybatis.demo;import javax.

3  创建映射 studentMapper

<?

4.  创建 conf.

<?

5.  创建 rest 资源

package com.mtour.mybatis.demo;import java.io.IOException;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType;  @Path("/student") public class demo { 		static String resource = "conf.

6. 启动调试 

 

源码下载: