欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

通过MyBatis读取数据库数据并提供rest接口访问

程序员文章站 2024-03-13 10:56:57
1 mysql 创建数据库脚本 -- phpmyadmin sql dump -- version 4.2.11 -- http://www.phpmyadm...

1 mysql 创建数据库脚本

-- phpmyadmin sql dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- host: localhost
-- generation time: 2016-08-02 18:13:50
-- 服务器版本: 5.6.21
-- php version: 5.6.3
set sql_mode = "no_auto_value_on_zero";
set time_zone = "+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 */;
--
-- database: `mybatis`
--
-- --------------------------------------------------------
--
-- 表的结构 `student`
--
create table if not exists `student` (
`id` int(10) not null,
`name` varchar(256) not null,
`age` int(4) not null
) engine=myisam auto_increment=2 default charset=latin1;
--
-- 转存表中的数据 `student`
--
insert into `student` (`id`, `name`, `age`) values
(1, 'zhansan', 20);
--
-- indexes for dumped tables
--
--
-- indexes for table `student`
--
alter table `student`
add primary key (`id`);
--
-- auto_increment for dumped tables
--
--
-- auto_increment for table `student`
--
alter table `student`
modify `id` int(10) not null auto_increment,auto_increment=2;
/*!40101 set character_set_client=@old_character_set_client */;
/*!40101 set character_set_results=@old_character_set_results */;
/*!40101 set collation_connection=@old_collation_connection */;

2 创建与数据库表student对应的pojo类

package com.mtour.mybatis.demo;
import javax.xml.bind.annotation.xmlrootelement;
@xmlrootelement
public class student {
int id;
string name;
int age;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getname() {
return name;
}
public void 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 mapper 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}
</select>
</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>
<environments default="development">
<environment 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" />
<property name="password" value="" />
</datasource>
</environment>
</environments>
<mappers>
<mapper resource="com/mtour/mybatis/demo/studentmapper.xml"/>
</mappers>
</configuration>

5. 创建 rest 资源

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.sqlsessionfactorybuilder;
import javax.ws.rs.get; 
import javax.ws.rs.path; 
import javax.ws.rs.produces; 
import javax.ws.rs.pathparam; 
import javax.ws.rs.core.mediatype; 
@path("/student") 
public class demo { 
static string resource = "conf.xml";
static inputstream is = demo.class.getclassloader().getresourceasstream(resource);
static sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is);
@get 
@produces(mediatype.text_plain) 
public string sayhello() { 
return "hello jersey , first demo" ; 
} 
@get 
@path("/{param}") 
@produces("text/plain;charset=utf-8") 
public string sayhellotoutf8(@pathparam("param") string username) { 
return "hello " + username; 
} 
@get 
@path("/getstudent/{id}") 
@produces(mediatype.application_json) 
public student getuserjson(@pathparam("id") string id) { 
integer studentid = integer.valueof(id);
sqlsession session = sessionfactory.opensession();
string statement = "com.mtour.mybatis.demo.studentmapper.getstudent";
student s = session.selectone(statement, studentid);
session.close();
return s; 
}
}

6. 启动调试

通过MyBatis读取数据库数据并提供rest接口访问

源码下载:http://xiazai.jb51.net/201605/yuanma/mybatisdemo(jb51).rar

以上所述是小编给大家介绍的通过mybatis读取数据库数据并提供rest接口访问,希望对大家有所帮助