1 mysql 创建数据库脚本
-PhpMyadmin SQL Dump-- Versão 4.2.11-- http: //www.phpmyadmin.net--- Host: Localhost-- Tempo de geração: 2016-08-02 18: 13: 50-- 服务器版本 :: 5.6.21-- PHP Versão: 5.6.3Set Sql_mod = "noUtO_UTO :: "+00: 00";/ *! 40101 Definir @old_character_set_client = @@ caractere_set_client */;/ *! 40101 Definir @old_character_set_results =@@CAREMELTE_SET_RESULTS */; *! 40101 Set @old_collation_connection = @Results * Banco de dados: `mybatis` ---- -------------------------------------------------------------- 表的结构` Student`- Create Tabela Se não existe `Student` (` id` int (10) não null, `name`` `` `idade 'int (4) NULL) MOTIN = Myisam` `Age` Int (4) NULL) = Myisam` `` Age` Int (4) NULL) `Student`-inserir valores` aluno` (`id`,` name`, `idade) (1, 'zhansan', 20); ---- ÍNDICES PARA TABELAS DUMPADAS ------ ÍNDICES PARA TABLE` ALUSEM `ALTER TABLE 'TABLE` Chave primária (`id`); `Student`modify` id` int (10) não nulo auto_increntry, auto_increntry = 2;/*! 40101 Definir caractere_set_client =@old_character_set_client*/;/*! 40101 Set Character_Set_Result =@Old_Character_Set_Result*/; */;
2 创建与数据库表 Aluno 对应的 POJO 类
pacote com.mtour.mybatis.demo; importar javax.xml.bind.annotation.xmlrootelement; @xmlrootelementpublic class Student {int id; string name; int age; public int getId () {retornar id; setName (nome da string) {this.name = name;} public int getage () {Age de retorno;} public void Setage (int Age) {this.age = Age;}}3 创建映射 Studentmapper
<? xml versão = "1.0" coding = "utf-8"?> <! namespace = "com.mtour.mybatis.demo.studentmapper"> <select id = "getStudent" parametertype = "int" resulttype = "com.mtour.mybatis.demo.student"> selecione * estudante onde id =#{id} </select> 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 tipo = "jdbc" /> <!-配置数据库连接信息-> <dataSource type = "pooled"> <propriedade name = "driver" value = "com.mysql.jdbc.driver" /> <nome da propriedade = "url" = "jdbc: mysql: // localhost: 330:" value = "root"/> <propriedade name = "senha" value = ""/> </dataSource> </ambiental
5. 创建 Rest 资源
pacote com.mtour.mybatis.demo; importar java.io.ioException; importar java.io.inputStream; importar org.apache.ibatis.session.sqlSession; importação org. importar javax.ws.rs.path; importar javax.ws.rs.produces; importar javax.ws.rs.pathparam; importar javax.ws.rs.core.mediatype; @Path ("/Student") Public Class Demo {Static String Resource = "conf.xml"; estático inputStream IS = Demo.class.getClassLoader (). GetResourceasStream (Resource); STATIC SQLSessionFactory = new SqlSessionFactoryBuilder (). Sayhello () {retorne "Hello Jersey, Primeira Demo"; } @Get @path ("/{param}") @Produces ("text/plana; charset = utf-8") public string sayHelloToutf8 (@pathparam ("param") string userr) {return "hello" + userName; } @Get @path ("/getStudent/{id}") @produces (mediatype.application_json) public student getUserjson (@pathparam ("id") string id) {integeridid = integger.valueof (id); sqlsession session = session.openssion (); "com.mtour.mybatis.demo.studentmapper.getStudent"; Student s = session.selectone (declaração, StudentId); session.close (); retorno s; }}6. 启动调试
源码下载: http: //xiazai.vevb.com/201605/yuanma/mybatisdemo (jb51) .rar
以上所述是小编给大家介绍的通过 mybatis 读取数据库数据并提供 Rest 接口访问 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!