参考资料:http://mybatis.github.io/mybatis-3/zh/index.html
1)项目结构
2)Bus.java
package com.mybatis.po; public class Bus { private String busID; private String routeID; private String currentStationID; private String nextStationID; private int state; private double lat; private double lng; private String busLPN; private long updateTime; public String getBusID() { return busID; } public void setBusID(String busID) { this.busID = busID; } public String getRouteID() { return routeID; } public void setRouteID(String routeID) { this.routeID = routeID; } public String getCurrentStationID() { return currentStationID; } public void setCurrentStationID(String currentStationID) { this.currentStationID = currentStationID; } public String getNextStationID() { return nextStationID; } public void setNextStationID(String nextStationID) { this.nextStationID = nextStationID; } public int getState() { return state; } public void setState(int state) { this.state = state; } public double getLat() { return lat; } public void setLat(double lat) { this.lat = lat; } public double getLng() { return lng; } public void setLng(double lng) { this.lng = lng; } public String getBusLPN() { return busLPN; } public void setBusLPN(String busLPN) { this.busLPN = busLPN; } public long getUpdateTime() { return updateTime; } public void setUpdateTime(long updateTime) { this.updateTime = updateTime; } }
3)BusDao.java
package com.mybatis.dao; import java.util.List; import com.mybatis.po.Bus; public interface BusDao { List<Bus> selectBus(String busID); }
4)Test.java
package com.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mybatis.dao.BusDao; import com.mybatis.po.Bus; public class Test { public static void main(String[] args) { String resource = "com/mybatis/config/mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); SqlSession session = sqlSessionFactory.openSession(); System.out.println(session); BusDao bdMapper = session.getMapper(BusDao.class); List<Bus> buses = bdMapper.selectBus("8146"); System.out.println(buses.size()); Bus bus = buses.get(0); System.out.println(bus.getBusID() + "\t" + bus.getCurrentStationID()); session.close(); } }
5)config.properties
#MyBatis-config.xml DB Connection Config driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/transit?characterEncoding=utf-8 username=root password=root
6)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> <properties resource="com/mybatis/config/config.properties"> <property name="username" value="root" /> <property name="password" value="root" /> </properties> <environments default="development"> <environment id="development"> <!-- JDBC | MANAGED --> <transactionManager type="JDBC" /> <!-- UNPOOLED | POOLED | JNDI --> <dataSource type="UNPOOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <!-- Using classpath relative resources --> <mapper resource="com/mybatis/config/BusDao.xml" /> </mappers> </configuration>
7)BusDao.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.mybatis.dao.BusDao"> <select id="selectBus" resultType="com.mybatis.po.Bus"> select * from bus where busID = #{busID} </select> </mapper>
8)Database Script
MYSQL
-- ---------------------------- -- Table structure for bus -- ---------------------------- DROP TABLE IF EXISTS `bus`; /* 公交车信息 */ CREATE TABLE `bus` ( `busID` varchar(32) NOT NULL COMMENT '公交车ID', `routeID` varchar(32) DEFAULT NULL COMMENT '公交路线编号', `currentStationID` varchar(32) DEFAULT NULL COMMENT '当前站点编号', `nextStationID` varchar(32) DEFAULT NULL COMMENT '下一站点编号', `state` int(11) DEFAULT NULL COMMENT '当前状态0/1', `lat` double DEFAULT NULL COMMENT '纬度', `lng` double DEFAULT NULL COMMENT '经度', `bus_lpn` varchar(45) DEFAULT NULL COMMENT '公交车牌照', `updateTime` time DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`busID`), KEY `bus_route_id_idx` (`routeID`), KEY `current_station_id_idx` (`currentStationID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of bus -- ---------------------------- INSERT INTO `bus` VALUES ('8146', '46-7', '1559', '1100', '0', '31.877692', '120.523078', '8146', '13:04:40'); INSERT INTO `bus` VALUES ('8413', '24-1', '2860', '2940', '0', '31.917505', '120.701187', '8413', '13:19:08');