程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 經由過程MyBatis讀取數據庫數據並供給rest接口拜訪

經由過程MyBatis讀取數據庫數據並供給rest接口拜訪

編輯:關於JAVA

經由過程MyBatis讀取數據庫數據並供給rest接口拜訪。本站提示廣大學習愛好者:(經由過程MyBatis讀取數據庫數據並供給rest接口拜訪)文章只能為提供參考,不一定能成為您想要的結果。以下是經由過程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.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

<?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="com.mtour.mybatis.demo.studentMapper">
<select id="getStudent" parameterType="int" 
resultType="com.mtour.mybatis.demo.Student">
select * from Student where id=#{id}
</select>
</mapper>

4. 創立 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:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mtour/mybatis/demo/studentMapper.xml"/>
</mappers>
</configuration>

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.jb51.net/201605/yuanma/mybatisDemo(jb51).rar

以上所述是小編給年夜家引見的經由過程MyBatis讀取數據庫數據並供給rest接口拜訪,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved