1 mysql 创建数据库脚本
-phpmyadmin SQL DUMP-الإصدار 4.2.11-- http: //www.phpmyadmin.net- المضيف: localhost-وقت التوليد: 2016-08-02 18: 13: 50-- "+00: 00" ؛/ *! 40101 SETELD_CHARACTER_SET_CLIENT = character_set_client */؛/ *! 40101 SETELD_CHARACTER_SET_RESULTS = character_set_results */؛/؛! 40101 set_collation_connection = collation_connection *؛ قاعدة البيانات: `mybatis` ---- ------------------------------------------------------------------ 表的结构` student`-- table table إذا لم يكن موجودًا `student` (` id` int (10) not null ، `name` varchar (256) null ،` ation (4) int (4) null) "الطالب"-إدخال في "الطالب" ("المعرف" ، "الاسم" ، "العمر") (1 ، "Zhansan" ، 20) ؛ ---- فهارس الجداول المملوءة ------ فهارس الجدول "الطالب"-جدول الطالب "الطالب". `` student`modify `id` int (10) not null auto_increment ، auto_increment = 2 ؛/*! */؛2 创建与数据库表 الطالب 对应的 pojo 类
package com.mtour.mybatis.demo ؛ import javax.xml.bind.annotation.xmlrootelement ؛ xmlrootelementplic class student {int id ؛ string name ؛ int age ؛ الاسم) {this.name = name ؛} public int getage () {return age ؛} public void setage (int age) {this.age = age ؛}}3 创建映射 الطالب
<؟ مساحة name.mtour.mybatis.demo.studentMapper "> <select id =" getStudent "parametertype =" int "resulttype =" com.mtour.mybatis.demo.student "> select * edr where id =#id} </select>
4. 创建 conf.xml
<؟ id = "development"> <TransactionManager type = "jdbc" /> <!-配置数据库连接信息-> <dataSource type = "prouded"> <property name = "driver" value = "com.mysql.jdbc.driver" /> <property name = "url value = "root"/> <property name = "password" value = "" "/> </dataSource> </eversion> </eversionments> <mappers> <mapper resource =" com/mtour/mybatis/demo/studentMapper.xml "/> </mappers> </configuration>
5. 创建 راحة 资源
package com.mtour.mybatis.demo ؛ استيراد java.io.ioException ؛ استيراد java.io.inputstream ؛ استيراد org.apache.ibatis.session.sqlsession javax.ws.rs.get ؛ استيراد javax.ws.rs.path ؛ استيراد javax.ws.rs.produces ؛ استيراد javax.ws.rs.pathparam ؛ استيراد javax.ws.rs.core.mediatepe ؛ path ("/student") demo class public {static string resource = "conf.xml" ؛ static inputStream هو = demo.class.getClassLoader (). Sayhello () {return "Hello Jersey ، First Demo" ؛ } getPath ("/{param}") produces ("text/plain ؛ charset = utf-8") السلسلة العامة sayhellotoutf8 (pathparam ("param") username) {return "hello" + username ؛ } get @path ("/getStudent/{id}") produces (mediaType.application_json) الطالب العام getUserjson (pathparam ("id") "com.mtour.mybatis.demo.studentMapper.getStudent" ؛ الطالب s = session.selectone (بيان ، studentId) ؛ session.close () ؛ return s ؛ }}6. 启动调试
: http: //xiazai.vevb.com/201605/yuanma/mybatisdemo (JB51) .Rar
以上所述是小编给大家介绍的通过 mybatis 读取数据库数据并提供 rest 接口访问 , 希望对大家有所帮助 , , , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!