CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '默認姓名' COMMENT '姓名', `age` int(11) DEFAULT '1', `sex` varchar(255) DEFAULT NULL, `random` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<mappers> <package name="org.mybatis.builder"/> </mappers>
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '默認姓名' COMMENT '姓名', `age` int(11) DEFAULT '1', `sex` varchar(255) DEFAULT NULL, `random` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<?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/sampledb?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/myMapper.xml"/> </mappers> </configuration>
<?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="mapper.myMapper"> <select id="selectStudent" resultType="first.Student"> select * from student where id = #{id} </select> </mapper>
package first; public class Student { private Long id; private String name; private Integer age; private String sex; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { final StringBuilder sb = new StringBuilder("Student{"); sb.append("id=").append(id); sb.append(", name='").append(name).append('\''); sb.append(", age=").append(age); sb.append(", sex='").append(sex).append('\''); sb.append('}'); return sb.toString(); } }
package first; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test { public static void main(String[] args) throws Exception { /* * 每一個基於 MyBatis 的應用都是以一個 SqlSessionFactory 的實例爲中心的。 * SqlSessionFactory 的實例能夠經過 SqlSessionFactoryBuilder 得到。 * 而 SqlSessionFactoryBuilder 則能夠從 XML 配置文件或一個預先定製的 Configuration 的實例構建出 SqlSessionFactory 的實例。 * */ String resource = "config/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); /* * 從 SqlSessionFactory 中獲取 SqlSession * */ SqlSession session = sqlSessionFactory.openSession(); try { Student student = (Student) session.selectOne("mapper.myMapper.selectStudent", 2); System.out.println(student); } finally { session.close(); } } }
package first; public interface MyMapper { Student selectStudent(Integer id); }
<?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="first.MyMapper"> <select id="selectStudent" resultType="first.Student"> select * from student where id = #{id} </select> </mapper>
package first; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test2 { public static void main(String[] args) throws Exception { /* * 每一個基於 MyBatis 的應用都是以一個 SqlSessionFactory 的實例爲中心的。 * SqlSessionFactory 的實例能夠經過 SqlSessionFactoryBuilder 得到。 * 而 SqlSessionFactoryBuilder 則能夠從 XML 配置文件或一個預先定製的 Configuration 的實例構建出 SqlSessionFactory 的實例。 * */ String resource = "config/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"development"); /* * 從 SqlSessionFactory 中獲取 SqlSession * */ SqlSession session = sqlSessionFactory.openSession(); try { MyMapper mapper = session.getMapper(MyMapper.class); Student student = mapper.selectStudent(2); System.out.println(student); } finally { session.close(); } } }
MyMapper mapper = session.getMapper(MyMapper.class); Student student = mapper.selectStudent(2);