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

1 mysql 创建数据库脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- 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类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

1
2
3
4
5
6
7
8
9
10
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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 资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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. 启动调试 

 

源码下载: http://download.csdn.net/detail/mtour/9593217

 

posted @   孤独的和弦  阅读(781)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示