1 mysql 创建数据库脚本
- PhpMyAdmin SQL Dump-- Version 4.2.11-- http: //www.phpmyadmin.net--- Hôte: LocalHost-- Temps de production: 2016-08-02 18: 13: 50-- 服务器版本 : 5.6.21-- PHP Version: 5.6.3set Sql_Mode = "no_auto_value_on_zero"; Set Time_ze = "+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 */;---- Base de données: `Mybatis` ---- ------------------------------------------------------------ 表的结构` Student` - Créer une table s'il n'existe pas `étudiant '(` id` int (10) pas null, `name` varchar (256) non null,` `Âge' Int (4) Null) moteur = myisam auto_incremenment = 2 par défaut CHARSET = LATIN1; «Étudiant» - Insertion dans «Student» («id», «Name», `` Age ») Valeurs (1», «Zhansan», 20); ---- Indexes pour les tables à vidange - `id»); --- Auto_increment pour tables à vidange - Auto- `Student`modify` id` int (10) pas null auto_increment, auto_increment = 2; / *! 40101 set caractères_set_client = @ old_character_set_client * /; / *! 40101 set caractères_set_results = @ old_caracter_set_results * /; * /;
2 创建与数据库表 Étudiant 对应的 Pojo 类
Package com.mtour.mybatis.demo; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelementPublic classe {int id; name de chaîne; int; public int getID () {return id;} public void setid (int id) {this.id = id;} public String GetName () {retourne; 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 Mappep 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} </lect> </papper> 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-3config.dtd"><configuration> id = "Development"> <transactionManager type = "JDBC" /> <! - 配置数据库连接信息 -> <dataSource type = "poolled"> <propriété name = "driver" value = "com.mysql.jdbc.driver" /> <propriété name = "url" value = "jdbc: mysql: // localhost: 3306 / mybatis" /> <woftaméli value = "root" /> <propriété name = "mot de passe" value = "" /> </ dataSource> </vironter> </ Environments> <mappers> <mapper ressource = "com / mtour / mybatis / démo / studentmapper.xml" /> </mappers> </ configuration>
5. 创建 repos 资源
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 javax.ws.rs.get; Importer Javax.ws.rs.path; Importer javax.ws.rs.produces; import javax.ws.rs.pathparam; Importer javax.ws.rs.core.mediaType; @Path ("/ Student") Demo de classe publique {static String Resource = "conf.xml"; static inputStream is = Demo.class.getClassLoader (). GetResourCeasStream (Resource); static sqlSessionFactory = @ get @produces (mediaTexe.Text). Sayhello () {return "Hello Jersey, première démo"; } @Get @path ("/ {param}") @produces ("text / plain; charset = utf-8") String public sayhellotoutf8 (@pathparam ("param") String username) {return "hello" + username; } @Get @path ("/ getStudent / {id}") @produces (mediaType.Application_Json) Étudiant public getuserjson (@pathparam ("id") String id) {Integer StudentId = Integer.Valueof (id); sqlSession session = sessionfactory.opeSpesse (); String Status = String State = "com.mtour.mybatis.demo.studentmapper.getStudent"; étudiant s = session.selectone (déclaration, étudiantID); session.close (); return s; }}6. 启动调试
源码下载 : http: //xiazai.vevb.com/201605/yuanma/mybatisdemo (JB51) .rar
以上所述是小编给大家介绍的通过 mybatis 读取数据库数据并提供 repos 接口访问 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!