Read database data via MyBatis and provide rest interface access

Source: Internet
Author: User
Tags mysql create mysql create database phpmyadmin

1 MySQL CREATE database script

--phpMyAdmin SQL dump--version 4.2.11--http://www.phpmyadmin.net----host:localhost--Generation time:2016-08-02 18:1 3: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 @[email protected] @CHARACTER_SET_CLIENT */;/*!40101 set @[email  Protected] @CHARACTER_SET_RESULTS */;/*!40101 set @[email protected] @COLLATION_CONNECTION */;/*!40101 set NAMES UTF8 */;----Database: ' mybatis '----------------------------------------------------------------table structure ' Student '-- CREATE TABLE IF not EXISTS ' Student ' (' id ' int (ten) not null, ' name ' varchar (in) not NULL, ' age ' int (4) NOT null) ENGINE =myisam auto_increment=2 DEFAULT charset=latin1;----Dump the data in the table ' Student '--insert into ' Student ' (' id ', ' name ', ' age ') VALUE S (1, ' Zhansan ');----Indexes for dumped tables------Indexes for table ' Student '--alter table ' Student ' ADD PRIMARY KE Y (' id ');----auto_increment for dumped tables------auto_increment for table ' Student '--alTER TABLE ' Student ' MODIFY ' id ' int (ten) not NULL auto_increment,auto_increment=2;/*!40101 SET [email protected]_ Character_set_client */;/*!40101 Set [email protected]_character_set_results */;/*!40101 set [email  protected]_collation_connection * *;

2 Creating the 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.) {this.age = age;}}

3 Creating a mapping 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"/> <!--Configuring 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> </environmen ts> <mappers> <mapper resource= "Com/mtour/mybatis/demo/studentmapper.xml"/> </MAPPERS&G    T </configuration> 

5.   Create 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.getClassLoa Der (). 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 Sayhellotoutf      8 (@PathParam ("param") String username) {return "Hello" + username; } @GET @Path ("/getstudent/{ID} ") @Produces (Mediatype.application_json) public Student Getuserjson (@PathParam (" id ") String ID) {I               Nteger 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 Download:

Read database data via MyBatis and provide rest interface access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.