MyBatis 3 入门
首先是数据源的配置,内容如下:
?接下来是mapper的配置:
?下面是Java类,bean如下:
package com.mybatis.lin;import java.io.Reader;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 {//读取数据源配置Reader reader = Resources.getResourceAsReader("dataResource.xml");//build SqlSessionFactory实例SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);//获取SqlSessionSqlSession session = sqlMapper.openSession();//获取MapperUserMapper userMapper = session.getMapper(UserMapper.class);//执行sqlUser user = userMapper.getUser("lin lvping");if (user != null) {System.out.println("username:"+user.getUsername()+", password:"+user.getPassword());}}}?测试结果如下:
username:lin lvping, password:123456
?
以上均参考自官方的guide文件:MyBatis-3-User-Guide.pdf,有空的同学可以仔细读读,很容易读懂的。