国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

通過MyBatis讀取數據庫數據并提供rest接口訪問

2019-11-26 14:00:52
字體:
來源:轉載
供稿:網友

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.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic 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.VeVB.COm/201605/yuanma/mybatisDemo(jb51).rar

以上所述是小編給大家介紹的通過MyBatis讀取數據庫數據并提供rest接口訪問,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清镇市| 来凤县| 柏乡县| 岑巩县| 安阳县| 湘潭县| 隆尧县| 长沙市| 沈阳市| 得荣县| 卢湾区| 开江县| 涿州市| 舞阳县| 秀山| 天祝| 贵定县| 桃源县| 陆河县| 武城县| 扶沟县| 满城县| 永顺县| 商洛市| 荥阳市| 遵义市| 永善县| 三门峡市| 嘉黎县| 阿克苏市| 承德县| 汽车| 股票| 德昌县| 天等县| 云阳县| 灵川县| 东乡族自治县| 丁青县| 大厂| 额尔古纳市|