Read database data through mybatis and provide rest interface access, mybatisrest
1. mysql database creation script
-- PhpMyAdmin SQL Dump -- version 4.2.11 -- http://www.phpmyadmin.net ---- Host: localhost -- Generation Time: 18:13:50 -- server Version: 5.6.21 -- PHP version: 5.6.3SET 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 '---- 'studen' of the orders TABLE structure -- create table if not exists 'studen' ('id' int (10) not null, 'name'varchar (256) not null, 'age' int (4) not null) ENGINE = MyISAM AUTO_INCREMENT = 2 default charset = latin1; ---- data in the table to be stored 'studen' -- insert into 'studen' ('id', 'name', 'age') VALUES (1, 'zhansan', 20); ---- Index Es for dumped tables ------ Indexes for table 'studen' -- alter table 'studen' add primary key ('id '); ---- AUTO_INCREMENT for dumped tables ------ AUTO_INCREMENT for table 'studen' -- alter table 'studen' 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. Create a pojo class corresponding to the database table Student
package com.mtour.mybatis.demo;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic 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. Create a stuing 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. Create 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"/> <! -- Configure database connection information --> <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. Create a rest Resource
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. Start debugging
Source code download: http://files.cnblogs.com/files/mtour/mybatisDemo.zip