• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

通过MyBatis读取数据库数据并提供rest接口访问

java 搞代码 4年前 (2022-01-05) 43次浏览 已收录 0个评论

这篇文章主要介绍了通过MyBatis读取数据库数据并提供rest接来源gao*daima.com搞@代#码网口访问 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下

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.3 SET 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.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Student { int id; String name; int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

3 创建映射 studentMapper

    select * from Student where id=#{id} 

4. 创建 conf.xml

     <!-- 配置数据库连接信息 -->  

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.xml"; static InputStream is = demo.class.getClassLoader().getResourceAsStream(resource); static SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello() { return "hello jersey , first demo" ; } @GET @Path("/{param}") @Produces("text/plain;charset=UTF-8") public String sayHelloToUTF8(@PathParam("param") String username) { return "Hello " + username; } @GET @Path("/getstudent/{id}") @Produces(MediaType.APPLICATION_JSON) public Student getUserJson(@PathParam("id") String id) { Integer studentId = Integer.valueOf(id); SqlSession session = sessionFactory.openSession(); String statement = "com.mtour.mybatis.demo.studentMapper.getStudent"; Student s = session.selectOne(statement, studentId); session.close(); return s; } }

6. 启动调试

源码下载:http://xiazai.gaodaima.com/201605/yuanma/mybatisDemo(jb51).rar

以上所述是小编给大家介绍的通过MyBatis读取数据库数据并提供rest接口访问,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对gaodaima搞代码网网站的支持!

以上就是通过MyBatis读取数据库数据并提供rest接口访问的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:通过MyBatis读取数据库数据并提供rest接口访问

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址