1 MySQL Create database script
--phpMyAdmin SQL Dump--version 4.2.11--http://www.phpmyadmin.net----host:localhost--Generation time:2016-08-0
2 18:13:50-Server version: 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 '----------------------------------------------------------------the structure of the table ' Student '--CREATE TA BLE IF not EXISTS ' Student ' (' id ' int (+) not null, ' name ' varchar (256) NOT NULL, ' age ' int (4) NOT null) Engine=myisam A
uto_increment=2 DEFAULT charset=latin1;
The data in the----Dump table ' Student '--INSERT into ' Student ' (' id ', ' name ', ' age ') VALUES (1, ' Zhansan ', 20);
----Indexes for dumped tables------the 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 (ten) 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 the Pojo class corresponding to the database table student
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 Creating Maps 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"/>
<!--metabase 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.getClassLo
Ader (). getResourceAsStream (Resource);
static Sqlsessionfactory sessionfactory = new Sqlsessionfactorybuilder (). Build (IS);
@GET @Produces (mediatype.text_plain) public String SayHello () {return "Hello Jersey, the" @GET @Path ("/{param}") @Produces ("Text/plain;charset=utf-8") public String sayHelloToUTF8 (@PathParam ("param") strin
G 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 Download: Http://xiazai.jb51.net/201605/yuanma/mybatisDemo (jb51). rar
The above is a small set for you to read through the MyBatis database data and provide rest interface access, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!