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

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

resultType="com.mtour.mybatis.demo.Student">

select * from Student where id=#{id}

4. 创建 conf.xml

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. 启动调试

1378d7149fb4135762d42e2970e6956f.png

以上所述是小编给大家介绍的通过MyBatis读取数据库数据并提供rest接口访问,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 JSP 中使用 MyBatis 读取数据库,需要遵循以下步骤: 1. 配置 MyBatisMyBatis 的配置文件中配置数据源和 SQL 映射文件的路径。 2. 创建 SQL 映射文件 在 SQL 映射文件中编写 SQL 语句,并使用 MyBatis 提供的标签进行 SQL 语句的拼接和参数的设置。 3. 在 JSP 中调用 MyBatis 在 JSP 中使用 JSTL 或 EL 表达式调用 MyBatis 读取数据库,可以使用 JSTL 的 `<sql:setDataSource>` 标签设置数据源,然后使用 `<sql:query>` 标签执行 SQL 语句,也可以使用 EL 表达式调用 MyBatis 的 Mapper 接口执行 SQL 语句。 以下是一个简单的示例: 1. MyBatis 配置文件 mybatis-config.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/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/mapper/UserMapper.xml"/> </mappers> </configuration> ``` 2. SQL 映射文件 UserMapper.xml: ``` <?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.example.mapper.UserMapper"> <select id="getUserById" resultType="com.example.model.User"> select * from user where id=#{id} </select> </mapper> ``` 3. JSP 页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>MyBatis JSP Demo</title> </head> <body> <sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="root" password="123456"/> <sql:query var="user" dataSource="${dataSource}"> SELECT * FROM user WHERE id = ${param.id} </sql:query> <table> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> <c:forEach var="row" items="${user.rows}"> <tr> <td>${row.id}</td> <td>${row.name}</td> <td>${row.age}</td> </tr> </c:forEach> </table> </body> </html> ``` 这个示例展示了如何使用 JSTL 和 MyBatis 读取数据库,其中 `<sql:setDataSource>` 标签设置了数据源,`<sql:query>` 标签执行了 SQL 语句,EL 表达式 `${param.id}` 传递了参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值