1 Mysql 创建数据库脚本
-PhpMyAdmin SQL Dump-- Version 4.2.11-- http: //www.phpmyadmin "+00: 00";/ *! 40101 set @old_character_set_client = @@ charakter_set_client */;/ *! 40101 set @old_character_set_results = @@charySet_Results */;/ *! 40101 set @old_connection Datenbank: `mybatis` ---- ---------------------------------------------------- 表的结构` Student`— create Tabelle Wenn nicht existiert `student` (` id` int (10) nicht null, `name` varchar (256) nicht null,` älter (4), nicht null) motor = myisam auto, myisam autoAncrement = 2; "Student"-in "Student" ("id", "name", "Alter") Werte (1, "Zhansan", 20); `Student`modify` id` int (10) nicht null auto_increment, auto_increment = 2;/*! */;2 创建与数据库表 Student 对应的 Pojo 类
paket com.mtour.mybatis.demo; import javax.xml.bind.annotation setName (String name) {this.name = name;} public int getage () {return ay;} public void setage (int ay) {this.age = älter;}}3 创建映射 StudentMapper
<? 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
<? id = "Entwicklung"> <transactionManager type = "jdbc" /> <!-配置数据库连接信息-> <dataSource type = "Pooled"> <Eigenschaft name = "treiber" value = "com.mysql.jdbc.driver value = "root"/> <Eigenschaft name = "password" value = ""/> </dataSource> </umwelt> </Umgebungen> <mappers> <mapper resource = "com/mtour/mybatis/Demo/studentMapper.xml"/> </mappers> </konfiguriert
5. 创建 Ruhe 资源
paket com.mtour.mybatis.demo; import Java.io.ioxception; import Java.io.inputStream; import org.apache.ibatis.session.sqlSession; javax.ws.rs.get; importieren javax.ws.rs.path; importieren javax.ws.rs.Produces; importieren javax.ws.rs.pathparam; import Javax.ws.rs.core.MediType; @Path ("/student") öffentliche Klasse Demo {static String ressource = "conf.xml"; static InputStream ist = Demo.class.getClassloader (). GetResourceAsStream (Ressource); statische SQLSessionFactory Sessionfactory = New SQLSessionFactoryBubers (). Sayhello () {return "Hallo Jersey, erste Demo"; } @Get @Path ("/{Param}") @Produces ("text/plain; charset = utf-8") public String SayHellOutf8 (@PathParam ("Param") String Benutzername) {return "Hallo" + Benutzername; } @Get @Path ("/getStudent/{id}") @Produces (mediateType.application_json) Public Student GetUserjson (@PathParam ("id") String id) {integer studentId = integer.ValueOf (id); "com.mtour.mybatis.demo.studentmapper.getStudent"; Student S = Session }}6. 启动调试
源码下载 : http: //xiazai.vevb.com/201605/yuanma/mybatisdemo (JB51) .rar
以上所述是小编给大家介绍的通过 mybatis 读取数据库数据并提供 Rest 接口访问 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!