主函数和mapper文件 select * from userinfo select * from userinfo where id=#{id} select a.nextval from dual insert into userinfo values(#{id},#{name},#{pwd},#{rolesid}) select * from userinfo where name=#{name} and pwd=#{pwd} package o
package org.jxnd.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 org.jxnd.bean.UserInfo; import org.jxnd.dao.UserInfoDao; public class Test { static SqlSessionFactory sqlSessionFactory; public static void main(String[] args) throws Exception { SqlSession session=getSqlSession(); UserInfoDao dao=session.getMapper(UserInfoDao.class); getByNameAndPwd(dao); // getById(dao); // add(dao); // getAll(dao); } private static void getByNameAndPwd(UserInfoDao dao) { UserInfo info=dao.selectByNameAndPwd("王麻子", "123"); System.out.println(info); } private static void getAll(UserInfoDao dao) { // TODO Auto-generated method stub List select a.nextval from dual insert into userinfo values(#{id},#{name},#{pwd},#{rolesid})list=dao.getUserAll(); for (UserInfo userInfo : list) { System.out.println(userInfo); } } private static void add(UserInfoDao dao) { // TODO Auto-generated method stub dao.add(new UserInfo(1, "王二麻子", "123", 1)); } private static void getById(UserInfoDao dao) { // TODO Auto-generated method stub System.out.println(dao.getUserById(1)); } static{ String resource = "mybatis.xml"; InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //sqlsession不能使用单例,sqlsessionFactory可以使用单例 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }