通過(guò)MyBatis讀取數(shù)據(jù)庫(kù)數(shù)據(jù)并提供rest接口訪問(wèn)
1 mysql 創(chuàng)建數(shù)據(jù)庫(kù)腳本
-- phpMyAdmin SQL Dump -- version 4.2.11 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: 2016-08-02 18:13:50 -- 服務(wù)器版本: 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` -- -- -------------------------------------------------------- -- -- 表的結(jié)構(gòu) `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; -- -- 轉(zhuǎn)存表中的數(shù)據(jù) `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 創(chuàng)建與數(shù)據(jù)庫(kù)表Student對(duì)應(yīng)的pojo類(lèi)
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 創(chuàng)建映射 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. 創(chuàng)建 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" /> <!-- 配置數(shù)據(jù)庫(kù)連接信息 --> <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. 創(chuàng)建 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. 啟動(dòng)調(diào)試
源碼下載:http://xiazai.jb51.net/201605/yuanma/mybatisDemo(jb51).rar
以上所述是小編給大家介紹的通過(guò)MyBatis讀取數(shù)據(jù)庫(kù)數(shù)據(jù)并提供rest接口訪問(wèn),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字
這篇文章主要介紹了用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字,需要的朋友可以參考下2017-02-02關(guān)于Spring?Cache?緩存攔截器(?CacheInterceptor)
這篇文章主要介紹了關(guān)于Spring?Cache緩存攔截器(?CacheInterceptor),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot工程打包與運(yùn)行的實(shí)現(xiàn)詳解
本文主要介紹了SpringBoot工程的打包與運(yùn)行的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Springboot Websocket Stomp 消息訂閱推送
本文主要介紹了Springboot Websocket Stomp 消息訂閱推送,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07Spring的Aware接口實(shí)現(xiàn)及執(zhí)行順序詳解
這篇文章主要為大家介紹了Spring的Aware接口實(shí)現(xiàn)及執(zhí)行順序詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12淺析Spring Boot中的spring-boot-load模塊
spring-boot-loader模塊允許我們使用java -jar archive.jar運(yùn)行包含嵌套依賴(lài)jar的jar或者war文件,它提供了三種類(lèi)啟動(dòng)器。下面通過(guò)本文給大家介紹spring-boot-load模塊的相關(guān)知識(shí),感興趣的朋友一起看看吧2018-01-01Spring中的@Conditional注解實(shí)現(xiàn)分析
這篇文章主要介紹了Spring中的@Conditional注解實(shí)現(xiàn)分析, @Conditional是Spring 4出現(xiàn)的注解,但是真正露出價(jià)值的是Spring Boot的擴(kuò)展@ConditionalOnBean等,需要的朋友可以參考下2023-12-12SpringBoot啟動(dòng)執(zhí)行sql腳本的3種方法實(shí)例
在應(yīng)用程序啟動(dòng)后,可以自動(dòng)執(zhí)行建庫(kù)、建表等SQL腳本,下面這篇文章主要給大家介紹了關(guān)于SpringBoot啟動(dòng)執(zhí)行sql腳本的3種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01