1 mysql 创建数据库脚本
-phpmyadmin sql dump-- versi 4.2.11-- http: //www.phpMyadmin.net--- host: localhost-- waktu generasi: 2016-08-02 18: 13: 50-- 服务器版本 服务器版本 : : »5.6.21-- Versi PHP: 5.6.3set SQL_MODE =" no_a_zo_zo_ze: no_zo_zo "no_zo" no_zo "no_zo; "+00: 00";/ *! 40101 Set @old_character_set_client = @@ karakter_set_client */;/ *! 40101 Set @old_character_set_results = @@ character_set_results */;/ *40101 Set @old_collation_connection = @@ Collation_conny- @@ Collation_conn. Database: `mybatis` ---- ------------------------------------------------------------ 表的结构` Siswa`-Buat tabel jika tidak ada `student` (` id` int (10) bukan nol, `name` varchar (256) bukan null,` usia` int (4) bukan null) `name` varchar (256) bukan null,` usia` int (4) bukan null) `name` varchar (256) bukan null,` usia` int (4) bukan null) `name` varchar (256) not null,` even` int (4) not null) `name 'varchar (256) not null,` even` int (4) not null) engine = myisam auto; `Siswa`-Mengererkan ke` student` (`id`,` name`, `Age`) nilai (1, 'zhansan', 20); ---- indeks untuk tabel yang dibuang ------ indeks untuk tabel` student`-tabel `tabel forter` tabel tabel `` `` `` `iD`; `Student`modify` id` int (10) bukan null auto_increment, auto_increment = 2;/*! 40101 Set karakter_set_client =@old_character_set_client*/;/*! 40101 set karakter_set_result =@old_character_set_results*/;/;/; */;
2 创建与数据库表 Siswa 对应的 Pojo 类
Paket com.mtour.mybatis.demo; import javax.xml.bind.annotation.xmlrooteLement; @xmlrooteLementPublic class public get public {int id; name string; int this.id public getid () {return id public void setid public (int id) {this. setName (name string) {this.name = name;} public int getage () {return usia;} public void setage (int usia) {this.age = usia;}}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 namespace = "com.mtour.mybatis.demo.studentmapper"> <pilih id = "getStudent" parameTerType = "int" resultType = "com.mtour.mybatis.demo.student"> Pilih * dari siswa di mana id =#{id} </mapper> 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<" config.dtd"> <configation>< "configry>" config.dtd"> " 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"/> <name properti = "kata sandi" value = ""/> </dateSource> </urveening> </sulvirments> <mappers> <mapper resource = "com/mtour/mybatis/demo/studentmapper.xml"/> </pappers> </konfigurasi>
5. 创建 istirahat 资源
Paket com.mtour.mybatis.demo; impor java.io.ioexception; impor java.io.inputstream; impor org.apache.ibatis.session.sqlsession; impor org.apache.ibatis.Sesesies.sqlsessionfactory; impor org.apache.apache.ibatis.sesesiespyfactoryf; javax.ws.rs.get; impor javax.ws.rs.path; impor javax.ws.rs.produces; impor javax.ws.rs.pathparam; impor javax.ws.rs.core.mediatype; @Path ("/student") Demo kelas publik {statis string resource = "conf.xml"; static inputstream is = demo.class.getClassLoader (). GetResourceasStream (sumber daya); static SqlSessionFactory (iSpactory = new SqlSessionFactoryBactoryBuilder (). string publik sayhello () {return "Hello Jersey, First Demo"; } @Get @path ("/{param}") @produces ("text/polos; charset = utf-8") string publik SayHelloToutF8 (@pathparam ("param") string username) {return "hello" + username; } @Get @path ("/getStudent/{id}") @produces (mediatype.application_json) Mahasiswa publik getUserjson (@pathparam ("id") ID string) {integer studentId = integer.valueof (id); SQLSesion sesion = sessionFacory.opension. "com.mtour.mybatis.demo.studentmapper.getstudent"; student s = session.selectone (pernyataan, studentId); session.close (); return s; }}6. 启动调试
源码下载 : http: //xiazai.vevb.com/201605/yuanma/mybatisdemo (jb51) .rar
以上所述是小编给大家介绍的通过 mybatis 读取数据库数据并提供 istirahat 接口访问 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!