?????????????????????
 

??????????п????????
?????????£?
 

?????????User
????package com.oumyye.model;
????public class User {
????private String id;
????private String name;
????private  int age;
????public String getId() {
????return id;
????}
????public void setId(String 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;
????}
????@Override
????public String toString() {
????return "User [id=" + id + "?? name=" + name + "?? age=" + age + "]";
????}
????}
??????????UserMapping.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.oumyye.mapping.UserMapping">
????<!-- ??select????б?д?????SQL??? ????select?????id?????getUser??id?????????????????????
???????parameterType???????????????????????resultType???????????????????????
????resultType="com.oumyye.model.User"???????????????????User????????
????User????users??????????????
????-->
????<!--
????????id?????????user????
????-->
????<select id="getUser" parameterType="String"
????resultType="com.oumyye.model.User">
????select * from user where id=#{id}
????</select>
????</mapper>
??????????mybatis.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" />
????<!-- ???????????????? -->
????<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="root" />
????</dataSource>
????</environment>
????</environments>
????<mappers>
????<mapper resource="com/oumyye/mapping/userMapping.xml"/>
????</mappers>
????</configuration>
??????????
????package test;
????import java.io.InputStream;
????import org.apache.ibatis.session.SqlSession;
????import org.apache.ibatis.session.SqlSessionFactory;
????import org.apache.ibatis.session.SqlSessionFactoryBuilder;
????import org.junit.Test;
????import com.oumyye.model.User;
????public class Tests {
????@Test
????public void test(){
????String resource = "mybatis.xml";
????//??????????????mybatis???????????????????????????????
????InputStream is = Tests.class.getClassLoader().getResourceAsStream(resource);
????//????sqlSession?????
????SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
????SqlSession session = sessionFactory.openSession();
????/**
????* ???sql???????????
????* com.oumyye.mapping.UserMapping??userMapper.xml?????mapper?????namespace????????
????* getUser??select?????id??????????select?????id???????????????е?SQL
????*/
????String statement = "com.oumyye.mapping.UserMapping.getUser";//???sql?????????
????//??в?????????user?????sql
????User user = session.selectOne(statement?? "1123");
????System.out.println(user.toString());
????}
????}
?????????